doc: add guides on writing tests involving promises#20988
doc: add guides on writing tests involving promises#20988joyeecheung wants to merge 3 commits intonodejs:masterfrom
Conversation
Mention `common.crashOnUnhandledRejection()` and wrapping the handlers in `common.mustCall()` or `common.mustNotCall()`
doc/guides/writing-tests.md
Outdated
There was a problem hiding this comment.
common.mustNotCall -> common.mustNotCall()?
doc/guides/writing-tests.md
Outdated
doc/guides/writing-tests.md
Outdated
doc/guides/writing-tests.md
Outdated
| (content) => assert.strictEqual(content.toString(), 'test') | ||
| )); | ||
| ``` | ||
|
|
doc/guides/writing-tests.md
Outdated
There was a problem hiding this comment.
It's an example, but I think it makes sense to add catch() to handle the case where the assertion fails.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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!
|
Landed in df16d20 |
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>
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>
Mention
common.crashOnUnhandledRejection()and wrapping thehandlers in
common.mustCall()orcommon.mustNotCall()Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes