Skip to content
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

doc: add guides on writing tests involving promises #20988

Closed

Conversation

joyeecheung
Copy link
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-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label May 27, 2018
@vsemozhetbyt vsemozhetbyt added test Issues and PRs related to the tests. promises Issues and PRs related to ECMAScript promises. labels May 27, 2018
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt left a comment

Choose a reason for hiding this comment

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

With nits)


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
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()?

const fs = require('fs').promises;

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

Choose a reason for hiding this comment

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

test -> test.?

// will fail the test
common.crashOnUnhandledRejection();

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

Choose a reason for hiding this comment

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

Ditto, period)

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

Choose a reason for hiding this comment

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

Extra empty line.

fs.readFile('test-file').then(
common.mustCall(
(content) => assert.strictEqual(content.toString(), 'test')
));
Copy link
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.

Copy link
Member Author

@joyeecheung joyeecheung May 28, 2018

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

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.

Copy link
Member Author

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
Member Author

@BridgeAR BridgeAR 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
Member

Landed in df16d20

@apapirovski apapirovski closed this Jun 1, 2018
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 <[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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. doc Issues and PRs related to the documentations. promises Issues and PRs related to ECMAScript promises. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants