Skip to content

doc: add guides on writing tests involving promises - #20988

Closed
joyeecheung wants to merge 3 commits into
nodejs:masterfrom
joyeecheung:unhandled-rejection-test
Closed

doc: add guides on writing tests involving promises#20988
joyeecheung wants to merge 3 commits into
nodejs:masterfrom
joyeecheung:unhandled-rejection-test

Conversation

@joyeecheung

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

Mention `common.crashOnUnhandledRejection()` and wrapping the
handlers in `common.mustCall()` or `common.mustNotCall()`
@nodejs-github-botnodejs-github-bot added the doc Issues and PRs related to the documentations. label May 27, 2018
@vsemozhetbytvsemozhetbyt added test Issues and PRs related to the tests. promises Issues and PRs related to ECMAScript promises. labels May 27, 2018

@vsemozhetbytvsemozhetbyt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With nits)

Comment threaddoc/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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.mustNotCall -> common.mustNotCall()?

Comment threaddoc/guides/writing-tests.md Outdated
const fs = require('fs').promises;

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test -> test.?

Comment threaddoc/guides/writing-tests.md Outdated
// will fail the test
common.crashOnUnhandledRejection();

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, period)

Comment threaddoc/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()`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, period)

(content) => assert.strictEqual(content.toString(), 'test')
));
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line.

Comment threaddoc/guides/writing-tests.md Outdated
fs.readFile('test-file').then(
common.mustCall(
(content) => assert.strictEqual(content.toString(), 'test')
));

Copy link
Copy Markdown
Member

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.

@joyeecheungjoyeecheungMay 28, 2018

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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

constcommon=require('../common');constassert=require('assert');constfs=require('fs');constfsPromises=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.

Copy link
Copy Markdown
MemberAuthor

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!

@joyeecheung

Copy link
Copy Markdown
MemberAuthor

@BridgeARBridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 28, 2018
@apapirovski

Copy link
Copy Markdown
Contributor

Landed in df16d20

apapirovski pushed a commit that referenced this pull request Jun 1, 2018
Mention `common.crashOnUnhandledRejection()` and wrapping the
handlers in `common.mustCall()` or `common.mustNotCall()`.
PR-URL: #20988
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Jun 6, 2018
Mention `common.crashOnUnhandledRejection()` and wrapping the
handlers in `common.mustCall()` or `common.mustNotCall()`.
PR-URL: #20988
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@MylesBorinsMylesBorins mentioned this pull request Jun 6, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.docIssues and PRs related to the documentations.promisesIssues and PRs related to ECMAScript promises.testIssues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@joyeecheung@apapirovski@danbev@jasnell@lpinca@BridgeAR@vsemozhetbyt@trivikr@nodejs-github-bot