Uh oh!
There was an error while loading. Please reload this page.
doc: add guides on writing tests involving promises - #20988
Conversation
Mention `common.crashOnUnhandledRejection()` and wrapping the handlers in `common.mustCall()` or `common.mustNotCall()`
| 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 |
There was a problem hiding this comment.
common.mustNotCall -> common.mustNotCall()?
| const fs = require('fs').promises; | ||
| // Use `common.crashOnUnhandledRejection()` to make sure unhandled rejections | ||
| // will fail the test |
| // will fail the test | ||
| common.crashOnUnhandledRejection(); | ||
| // Or, wrap the `onRejected` handler in `common.mustNotCall()` |
| // Or, wrap the `onRejected` handler in `common.mustNotCall()` | ||
| fs.writeFile('test-file', 'test').catch(common.mustNotCall()); | ||
| // Or, wrap the `onFulfilled` handler in `common.mustCall()` |
| (content) => assert.strictEqual(content.toString(), 'test') | ||
| )); | ||
| ``` | ||
| fs.readFile('test-file').then( | ||
| common.mustCall( | ||
| (content) => assert.strictEqual(content.toString(), 'test') | ||
| )); |
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
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.
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!
joyeecheung
commented
May 28, 2018
apapirovski
commented
Jun 1, 2018
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