GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — assert: ensure .rejects() disallows sync throws by not-an-aardvark · Pull Request #19650 · nodejs/node

Conversation

@not-an-aardvark

@not-an-aardvark not-an-aardvark commented Mar 28, 2018

Copy link
Copy Markdown
Contributor
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

This PR has two commits; the first commit fixes an issue with the assert tests, and the second commit fixes #19646.

First commit:

test: ensure failed assertions cause build to fail

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.

Second commit:

assert: ensure .rejects() disallows sync throws

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: https://github.com/nodejs/node/issues/19646

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot nodejs-github-bot added the assert Issues and PRs related to the assert subsystem. label Mar 28, 2018
BridgeAR
BridgeAR approved these changes Mar 28, 2018
View reviewed changes

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/parallel/test-assert-async.js Outdated

@BridgeAR BridgeAR Mar 28, 2018

Copy link
Copy Markdown
Member

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); })

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/parallel/test-assert-async.js Outdated

@BridgeAR BridgeAR Mar 28, 2018

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@not-an-aardvark not-an-aardvark Mar 28, 2018

Copy link
Copy Markdown
Contributor Author

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).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@not-an-aardvark not-an-aardvark Mar 28, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/parallel/test-assert-async.js Outdated

@BridgeAR BridgeAR Mar 28, 2018

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@targos

targos commented Mar 28, 2018

Copy link
Copy Markdown
Member

@BridgeAR about .then(common.mustCall()):

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() at the top and an async IIFE:

common.crashOnUnhandledRejection();

(async () => {
  {
    // test 1
    await assert.rejects(something);
  }

  {
    // test 2
    await assert.rejects(somethingElse);
  }
})().then(common.mustCall());

We should maybe add something like common.runAsync(async () => {}) that does both?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR

BridgeAR commented Mar 28, 2018

Copy link
Copy Markdown
Member

@targos fair point. But should the test not time out in case the promise does not fulfill?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TimothyGu

TimothyGu commented Mar 28, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@BridgeAR Here's a case where the .then(common.mustCall()) is absolutely necessary:

// 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.

BridgeAR reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jasnell
jasnell approved these changes Mar 28, 2018
View reviewed changes
@not-an-aardvark
not-an-aardvark force-pushed the assert-rejects-sync-error branch from 3749412 to edeb64c Compare March 28, 2018 18:06
@not-an-aardvark

not-an-aardvark commented Mar 28, 2018

Copy link
Copy Markdown
Contributor Author

Updated to follow the suggestions in #19650 (comment) and #19650 (comment).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

not-an-aardvark added 2 commits March 28, 2018 14:15
@not-an-aardvark
test: ensure failed assertions cause build to fail
4774af1
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.
@not-an-aardvark
assert: ensure .rejects() disallows sync throws
75d2d1e
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
@not-an-aardvark
not-an-aardvark force-pushed the assert-rejects-sync-error branch from edeb64c to 75d2d1e Compare March 28, 2018 18:16
@not-an-aardvark

not-an-aardvark commented Mar 28, 2018

Copy link
Copy Markdown
Contributor Author

CI: https://ci.nodejs.org/job/node-test-pull-request/13906/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR

BridgeAR commented Mar 29, 2018

Copy link
Copy Markdown
Member

@TimothyGu thanks for the heads up!

@not-an-aardvark to have feature parity, it might make sense to check in assert.doesNotReject that the passed in function will actually return a promise. From that point on the function has some legitimacy. Right now I do not see any legit use case. What do you think?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented Mar 29, 2018

Copy link
Copy Markdown
Member

Just thinking about it now: wouldn't it be better if assert.rejects expected a Promise instead of a function?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR

BridgeAR commented Mar 29, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@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:

  • We use functions in assert.throws and assert.doesNotThrow
  • It has the benefit to test a specific functionality to really return a promise

I am going to give it another thought when I have some more time later on.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented Mar 29, 2018

Copy link
Copy Markdown
Member

In favor of changing it: existing assertion libraries that I know (Should.js, Chai as Promised, jest) work directly with promises.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@not-an-aardvark

not-an-aardvark commented Mar 29, 2018

Copy link
Copy Markdown
Contributor Author

Arguably, this is different from assert.throws because assert.throws makes an assertion about how a function behaves, whereas assert.rejects only makes an assertion about the function's return value. So it would make sense for it to just accept a Promise rather than a function.

On the other hand, one potential argument for the current assert.rejects behavior is that it could be simpler for users who don't know how Promises work. They might have a mental model of "always use await when calling an async function to avoid weird behavior". With that mental model, assert.rejects(someAsyncFunction) would make sense, but assert.rejects(someAsyncFunction()) would not make sense. They might try to use assert.rejects(await someAsyncFunction()) instead, but this would cause other problems. The current behavior of assert.rejects would allow for a mental model of "this is just like assert.throws, but for async functions."

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@not-an-aardvark

not-an-aardvark commented Apr 3, 2018

Copy link
Copy Markdown
Contributor Author

@BridgeAR @targos What would you recommend doing with this PR? If the behavior of assert.rejects is going to be changed to accept Promises rather than functions, then I'm fine with closing this. However, if we don't do anything, then it seems likely that the current behavior will end up landing in a release, after which it will be much harder to change. (I haven't landed this PR yet because I'm not sure if you wanted to change the API signature instead.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR

BridgeAR commented Apr 3, 2018

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@not-an-aardvark

not-an-aardvark commented Apr 4, 2018

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@not-an-aardvark

not-an-aardvark commented Apr 4, 2018

Copy link
Copy Markdown
Contributor Author

Landed in 8995125 and fdb35d8

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@not-an-aardvark not-an-aardvark closed this Apr 4, 2018
not-an-aardvark added a commit that referenced this pull request Apr 4, 2018
@not-an-aardvark
test: ensure failed assertions cause build to fail
8995125
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]>
not-an-aardvark added a commit that referenced this pull request Apr 4, 2018
@not-an-aardvark
assert: ensure .rejects() disallows sync throws
fdb35d8
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]>
@not-an-aardvark
not-an-aardvark deleted the assert-rejects-sync-error branch April 4, 2018 02:48
@targos targos added the backport-requested-v9.x label Apr 4, 2018
@not-an-aardvark not-an-aardvark mentioned this pull request Apr 4, 2018
assert: adds rejects() and doesNotReject() #18023
Closed
4 tasks done
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
@not-an-aardvark @BridgeAR
test: ensure failed assertions cause build to fail
204b024
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]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
@not-an-aardvark @BridgeAR
assert: ensure .rejects() disallows sync throws
297fcf0
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]>
@MylesBorins MylesBorins added the dont-land-on-v8.x label Aug 7, 2018
@not-an-aardvark not-an-aardvark mentioned this pull request Nov 1, 2018
[v8.x] backport assert: add rejects() and doesNotReject() #24019
Closed
4 tasks
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Nov 2, 2018
@not-an-aardvark @MylesBorins
test: ensure failed assertions cause build to fail
30bfd25
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]>
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Nov 2, 2018
@not-an-aardvark @MylesBorins
assert: ensure .rejects() disallows sync throws
c247052
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]>
@MylesBorins MylesBorins added backported-to-v8.x and removed dont-land-on-v8.x labels Nov 2, 2018
MylesBorins pushed a commit that referenced this pull request Nov 4, 2018
@not-an-aardvark @MylesBorins
test: ensure failed assertions cause build to fail
3cd4462
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]>
MylesBorins pushed a commit that referenced this pull request Nov 4, 2018
@not-an-aardvark @MylesBorins
assert: ensure .rejects() disallows sync throws
0d241ba
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]>
@BethGriggs BethGriggs mentioned this pull request Nov 12, 2018
v8.13.0 proposal #23974
Merged
@beevelop beevelop mentioned this pull request Nov 20, 2018
2018-11-20 Version 8.13.0 'Carbon' (LTS) @MylesBorins prepared by @BethGriggs beevelop/docker-android-nodejs#250
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell approved these changes
@BridgeAR BridgeAR BridgeAR approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

assert Issues and PRs related to the assert subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

assert.rejects passes if the given function throws synchronously

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@not-an-aardvark @targos @BridgeAR @TimothyGu @jasnell @MylesBorins @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.