Repository navigation
assert: inconsistency in assert.doesNotThrow #2385
Description
Activity
- addedassertIssues and PRs related to the assert subsystem.Issues and PRs related to the assert subsystem.
on Aug 15, 2015 Related: nodejs/node-v0.x-archive#6580
The docs show different signatures for
assert.doesNotThrowandassert.throwsbut they use the same code under the covers. Need to determine for certain ifassert.doesNotThrowis supposed to have an identical signature. /cc @trevnorris @cjihrigTerribly sorry for the mess above!
@jasnell
doesNotThrow()probably should not have the same signature, asthrows()since once of the arguments tothrows()is the expected error. It would be nice if the docs clarified the expected behavior of each function in more detail. The docs forthrows()don't mention whatmessagereally does, so you're forced to look in the code. The docs fordoesNotThrow()are even worse.- addeddocIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.
on Aug 25, 2015 I'm working on updating the documentation but first I want clarify the behavior of
throws/doesNotThrow. The current code (with the PR) throws anAssertionErrorin some cases but in three out of five cases. The other two are in the lastifstatement:// if not the right exception or if did not expect an exception // then rethrow if ((shouldThrow && actual && expected && !expectedException(actual, expected)) || (!shouldThrow && actual)) { throw actual; }
and this simply rethrows the error from the block. That seems inconsistent and counter-intuitive as I, at least, would've expected
assertto only throwAssertionError. These two also ignoremessage.The Assert API is now Locked. Should this be closed? Or is this a bug fix? Does it matter that it's a bug that does not seem to adversely affect Node.js project tests?
- added a commit that references this issue
on Apr 15, 2016 - added a commit that references this issue
on Apr 18, 2016 - added 2 commits that reference this issue
on Apr 19, 2016 1 remaining item
- added a commit that references this issue
on Apr 25, 2016 - added a commit that references this issue
on Apr 26, 2016 - added 2 commits that reference this issue
on May 3, 2016 - added a commit that references this issue
on May 18, 2016 There's still a problem with it, if the second argument is not callable then it will always throw "Got unwanted exception.." instead of the actual exception, which contradicts the documentation which clearly states if the second argument is falsy it will throw the original exception. I currently resolve the bug with;
assert.doesNotThrow(() => somethingThatThrows(), () => null)Documentation:
If an error is thrown and it is the same type as that specified by the error parameter, then an AssertionError is thrown. If the error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
// tested with 7.10.0
Reacted by Erwin GaitanThis is fixed in current master. Closing. (Comment or re-open or open a new issue if I'm wrong.)
nodejs/node-v0.x-archive#6470 is an old PR that never landed but points to a valid issue.
The test case:
Prints the error stack but does not output an AssertionError.
However,
Raises an Assertion Error (
AssertionError: Got unwanted exception (Error)..And
Both just output
custom messagewithout raising the Assertion Error.The solution submitted in nodejs/node-v0.x-archive#6470 should be investigated as a possible fix but possibly needs to be revisited.