@@ -223,6 +223,37 @@ countdown.dec();
223
223
countdown .dec (); // The countdown callback will be invoked now.
224
224
```
225
225
226
+ #### Testing promises
227
+
228
+ When writing tests involving promises, either make sure that the
229
+ ` onFulfilled ` or the ` onRejected ` handler is wrapped in
230
+ ` common.mustCall() ` or ` common.mustNotCall() ` accordingly, or
231
+ call ` common.crashOnUnhandledRejection() ` in the top level of the
232
+ test to make sure that unhandled rejections would result in a test
233
+ failure. For example:
234
+
235
+ ``` javascript
236
+ const common = require (' ../common' );
237
+ const assert = require (' assert' );
238
+ const fs = require (' fs' ).promises ;
239
+
240
+ // Use `common.crashOnUnhandledRejection()` to make sure unhandled rejections
241
+ // will fail the test.
242
+ common .crashOnUnhandledRejection ();
243
+
244
+ // Or, wrap the `onRejected` handler in `common.mustNotCall()`.
245
+ fs .writeFile (' test-file' , ' test' ).catch (common .mustNotCall ());
246
+
247
+ // Or, wrap the `onFulfilled` handler in `common.mustCall()`.
248
+ // If there are assertions in the `onFulfilled` handler, wrap
249
+ // the next `onRejected` handler in `common.mustNotCall()`
250
+ // to handle potential failures.
251
+ fs .readFile (' test-file' ).then (
252
+ common .mustCall (
253
+ (content ) => assert .strictEqual (content .toString (), ' test2' )
254
+ ))
255
+ .catch (common .mustNotCall ());
256
+ ```
226
257
227
258
### Flags
228
259
0 commit comments