@@ -105,11 +105,11 @@ top level test with two subtests.
105105
106106``` js
107107test (' top level test' , async (t ) => {
108- t .test (' subtest 1' , (t ) => {
108+ await t .test (' subtest 1' , (t ) => {
109109 assert .strictEqual (1 , 1 );
110110 });
111111
112- t .test (' subtest 2' , (t ) => {
112+ await t .test (' subtest 2' , (t ) => {
113113 assert .strictEqual (2 , 2 );
114114 });
115115});
@@ -118,7 +118,12 @@ test('top level test', async (t) => {
118118> ** Note:** ` beforeEach ` and ` afterEach ` hooks are triggered
119119> between each subtest execution.
120120
121- Any subtest failures cause the parent test to fail.
121+ In this example, ` await ` is used to ensure that both subtests have completed.
122+ This is necessary because tests do not wait for their subtests to
123+ complete, unlike tests created within suites.
124+ Any subtests that are still outstanding when their parent finishes
125+ are cancelled and treated as failures. Any subtest failures cause the parent
126+ test to fail.
122127
123128## Skipping tests
124129
@@ -236,20 +241,20 @@ that are not executed are omitted from the test runner output.
236241// The suite's 'only' option is set, so these tests are run.
237242test (' this test is run' , { only: true }, async (t ) => {
238243 // Within this test, all subtests are run by default.
239- t .test (' running subtest' );
244+ await t .test (' running subtest' );
240245
241246 // The test context can be updated to run subtests with the 'only' option.
242247 t .runOnly (true );
243- t .test (' this subtest is now skipped' );
244- t .test (' this subtest is run' , { only: true });
248+ await t .test (' this subtest is now skipped' );
249+ await t .test (' this subtest is run' , { only: true });
245250
246251 // Switch the context back to execute all tests.
247252 t .runOnly (false );
248- t .test (' this subtest is now run' );
253+ await t .test (' this subtest is now run' );
249254
250255 // Explicitly do not run these tests.
251- t .test (' skipped subtest 3' , { only: false });
252- t .test (' skipped subtest 4' , { skip: true });
256+ await t .test (' skipped subtest 3' , { only: false });
257+ await t .test (' skipped subtest 4' , { skip: true });
253258});
254259
255260// The 'only' option is not set, so this test is skipped.
@@ -304,13 +309,13 @@ multiple times (e.g. `--test-name-pattern="test 1"`,
304309
305310``` js
306311test (' test 1' , async (t ) => {
307- t .test (' test 2' );
308- t .test (' test 3' );
312+ await t .test (' test 2' );
313+ await t .test (' test 3' );
309314});
310315
311316test (' Test 4' , async (t ) => {
312- t .test (' Test 5' );
313- t .test (' test 6' );
317+ await t .test (' Test 5' );
318+ await t .test (' test 6' );
314319});
315320```
316321
@@ -3388,9 +3393,12 @@ before each subtest of the current test.
33883393``` js
33893394test (' top level test' , async (t ) => {
33903395 t .beforeEach ((t ) => t .diagnostic (` about to run ${ t .name } ` ));
3391- t .test (' This is a subtest' , (t ) => {
3392- assert .ok (' some relevant assertion here' );
3393- });
3396+ await t .test (
3397+ ' This is a subtest' ,
3398+ (t ) => {
3399+ assert .ok (' some relevant assertion here' );
3400+ },
3401+ );
33943402});
33953403```
33963404
@@ -3448,9 +3456,12 @@ after each subtest of the current test.
34483456``` js
34493457test (' top level test' , async (t ) => {
34503458 t .afterEach ((t ) => t .diagnostic (` finished running ${ t .name } ` ));
3451- t .test (' This is a subtest' , (t ) => {
3452- assert .ok (' some relevant assertion here' );
3453- });
3459+ await t .test (
3460+ ' This is a subtest' ,
3461+ (t ) => {
3462+ assert .ok (' some relevant assertion here' );
3463+ },
3464+ );
34543465});
34553466```
34563467
@@ -3702,8 +3713,10 @@ no-op.
37023713test (' top level test' , (t ) => {
37033714 // The test context can be set to run subtests with the 'only' option.
37043715 t .runOnly (true );
3705- t .test (' this subtest is now skipped' );
3706- t .test (' this subtest is run' , { only: true });
3716+ return Promise .all ([
3717+ t .test (' this subtest is now skipped' ),
3718+ t .test (' this subtest is run' , { only: true }),
3719+ ]);
37073720});
37083721```
37093722
@@ -3775,10 +3788,6 @@ added:
37753788 - v18.0.0
37763789 - v16.17.0
37773790changes:
3778- - version:
3779- - v24.0.0
3780- pr-url: https://github.com/nodejs/node/pull/56664
3781- description: This function no longer returns a `Promise`.
37823791 - version:
37833792 - v18.8.0
37843793 - v16.18.0
@@ -3823,13 +3832,14 @@ changes:
38233832 to this function is a [ ` TestContext ` ] [ ] object. If the test uses callbacks,
38243833 the callback function is passed as the second argument. ** Default:** A no-op
38253834 function.
3835+ * Returns: {Promise} Fulfilled with ` undefined ` once the test completes.
38263836
38273837This function is used to create subtests under the current test. This function
38283838behaves in the same fashion as the top level [ ` test() ` ] [ ] function.
38293839
38303840``` js
38313841test (' top level test' , async (t ) => {
3832- t .test (
3842+ await t .test (
38333843 ' This is a subtest' ,
38343844 { only: false , skip: false , concurrency: 1 , todo: false , plan: 1 },
38353845 (t ) => {
0 commit comments