Uh oh!
There was an error while loading. Please reload this page.
assert: ensure .rejects() disallows sync throws - #19650
Conversation
There was a problem hiding this comment.
Suggestion: use catch(common.mustCall(err) => { assert.strictEqual(err, THROWN_ERROR); })
There was a problem hiding this comment.
Nit: this test should now be redundant if I am not mistaken.
There was a problem hiding this comment.
I'm assuming you mean that this test is redundant with the other test here. However, this test is a bit different from the other one because it throws asynchronously, and so the promise returned by assert.rejects fulfills (whereas in the other test, the function throws synchronously and the promise returned by assert.rejects rejects and needs to be caught separately).
There was a problem hiding this comment.
Actually, I just realized what you meant: this test is now redundant with this other test. I agree, I'll remove the other one.
edit: Removed.
There was a problem hiding this comment.
Nit: use common.crashOnUnhandledRejection() at the top of the file instead of adding then(common.mustCall()) everywhere.
targos
commented
Mar 28, 2018
@BridgeAR about It is necessary to make sure the promise eventually fulfills (does not stay forever in a pending state). I think the best would be to combine common.crashOnUnhandledRejection();(async()=>{{// test 1awaitassert.rejects(something);}{// test 2awaitassert.rejects(somethingElse);}})().then(common.mustCall());We should maybe add something like |
BridgeAR
commented
Mar 28, 2018
@targos fair point. But should the test not time out in case the promise does not fulfill? |
@BridgeAR Here's a case where the // Never fulfills.constprom=newPromise(()=>{});(async()=>{awaitprom;console.log('Never printed');})();The process does not stay alive since promise states are not part of the event loop. |
3749412 to
edeb64cComparenot-an-aardvark
commented
Mar 28, 2018
Updated to follow the suggestions in #19650 (comment) and #19650 (comment). |
This updates the test in `test/parallel/test-assert-async.js` to add an assertion that the Promises used in the test end up fulfilled. Previously, if an assertion failure occurred, the Promises would have rejected and a warning would have been logged, but the test would still have exit code 0.
This updates `assert.rejects()` to disallow any errors that are thrown synchronously from the given function. Previously, throwing an error would cause the same behavior as returning a rejected Promise. Fixes: nodejs#19646
edeb64c to
75d2d1eComparenot-an-aardvark
commented
Mar 28, 2018
BridgeAR
commented
Mar 29, 2018
@TimothyGu thanks for the heads up! @not-an-aardvark to have feature parity, it might make sense to check in |
targos
commented
Mar 29, 2018
Just thinking about it now: wouldn't it be better if assert.rejects expected a Promise instead of a function? |
@targos I agree. That solves that dilemma. Since we did not yet publish the functionality, we can still change it. [update] Thinking about it again: It might still be better to only handle functions:
I am going to give it another thought when I have some more time later on. |
targos
commented
Mar 29, 2018
In favor of changing it: existing assertion libraries that I know (Should.js, Chai as Promised, jest) work directly with promises. |
not-an-aardvark
commented
Mar 29, 2018
Arguably, this is different from On the other hand, one potential argument for the current |
not-an-aardvark
commented
Apr 3, 2018
@BridgeAR@targos What would you recommend doing with this PR? If the behavior of |
BridgeAR
commented
Apr 3, 2018
I am actually considering supporting to accept both: passing in a function that returns a promise or directly passing in a promise. That would ease the user experience. I do not really see a downside accepting both. |
not-an-aardvark
commented
Apr 4, 2018
I'm going to land this since we seem to be in agreement that it's an improvement over the current behavior, but I'd be fine with the behavior being changed in a future PR. |
not-an-aardvark
commented
Apr 4, 2018
This updates the test in `test/parallel/test-assert-async.js` to add an assertion that the Promises used in the test end up fulfilled. Previously, if an assertion failure occurred, the Promises would have rejected and a warning would have been logged, but the test would still have exit code 0. PR-URL: #19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates `assert.rejects()` to disallow any errors that are thrown synchronously from the given function. Previously, throwing an error would cause the same behavior as returning a rejected Promise. Fixes: #19646 PR-URL: #19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates the test in `test/parallel/test-assert-async.js` to add an assertion that the Promises used in the test end up fulfilled. Previously, if an assertion failure occurred, the Promises would have rejected and a warning would have been logged, but the test would still have exit code 0. PR-URL: nodejs#19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates `assert.rejects()` to disallow any errors that are thrown synchronously from the given function. Previously, throwing an error would cause the same behavior as returning a rejected Promise. Fixes: nodejs#19646 PR-URL: nodejs#19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates the test in `test/parallel/test-assert-async.js` to add an assertion that the Promises used in the test end up fulfilled. Previously, if an assertion failure occurred, the Promises would have rejected and a warning would have been logged, but the test would still have exit code 0. PR-URL: nodejs#19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates `assert.rejects()` to disallow any errors that are thrown synchronously from the given function. Previously, throwing an error would cause the same behavior as returning a rejected Promise. Fixes: nodejs#19646 PR-URL: nodejs#19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates the test in `test/parallel/test-assert-async.js` to add an assertion that the Promises used in the test end up fulfilled. Previously, if an assertion failure occurred, the Promises would have rejected and a warning would have been logged, but the test would still have exit code 0. Backport-PR-URL: #24019 PR-URL: #19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This updates `assert.rejects()` to disallow any errors that are thrown synchronously from the given function. Previously, throwing an error would cause the same behavior as returning a rejected Promise. Fixes: #19646 Backport-PR-URL: #24019 PR-URL: #19650 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesThis PR has two commits; the first commit fixes an issue with the
asserttests, and the second commit fixes#19646.First commit:
Second commit:
Note that the second commit would be semver-major, but it modifies an API (introduced in #18023) that has not been backported to a release branch yet. As a result, the second commit could be backported as long as it's applied at the same time as #18023.