-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: ensure .rejects() disallows sync throws #19650
assert: ensure .rejects() disallows sync throws #19650
Conversation
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.
LGTM
test/parallel/test-assert-async.js
Outdated
common.mustNotCall(), | ||
common.mustCall((err) => { | ||
assert.strictEqual(err, THROWN_ERROR); | ||
}) |
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.
Suggestion: use catch(common.mustCall(err) => { assert.strictEqual(err, THROWN_ERROR); })
test/parallel/test-assert-async.js
Outdated
common.expectsError({ | ||
code: 'ERR_ASSERTION', | ||
type: assert.AssertionError, | ||
message: 'Failed' | ||
}) | ||
); | ||
).then(common.mustCall()); |
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.
Nit: this test should now be redundant if I am not mistaken.
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
test/parallel/test-assert-async.js
Outdated
@@ -29,7 +29,7 @@ assert.doesNotReject(() => {}); | |||
type: assert.AssertionError, | |||
message: 'Failed' | |||
})); | |||
assert.doesNotReject(() => promise); | |||
assert.doesNotReject(() => promise).then(common.mustCall()); |
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.
Nit: use common.crashOnUnhandledRejection()
at the top of the file instead of adding then(common.mustCall())
everywhere.
@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 1
await assert.rejects(something);
}
{
// test 2
await assert.rejects(somethingElse);
}
})().then(common.mustCall()); We should maybe add something like |
@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.
const prom = new Promise(() => {});
(async () => {
await prom;
console.log('Never printed');
})(); The process does not stay alive since promise states are not part of the event loop. |
3749412
to
edeb64c
Compare
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
75d2d1e
Compare
@TimothyGu thanks for the heads up! @not-an-aardvark to have feature parity, it might make sense to check in |
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. |
In favor of changing it: existing assertion libraries that I know (Should.js, Chai as Promised, jest) work directly with promises. |
Arguably, this is different from On the other hand, one potential argument for the current |
@BridgeAR @targos What would you recommend doing with this PR? If the behavior of |
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. |
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. |
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesThis PR has two commits; the first commit fixes an issue with the
assert
tests, 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.