-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
assert.doesNotThrow, show error message #12167
Conversation
|
7b667a0
to
36d3cb3
Compare
@vsemozhetbyt thank you for the feedback. I changed the commit text plus squashed two commits. |
Is this semver-major? If so, beware v.8 rc deadline will be closing in 2 days. |
seems like Could someone please help me to solve it? |
@krydos Given the nature of this change, I think we can ignore the Jenkins issue as unrelated. If someone really wants to, they can re-run CI. Labeling |
The |
@jasnell what if I'll change it to EDIT: |
Well, I don't think just including the original error message is useful enough. The stack trace is usually more helpful, but if we use the original stack trace that may/may not be confusing with the modified exception message. Perhaps if we separated with a newline that might be clearer? For example: err.message = 'Got unwanted exception:\n' + err.message;
throw err; Doing so would mean we would need to tweak |
@mscdex excuse me, but I didn't really understand why do we need to change how As you and @jasnell noticed, and I agree with, that I also didn't understand the meaning of |
CI 3: https://ci.nodejs.org/job/node-test-commit/8845/ (all green) |
@krydos Nevermind, I suppose it works fine for this situation because it's synchronous. |
36d3cb3
to
166a4f9
Compare
In my last commit I returned EDIT: |
test/parallel/test-assert.js
Outdated
@@ -406,7 +406,7 @@ assert.doesNotThrow(function() { assert.ifError(); }); | |||
|
|||
assert.throws(() => { | |||
assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); | |||
}, /Got unwanted exception: user message/, | |||
}, /Got unwanted exception:\ntest: user message/, | |||
'a.doesNotThrow ignores user message'); |
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.
The indenting is off 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.
@jasnell a.doesNotThrow ignores...
should be aligned with /Got unwanted exception...
. Do I understand it correctly?
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.
...probably not since jslint throws error about it.
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.
@jasnell unfortunately I didn't really understand where indenting was off :( I hope it was fixed with new commit... but probably not.
lib/assert.js
Outdated
fail(actual, expected, 'Got unwanted exception' + message); | ||
fail(actual, expected, | ||
'Got unwanted exception:\n' + | ||
actual.message + message); |
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.
Why is it 'Got unwanted exception:\n' + actual.message + message
instead of 'Got unwanted exception: ' + message + '\n' + actual.message
(more similar to what it used to be)? This way it would be:
Got unwanted exception: explain why it is unwanted.
Message of the unwanted exception.
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.
Looks really good.
But probably something like that
'Got unwanted exception' + message + '\n' + actual.message
(without : after exception
) since message
will have it (if message exists). Otherwise message will have just . (dot).
So, if message
wasn't passed it will look like:
Got unwanted exception.
Message of the unwanted exception.
I really like your idea.
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 just pushed the changes you suggested. Thank you!
test/parallel/test-assert.js
Outdated
@@ -406,7 +406,7 @@ assert.doesNotThrow(function() { assert.ifError(); }); | |||
|
|||
assert.throws(() => { | |||
assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); | |||
}, /Got unwanted exception: user message/, | |||
}, /Got unwanted exception:\ntest: user message/, | |||
'a.doesNotThrow ignores user message'); |
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.
This message is no longer accurate...this test fails if a.doesNotThrow
ignores user message or if it ignores the message of the unwanted exception..
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 completely agree with your here. I added another test case that tests unwanted exception message. I hope it is ok.
166a4f9
to
db9d871
Compare
@jasnell ping. |
test/parallel/test-assert.js
Outdated
assert.throws(() => { | ||
assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); | ||
}, /Got unwanted exception: user message\ntest/, | ||
'a.doesNotThrow ignores message of the unwanted exception'); |
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 think what @jasnell mean is that the opening '
should be directly below the opening /
. (Or just drop the extra third argument here, I think I would prefer that.)
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.
Thank you @addaleax ... yes, that's what I meant.
(I missed the earlier ping)
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 actually tried to align '
with /
but I was unable to pass linter in this case. It was saying I have wrong indentation.
Since it is ok to remove third argument this is exactly what I did.
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.
Yeah I've seen that happen before, it wants to align with the function call's opening (
.
db9d871
to
61ded4e
Compare
test/parallel/test-assert.js
Outdated
@@ -409,6 +409,10 @@ assert.throws(() => { | |||
}, /Got unwanted exception: user message/, | |||
'a.doesNotThrow ignores user message'); | |||
|
|||
assert.throws(() => { | |||
assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); | |||
}, /Got unwanted exception: user message\ntest/); |
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.
Can we add a test to confirm we don't get unhelpful strings (like "undefined") if the user leaves out the optional message
argument?
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.
Also, probably a good idea to include a test case for if a primitive is thrown rather than a proper Error
. Like, maybe throw "big problem"
. You'll probably need to disable an eslint rule with a comment because we lint for throw
being used with an unexpected type like that.
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.
@Trott thank you for recommendations. I've added one more test to check if no user message
provided.
As for second comment about throw primitive... it really displays undefined
as error text. It goes to another branch and do not entering in the if
that I touched in PR. It happens probably because util.isError
is returning false
since string isn't good Error object.
I'm still thinking about good way to fix it...
61ded4e
to
1c9f79b
Compare
Sanity of rebase: https://ci.nodejs.org/job/node-test-commit-linuxone/7173/ 🔴 |
assert.doesNotThrow() should show actual error message instead of "Got unwanted exception" which is not really helpful. PR-URL: nodejs#12167 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
@krydos Needs a rebase |
lib/assert.js
Outdated
fail(actual, expected, 'Got unwanted exception' + message); | ||
fail(actual, expected, | ||
'Got unwanted exception' + | ||
message + '\n' + actual.message); |
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.
needs a 4th argument caller
should be either assert.throws
or assert.doesNotThrow
test/parallel/test-assert.js
Outdated
@@ -409,6 +409,14 @@ assert.throws(() => { | |||
}, /Got unwanted exception: user message/, | |||
'a.doesNotThrow ignores user message'); | |||
|
|||
assert.throws(() => { |
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.
IMHO the check should be using try / catch
otherwise we get into a weird situation where we use the function to test itself and Alex Turing will get mad, and say 🛑
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.
@refack thanks for the feedback. throws
calls innerThrows
internally and doesNotThrow
calls innerThrows
internally as well. That's why we use function to test itself? Did I understand you correctly? Do we need just usual try / catch
instead of assert.throws
right?
While trying to test if I rebased this correctly I got: not ok 53 parallel/test-assert
---
duration_ms: 0.90
severity: fail
stack: |-
assert.js:544
throw actual;
^
AssertionError [ERR_ASSERTION]: Got unwanted exception: user message
[object Object]
at innerThrows (assert.js:549:7)
at Function.doesNotThrow (assert.js:566:3)
at assert.throws (/data/iojs/build/workspace/node-test-commit-linuxone/nodes/rhel72-s390x/test/parallel/test-assert.js:472:10)
at tryBlock (assert.js:514:5)
at innerThrows (assert.js:533:18)
at Function.throws (assert.js:562:3)
at Object.<anonymous> (/data/iojs/build/workspace/node-test-commit-linuxone/nodes/rhel72-s390x/test/parallel/test-assert.js:471:8)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
... Who throw the exception? The function we are testing, or the function we are using to assert the the result is as expected? 💫 |
I'm currently not sure why |
1c9f79b
to
d081803
Compare
Ok, I also could avoid using p.s. |
I think using the current thrower is fine, we could add a comment for why it's +1 to @refack 's idea of using try-catch instead of |
assert.doesNotThrow() should show actual error message instead of "Got unwanted exception" which is not really helpful.
d081803
to
d023feb
Compare
@joyeecheung @refack thanks for explanation and suggestion to use |
Quick CI just for freshness: https://ci.nodejs.org/job/node-test-commit-linuxone/7434/ |
assert.doesNotThrow() should show actual error message instead of "Got unwanted exception" which is not really helpful. PR-URL: nodejs#12167 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Landed in c53db1e |
Thanks everyone for help and feedback 🙌 |
As mentioned in #12079
doesNotThrow
doesn't show actual error message but shows Got unwanted error which is not really helpful. As @mscdex said it will be better to show actual error message so this is what I have done here.parallel/test-assert.js
also was changed a bit.I hope it will be helpful.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
assert