@@ -1472,11 +1472,6 @@ run({ files: [path.resolve('./tests/test.js')] })
1472
1472
added:
1473
1473
- v22.0.0
1474
1474
- v20.13.0
1475
- changes:
1476
- - version:
1477
- - v24.0.0
1478
- pr-url: https://github.com/nodejs/node/pull/56664
1479
- description: This function no longer returns a `Promise`.
1480
1475
-->
1481
1476
1482
1477
* ` name ` {string} The name of the suite, which is displayed when reporting test
@@ -1487,6 +1482,7 @@ changes:
1487
1482
* ` fn ` {Function|AsyncFunction} The suite function declaring nested tests and
1488
1483
suites. The first argument to this function is a [ ` SuiteContext ` ] [ ] object.
1489
1484
** Default:** A no-op function.
1485
+ * Returns: {Promise} Immediately fulfilled with ` undefined ` .
1490
1486
1491
1487
The ` suite() ` function is imported from the ` node:test ` module.
1492
1488
@@ -1530,10 +1526,6 @@ added:
1530
1526
- v18.0.0
1531
1527
- v16.17.0
1532
1528
changes:
1533
- - version:
1534
- - v24.0.0
1535
- pr-url: https://github.com/nodejs/node/pull/56664
1536
- description: This function no longer returns a `Promise`.
1537
1529
- version:
1538
1530
- v20.2.0
1539
1531
- v18.17.0
@@ -1583,6 +1575,8 @@ changes:
1583
1575
to this function is a [ ` TestContext ` ] [ ] object. If the test uses callbacks,
1584
1576
the callback function is passed as the second argument. ** Default:** A no-op
1585
1577
function.
1578
+ * Returns: {Promise} Fulfilled with ` undefined ` once
1579
+ the test completes, or immediately if the test runs within a suite.
1586
1580
1587
1581
The ` test() ` function is the value imported from the ` test ` module. Each
1588
1582
invocation of this function results in reporting the test to the {TestsStream}.
@@ -1591,6 +1585,26 @@ The `TestContext` object passed to the `fn` argument can be used to perform
1591
1585
actions related to the current test. Examples include skipping the test, adding
1592
1586
additional diagnostic information, or creating subtests.
1593
1587
1588
+ ` test() ` returns a ` Promise ` that fulfills once the test completes.
1589
+ if ` test() ` is called within a suite, it fulfills immediately.
1590
+ The return value can usually be discarded for top level tests.
1591
+ However, the return value from subtests should be used to prevent the parent
1592
+ test from finishing first and cancelling the subtest
1593
+ as shown in the following example.
1594
+
1595
+ ``` js
1596
+ test (' top level test' , async (t ) => {
1597
+ // The setTimeout() in the following subtest would cause it to outlive its
1598
+ // parent test if 'await' is removed on the next line. Once the parent test
1599
+ // completes, it will cancel any outstanding subtests.
1600
+ await t .test (' longer running subtest' , async (t ) => {
1601
+ return new Promise ((resolve , reject ) => {
1602
+ setTimeout (resolve, 1000 );
1603
+ });
1604
+ });
1605
+ });
1606
+ ```
1607
+
1594
1608
The ` timeout ` option can be used to fail the test if it takes longer than
1595
1609
` timeout ` milliseconds to complete. However, it is not a reliable mechanism for
1596
1610
canceling tests because a running test might block the application thread and
0 commit comments