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 — doc: add guides on writing tests involving promises by joyeecheung · Pull Request #20988 · nodejs/node

Conversation

@joyeecheung

@joyeecheung joyeecheung commented May 27, 2018

Copy link
Copy Markdown
Member

Mention common.crashOnUnhandledRejection() and wrapping the
handlers in common.mustCall() or common.mustNotCall()

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • documentation is changed or added
  • commit message follows commit guidelines

Sorry, something went wrong.

Uh oh!

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

All reactions
@joyeecheung
doc: add guides on writing tests involving promises
61354c9
Mention `common.crashOnUnhandledRejection()` and wrapping the
handlers in `common.mustCall()` or `common.mustNotCall()`
@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to Node.js documentation. label May 27, 2018
@vsemozhetbyt vsemozhetbyt added test Issues and PRs related to Node.js core tests and test infrastructure. promises Issues and PRs related to ECMAScript promises. labels May 27, 2018
vsemozhetbyt
vsemozhetbyt approved these changes May 27, 2018
View reviewed changes

@vsemozhetbyt vsemozhetbyt left a comment

Copy link
Copy Markdown
Contributor

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.

With nits)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread doc/guides/writing-tests.md Outdated

When writing tests involving promises, either make sure that the
`onFulfilled` or the `onRejected` handler is wrapped in
`common.mustCall()` or `common.mustNotCall` accordingly, or

@vsemozhetbyt vsemozhetbyt May 27, 2018

Copy link
Copy Markdown
Contributor

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.

common.mustNotCall -> common.mustNotCall()?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread doc/guides/writing-tests.md Outdated
const fs = require('fs').promises;

// Use `common.crashOnUnhandledRejection()` to make sure unhandled rejections
// will fail the test

@vsemozhetbyt vsemozhetbyt May 27, 2018

Copy link
Copy Markdown
Contributor

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.

test -> test.?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread doc/guides/writing-tests.md Outdated
// will fail the test
common.crashOnUnhandledRejection();

// Or, wrap the `onRejected` handler in `common.mustNotCall()`

@vsemozhetbyt vsemozhetbyt May 27, 2018

Copy link
Copy Markdown
Contributor

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.

Ditto, period)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread doc/guides/writing-tests.md Outdated
// Or, wrap the `onRejected` handler in `common.mustNotCall()`
fs.writeFile('test-file', 'test').catch(common.mustNotCall());

// Or, wrap the `onFulfilled` handler in `common.mustCall()`

@vsemozhetbyt vsemozhetbyt May 27, 2018

Copy link
Copy Markdown
Contributor

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.

Ditto, period)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread doc/guides/writing-tests.md
(content) => assert.strictEqual(content.toString(), 'test')
));
```

@vsemozhetbyt vsemozhetbyt May 27, 2018

Copy link
Copy Markdown
Contributor

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.

Extra empty line.

Sorry, something went wrong.

Uh oh!

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

All reactions
lpinca
lpinca reviewed May 27, 2018
View reviewed changes
Comment thread doc/guides/writing-tests.md Outdated
fs.readFile('test-file').then(
common.mustCall(
(content) => assert.strictEqual(content.toString(), 'test')
));

@lpinca lpinca May 27, 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.

It's an example, but I think it makes sense to add catch() to handle the case where the assertion fails.

Sorry, something went wrong.

Uh oh!

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

All reactions

@joyeecheung joyeecheung May 28, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member 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.

@lpinca that is demonstrated in the first example...also the onFullfilled and the onRejected handler are mutually exclusive so common.mustCall should be able to catch that.

Sorry, something went wrong.

Uh oh!

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

All reactions

@lpinca lpinca May 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.

I'm not sure I understand, this

const common = require('../common');
const assert = require('assert');
const fs = require('fs');

const fsPromises = fs.promises;

fs.writeFileSync('test-file', 'foo');
fsPromises.readFile('test-file').then(
  common.mustCall((content) => {
    assert.strictEqual(content.toString(), 'test');
  })
);

should make the test fails, but it doesn't if common.crashOnUnhandledRejection(); is not added.

Sorry, something went wrong.

Uh oh!

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

All reactions

@joyeecheung joyeecheung May 28, 2018

Copy link
Copy Markdown
Member 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.

@lpinca I see what you mean now, the catch needs to handle possible failures if the onFulfilled handler is not empty. Thanks for catching that!

Sorry, something went wrong.

Uh oh!

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

All reactions
danbev
danbev approved these changes May 28, 2018
View reviewed changes
joyeecheung added 2 commits May 28, 2018 14:01
@joyeecheung
fixup! doc: add guides on writing tests involving promises
a284df4
@joyeecheung
fixup! doc: add guides on writing tests involving promises
59fd141
@joyeecheung

joyeecheung commented May 28, 2018

Copy link
Copy Markdown
Member Author

Lite CI: https://ci.nodejs.org/job/node-test-pull-request-lite/760/

All reactions

Sorry, something went wrong.

Uh oh!

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

lpinca
lpinca approved these changes May 28, 2018
View reviewed changes
BridgeAR
BridgeAR approved these changes May 28, 2018
View reviewed changes
@BridgeAR BridgeAR added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label May 28, 2018
trivikr
trivikr approved these changes May 28, 2018
View reviewed changes
jasnell
jasnell approved these changes May 29, 2018
View reviewed changes
@apapirovski

apapirovski commented Jun 1, 2018

Copy link
Copy Markdown
Contributor

Landed in df16d20

All reactions

Sorry, something went wrong.

Uh oh!

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

@apapirovski apapirovski closed this Jun 1, 2018
apapirovski pushed a commit that referenced this pull request Jun 1, 2018
@joyeecheung @apapirovski
doc: add guides on writing tests involving promises
df16d20
Mention `common.crashOnUnhandledRejection()` and wrapping the
handlers in `common.mustCall()` or `common.mustNotCall()`.

PR-URL: #20988
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jun 6, 2018
@joyeecheung @MylesBorins
doc: add guides on writing tests involving promises
fd201e0
Mention `common.crashOnUnhandledRejection()` and wrapping the
handlers in `common.mustCall()` or `common.mustNotCall()`.

PR-URL: #20988
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Jun 6, 2018
v10.4.0 proposal #21167
Merged
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
@lpinca lpinca lpinca approved these changes
@BridgeAR BridgeAR BridgeAR approved these changes
@trivikr trivikr trivikr approved these changes
+2 more reviewers
@danbev danbev danbev approved these changes
@vsemozhetbyt vsemozhetbyt vsemozhetbyt approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. doc Issues and PRs related to Node.js documentation. promises Issues and PRs related to ECMAScript promises. test Issues and PRs related to Node.js core tests and test infrastructure.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

9 participants

@joyeecheung @apapirovski @danbev @jasnell @lpinca @BridgeAR @vsemozhetbyt @trivikr @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.