Skip to content

Commit 2b34ffe

Browse files
committed
test_runner: remove promises returned by t.test()
This commit updates the TestContext.prototype.test() API to no longer return a Promise. Fixes: #51292
1 parent e35af67 commit 2b34ffe

11 files changed

+36
-76
lines changed

doc/api/test.md

+26-36
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ top level test with two subtests.
105105

106106
```js
107107
test('top level test', async (t) => {
108-
await t.test('subtest 1', (t) => {
108+
t.test('subtest 1', (t) => {
109109
assert.strictEqual(1, 1);
110110
});
111111

112-
await t.test('subtest 2', (t) => {
112+
t.test('subtest 2', (t) => {
113113
assert.strictEqual(2, 2);
114114
});
115115
});
@@ -118,12 +118,7 @@ test('top level test', async (t) => {
118118
> **Note:** `beforeEach` and `afterEach` hooks are triggered
119119
> between each subtest execution.
120120
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.
121+
Any subtest failures cause the parent test to fail.
127122

128123
## Skipping tests
129124

@@ -241,20 +236,20 @@ that are not executed are omitted from the test runner output.
241236
// The suite's 'only' option is set, so these tests are run.
242237
test('this test is run', { only: true }, async (t) => {
243238
// Within this test, all subtests are run by default.
244-
await t.test('running subtest');
239+
t.test('running subtest');
245240

246241
// The test context can be updated to run subtests with the 'only' option.
247242
t.runOnly(true);
248-
await t.test('this subtest is now skipped');
249-
await t.test('this subtest is run', { only: true });
243+
t.test('this subtest is now skipped');
244+
t.test('this subtest is run', { only: true });
250245

251246
// Switch the context back to execute all tests.
252247
t.runOnly(false);
253-
await t.test('this subtest is now run');
248+
t.test('this subtest is now run');
254249

255250
// Explicitly do not run these tests.
256-
await t.test('skipped subtest 3', { only: false });
257-
await t.test('skipped subtest 4', { skip: true });
251+
t.test('skipped subtest 3', { only: false });
252+
t.test('skipped subtest 4', { skip: true });
258253
});
259254

260255
// The 'only' option is not set, so this test is skipped.
@@ -309,13 +304,13 @@ multiple times (e.g. `--test-name-pattern="test 1"`,
309304

310305
```js
311306
test('test 1', async (t) => {
312-
await t.test('test 2');
313-
await t.test('test 3');
307+
t.test('test 2');
308+
t.test('test 3');
314309
});
315310

316311
test('Test 4', async (t) => {
317-
await t.test('Test 5');
318-
await t.test('test 6');
312+
t.test('Test 5');
313+
t.test('test 6');
319314
});
320315
```
321316

@@ -3175,12 +3170,9 @@ before each subtest of the current test.
31753170
```js
31763171
test('top level test', async (t) => {
31773172
t.beforeEach((t) => t.diagnostic(`about to run ${t.name}`));
3178-
await t.test(
3179-
'This is a subtest',
3180-
(t) => {
3181-
assert.ok('some relevant assertion here');
3182-
},
3183-
);
3173+
t.test('This is a subtest', (t) => {
3174+
assert.ok('some relevant assertion here');
3175+
});
31843176
});
31853177
```
31863178

@@ -3238,12 +3230,9 @@ after each subtest of the current test.
32383230
```js
32393231
test('top level test', async (t) => {
32403232
t.afterEach((t) => t.diagnostic(`finished running ${t.name}`));
3241-
await t.test(
3242-
'This is a subtest',
3243-
(t) => {
3244-
assert.ok('some relevant assertion here');
3245-
},
3246-
);
3233+
t.test('This is a subtest', (t) => {
3234+
assert.ok('some relevant assertion here');
3235+
});
32473236
});
32483237
```
32493238

@@ -3458,10 +3447,8 @@ no-op.
34583447
test('top level test', (t) => {
34593448
// The test context can be set to run subtests with the 'only' option.
34603449
t.runOnly(true);
3461-
return Promise.all([
3462-
t.test('this subtest is now skipped'),
3463-
t.test('this subtest is run', { only: true }),
3464-
]);
3450+
t.test('this subtest is now skipped');
3451+
t.test('this subtest is run', { only: true });
34653452
});
34663453
```
34673454

@@ -3533,6 +3520,10 @@ added:
35333520
- v18.0.0
35343521
- v16.17.0
35353522
changes:
3523+
- version:
3524+
- REPLACEME
3525+
pr-url: https://github.com/nodejs/node/pull/56664
3526+
description: This function no longer returns a `Promise`.
35363527
- version:
35373528
- v18.8.0
35383529
- v16.18.0
@@ -3577,14 +3568,13 @@ changes:
35773568
to this function is a [`TestContext`][] object. If the test uses callbacks,
35783569
the callback function is passed as the second argument. **Default:** A no-op
35793570
function.
3580-
* Returns: {Promise} Fulfilled with `undefined` once the test completes.
35813571

35823572
This function is used to create subtests under the current test. This function
35833573
behaves in the same fashion as the top level [`test()`][] function.
35843574

35853575
```js
35863576
test('top level test', async (t) => {
3587-
await t.test(
3577+
t.test(
35883578
'This is a subtest',
35893579
{ only: false, skip: false, concurrency: 1, todo: false, plan: 1 },
35903580
(t) => {

lib/internal/test_runner/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class TestContext {
304304
Test, name, options, fn, overrides,
305305
);
306306

307-
return subtest.start();
307+
subtest.start();
308308
}
309309

310310
before(fn, options) {

test/fixtures/test-runner/output/dot_reporter.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ Failed tests:
154154
*
155155
*
156156
*
157-
*
158-
*
159157
✖ subtest sync throw fails (*ms)
160158
'2 subtests failed'
161159
✖ timed out async test (*ms)

test/fixtures/test-runner/output/hooks.snapshot

-5
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ not ok 16 - t.after throws - no subtests
576576
*
577577
*
578578
*
579-
*
580579
...
581580
1..2
582581
not ok 17 - t.beforeEach throws
@@ -607,8 +606,6 @@ not ok 17 - t.beforeEach throws
607606
*
608607
*
609608
*
610-
*
611-
*
612609
...
613610
# Subtest: 2
614611
not ok 2 - 2
@@ -629,7 +626,6 @@ not ok 17 - t.beforeEach throws
629626
*
630627
*
631628
*
632-
*
633629
...
634630
1..2
635631
not ok 18 - t.afterEach throws
@@ -757,7 +753,6 @@ not ok 21 - afterEach context when test fails
757753
*
758754
*
759755
*
760-
*
761756
...
762757
1..2
763758
not ok 22 - afterEach throws and test fails

test/fixtures/test-runner/output/hooks_spec_reporter.snapshot

-10
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@
282282
*
283283
*
284284
*
285-
*
286285

287286
t.beforeEach throws (*ms)
288287
t.afterEach throws
@@ -296,8 +295,6 @@
296295
*
297296
*
298297
*
299-
*
300-
*
301298

302299
2 (*ms)
303300
Error: afterEach
@@ -310,7 +307,6 @@
310307
*
311308
*
312309
*
313-
*
314310

315311
t.afterEach throws (*ms)
316312
afterEach when test fails
@@ -364,7 +360,6 @@
364360
*
365361
*
366362
*
367-
*
368363

369364
afterEach throws and test fails (*ms)
370365
t.after() is called if test body throws (*ms)
@@ -674,7 +669,6 @@
674669
*
675670
*
676671
*
677-
*
678672

679673
*
680674
1 (*ms)
@@ -687,8 +681,6 @@
687681
*
688682
*
689683
*
690-
*
691-
*
692684

693685
*
694686
2 (*ms)
@@ -702,7 +694,6 @@
702694
*
703695
*
704696
*
705-
*
706697

707698
*
708699
1 (*ms)
@@ -750,7 +741,6 @@
750741
*
751742
*
752743
*
753-
*
754744

755745
*
756746
t.after() is called if test body throws (*ms)

test/fixtures/test-runner/output/junit_reporter.snapshot

+1-4
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
351351
</testcase>
352352
<testcase name="sync throw fails at second" time="*" classname="test" failure="thrown from subtest sync throw fails at second">
353353
<failure type="testCodeFailure" message="thrown from subtest sync throw fails at second">
354-
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second
355-
* {
354+
[Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second] {
356355
code: 'ERR_TEST_FAILURE',
357356
failureType: 'testCodeFailure',
358357
cause: Error: thrown from subtest sync throw fails at second
@@ -362,8 +361,6 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second
362361
*
363362
*
364363
*
365-
*
366-
*
367364
}
368365
</failure>
369366
</testcase>

test/fixtures/test-runner/output/output.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,6 @@ not ok 51 - custom inspect symbol that throws fail
598598
*
599599
*
600600
*
601-
*
602-
*
603601
...
604602
1..2
605603
not ok 52 - subtest sync throw fails

test/fixtures/test-runner/output/output_cli.snapshot

-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,6 @@ not ok 51 - custom inspect symbol that throws fail
606606
*
607607
*
608608
*
609-
*
610-
*
611609
...
612610
1..2
613611
not ok 52 - subtest sync throw fails

test/fixtures/test-runner/output/spec_reporter.snapshot

-4
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@
216216
*
217217
*
218218
*
219-
*
220-
*
221219

222220
subtest sync throw fails (*ms)
223221
timed out async test (*ms)
@@ -495,8 +493,6 @@
495493
*
496494
*
497495
*
498-
*
499-
*
500496

501497
*
502498
timed out async test (*ms)

test/fixtures/test-runner/output/spec_reporter_cli.snapshot

-4
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@
219219
*
220220
*
221221
*
222-
*
223-
*
224222

225223
subtest sync throw fails (*ms)
226224
timed out async test (*ms)
@@ -498,8 +496,6 @@
498496
*
499497
*
500498
*
501-
*
502-
*
503499

504500
*
505501
timed out async test (*ms)

test/parallel/test-runner-module-mocking.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,15 @@ test('mocks are automatically restored', async (t) => {
448448
assert.strictEqual(mocked.fn(), 43);
449449
});
450450

451-
const cjsMock = require(cjsFixture);
452-
const esmMock = await import(esmFixture);
451+
t.test('checks original behavior', async () => {
452+
const cjsMock = require(cjsFixture);
453+
const esmMock = await import(esmFixture);
453454

454-
assert.strictEqual(cjsMock.string, 'original cjs string');
455-
assert.strictEqual(cjsMock.fn, undefined);
456-
assert.strictEqual(esmMock.string, 'original esm string');
457-
assert.strictEqual(esmMock.fn, undefined);
455+
assert.strictEqual(cjsMock.string, 'original cjs string');
456+
assert.strictEqual(cjsMock.fn, undefined);
457+
assert.strictEqual(esmMock.string, 'original esm string');
458+
assert.strictEqual(esmMock.fn, undefined);
459+
});
458460
});
459461

460462
test('mocks can be restored independently', async (t) => {

0 commit comments

Comments
 (0)