-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: improve common.expectsError #19797
Conversation
test/common/index.js
Outdated
console.error( | ||
'Please use `assert.throws()` instead of `common.expectsError()`. ' + | ||
'To verify the error type, compare the error name.' | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually -1 on this bit. Adding common.expectsError()
was deliberate to differentiate between checking ordinary JavaScript throws and uses of internal/errors
. If the output for assert.throws()
is better, then the better thing to do is improve the output for common.expectsError()
accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am -1 as well. It's fairly easy to change common.expectsError()
to meet whatever we need, but changing assert.throws
comes with much more implications - IIRC assert
should've not been exposed to users for the same reason, and it used to have a locked
stability index. It has been changed to stable
because we were getting rid of the locked
index altogether but I don't think we have actually discussed that changes to the public assert
are encouraged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I can follow. I can not remember the original implementation but the overloading that triggers assert.throws
is implemented by me and that was done because assert.throws
did not have the same capabilities as common.expectsError
at that point of time. Since then I added support for objects in assert.throws
and improved the error output significantly.
Right now there is still a valid use case for common.expectsError
: in case it is used as callback function. But in all other cases I believe it is better to use assert.throws
due to multiple reasons and it is also much more intuitive for new users. And the warning is there for exactly that use case: use assert.throws
directly if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joyeecheung as far as I know there were multiple discussions about this (in a general term). I believe code should never be "locked" as it is important to further improve things. It was the reason that the module became a wart in Node.js because people used it and stumbled upon deficiencies of it. Now the user experience is actually quite nice and people can use it out of my perspective. Even though there are of course more things that can be improved.
As far as I see it, you are against adding this comment. Are you against the PR in general? I have no strong feelings besides believing that we should use assert.throws
and not common.expectsError
but I will go with what ever is the opinion of others here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BridgeAR I love the work you did on assert.throws()
when I used them in our own tests, but I am still skeptical about actually changing the public assert significantly, given that it used to be locked - we did promise our users that it won't receive such a drastic change but we are now breaking the promise. Had I notice the changes to assert I would suggest to keep the changes in our internal test utilities for a while and have some discussion about this before actually exposing them to users, sorry.
BTW this is thread about removing the locked
stability index. #11200
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joyeecheung I am not aware of any bugs in assert
but I am very happy to address such immediately, when they come up :-). It confuses me though, that implementing new features in assert
has to be harder than implementing new features somewhere else in the code base?
assert
is also one of the best covered modules: https://coverage.nodejs.org/coverage-fdb35d89605de642/root/assert.js.html
https://coverage.nodejs.org/coverage-fdb35d89605de642/root/internal/util/comparisons.js.html
https://coverage.nodejs.org/coverage-fdb35d89605de642/root/internal/errors.js.html
That also applies to the used code from assert
(e.g., util.inspect).
And if requested, I can also improve the coverage to 100%.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BridgeAR #19106 and #19221 are still open. To be honest I don't really think there are good solutions to any of them, even the solutions I proposed are opinionated and users will always end up having to change their code to make it look right again in certain situations. It's not that implementing new features in public API is harder, but it's more disruptive to have opinionated, complex changes in the public API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BridgeAR The thing is, the more feature that assert receives, the more weird the whole things seem weird to me. As a user, I would not expect a good diff and color output from the built in assert module, at most I would just expect that it provides something that empowers user land to built such features on top of (e.g. properties on the Error object), and just be dumb and print everything by default. I also don't see any good way out with the EOL issue to be honest, .gitattributes
should be improved of course, but fixing it to make the assert work seems odd to me. We can't expect users all have proper .gitattributes
set, or use git to deploy their running code at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I do not see this as a issue. It is all well documented in the docs. So I think it depends on when the user read the docs the last time. My main point on this is: everything in core should not only "work" but it should also provide a good user experience.
The EOL issue is something that does not seem specific to assert
but indicates for me that we have a issue somewhere else and it is just visible in assert
because we verify the message output all the time.
976efe3
to
719b61f
Compare
The output is now improved by showing most properties all at once. Besides that this adds a warning to use `assert.throws` instead due to a better output.
719b61f
to
e72a028
Compare
I just rebased, removed the message and updated the code further to actually compare all input at the same time from now on. So it now will not matter if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan of tossing the properties around different objects, but since it's just internal test utils, LGTM
3f93fc2
to
8a76c1a
Compare
@joyeecheung I totally agree. I would not have done that if it would not be the internal tests. I just pushed another commit with some minor adjustments that I missed earlier. PTAL |
Note: the commit message has to be fixed while landing. |
Still LGTM! |
The output is now improved by showing most properties all at once. Besides that this adds a warning to use `assert.throws` instead due to a better output. PR-URL: nodejs#19797 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Landed in f85d599 |
The output is now improved by showing most properties all at once. Besides that this adds a warning to use `assert.throws` instead due to a better output. PR-URL: #19797 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
The output is now improved by showing most properties all at once.
Besides that this adds a warning to use
assert.throws
insteaddue to a better output.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes