Skip to content

Commit 391ff0d

Browse files
MoLowjuanarbol
authored andcommittedMar 5, 2023
test_runner: allow nesting test within describe
PR-URL: #46544 Backport-PR-URL: #46839 Fixes: #46478 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4f5aff2 commit 391ff0d

File tree

4 files changed

+76
-62
lines changed

4 files changed

+76
-62
lines changed
 

‎lib/internal/test_runner/harness.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ function getGlobalRoot() {
165165
}
166166

167167
function test(name, options, fn) {
168-
const subtest = getGlobalRoot().createSubtest(Test, name, options, fn);
168+
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
169+
const subtest = parent.createSubtest(Test, name, options, fn);
169170
return subtest.start();
170171
}
171172

‎test/message/test_runner_describe_it.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33
require('../common');
44
const assert = require('node:assert');
5-
const { describe, it } = require('node:test');
5+
const { describe, it, test } = require('node:test');
66
const util = require('util');
77

88

@@ -41,6 +41,8 @@ it('async pass', async () => {
4141

4242
});
4343

44+
test('mixing describe/it and test should work', () => {});
45+
4446
it('async throw fail', async () => {
4547
throw new Error('thrown from async throw fail');
4648
});
@@ -95,6 +97,7 @@ describe('subtest sync throw fail', () => {
9597
it('+sync throw fail', () => {
9698
throw new Error('thrown from subtest sync throw fail');
9799
});
100+
test('mixing describe/it and test should work', () => {});
98101
});
99102

100103
it('sync throw non-error fail', async () => {
@@ -106,7 +109,7 @@ describe('level 0a', { concurrency: 4 }, () => {
106109
const p1a = new Promise((resolve) => {
107110
setTimeout(() => {
108111
resolve();
109-
}, 1000);
112+
}, 100);
110113
});
111114

112115
return p1a;
@@ -124,7 +127,7 @@ describe('level 0a', { concurrency: 4 }, () => {
124127
const p1c = new Promise((resolve) => {
125128
setTimeout(() => {
126129
resolve();
127-
}, 2000);
130+
}, 200);
128131
});
129132

130133
return p1c;
@@ -134,7 +137,7 @@ describe('level 0a', { concurrency: 4 }, () => {
134137
const p1c = new Promise((resolve) => {
135138
setTimeout(() => {
136139
resolve();
137-
}, 1500);
140+
}, 150);
138141
});
139142

140143
return p1c;
@@ -143,7 +146,7 @@ describe('level 0a', { concurrency: 4 }, () => {
143146
const p0a = new Promise((resolve) => {
144147
setTimeout(() => {
145148
resolve();
146-
}, 3000);
149+
}, 300);
147150
});
148151

149152
return p0a;
@@ -309,12 +312,12 @@ describe('describe async throw fails', async () => {
309312
describe('timeouts', () => {
310313
it('timed out async test', { timeout: 5 }, async () => {
311314
return new Promise((resolve) => {
312-
setTimeout(resolve, 1000);
315+
setTimeout(resolve, 100);
313316
});
314317
});
315318

316319
it('timed out callback test', { timeout: 5 }, (done) => {
317-
setTimeout(done, 1000);
320+
setTimeout(done, 100);
318321
});
319322

320323

‎test/message/test_runner_describe_it.out

+63-53
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ ok 10 - async pass
8282
---
8383
duration_ms: *
8484
...
85+
# Subtest: mixing describe/it and test should work
86+
ok 11 - mixing describe/it and test should work
87+
---
88+
duration_ms: *
89+
...
8590
# Subtest: async throw fail
86-
not ok 11 - async throw fail
91+
not ok 12 - async throw fail
8792
---
8893
duration_ms: *
8994
failureType: 'testCodeFailure'
@@ -99,15 +104,15 @@ not ok 11 - async throw fail
99104
*
100105
...
101106
# Subtest: async skip fail
102-
not ok 12 - async skip fail
107+
not ok 13 - async skip fail
103108
---
104109
duration_ms: *
105110
failureType: 'callbackAndPromisePresent'
106111
error: 'passed a callback but also returned a Promise'
107112
code: 'ERR_TEST_FAILURE'
108113
...
109114
# Subtest: async assertion fail
110-
not ok 13 - async assertion fail
115+
not ok 14 - async assertion fail
111116
---
112117
duration_ms: *
113118
failureType: 'testCodeFailure'
@@ -130,12 +135,12 @@ not ok 13 - async assertion fail
130135
*
131136
...
132137
# Subtest: resolve pass
133-
ok 14 - resolve pass
138+
ok 15 - resolve pass
134139
---
135140
duration_ms: *
136141
...
137142
# Subtest: reject fail
138-
not ok 15 - reject fail
143+
not ok 16 - reject fail
139144
---
140145
duration_ms: *
141146
failureType: 'testCodeFailure'
@@ -151,27 +156,27 @@ not ok 15 - reject fail
151156
*
152157
...
153158
# Subtest: unhandled rejection - passes but warns
154-
ok 16 - unhandled rejection - passes but warns
159+
ok 17 - unhandled rejection - passes but warns
155160
---
156161
duration_ms: *
157162
...
158163
# Subtest: async unhandled rejection - passes but warns
159-
ok 17 - async unhandled rejection - passes but warns
164+
ok 18 - async unhandled rejection - passes but warns
160165
---
161166
duration_ms: *
162167
...
163168
# Subtest: immediate throw - passes but warns
164-
ok 18 - immediate throw - passes but warns
169+
ok 19 - immediate throw - passes but warns
165170
---
166171
duration_ms: *
167172
...
168173
# Subtest: immediate reject - passes but warns
169-
ok 19 - immediate reject - passes but warns
174+
ok 20 - immediate reject - passes but warns
170175
---
171176
duration_ms: *
172177
...
173178
# Subtest: immediate resolve pass
174-
ok 20 - immediate resolve pass
179+
ok 21 - immediate resolve pass
175180
---
176181
duration_ms: *
177182
...
@@ -195,16 +200,21 @@ ok 20 - immediate resolve pass
195200
*
196201
*
197202
...
198-
1..1
199-
not ok 21 - subtest sync throw fail
203+
# Subtest: mixing describe/it and test should work
204+
ok 2 - mixing describe/it and test should work
205+
---
206+
duration_ms: *
207+
...
208+
1..2
209+
not ok 22 - subtest sync throw fail
200210
---
201211
duration_ms: *
202212
failureType: 'subtestsFailed'
203213
error: '1 subtest failed'
204214
code: 'ERR_TEST_FAILURE'
205215
...
206216
# Subtest: sync throw non-error fail
207-
not ok 22 - sync throw non-error fail
217+
not ok 23 - sync throw non-error fail
208218
---
209219
duration_ms: *
210220
failureType: 'testCodeFailure'
@@ -233,27 +243,27 @@ not ok 22 - sync throw non-error fail
233243
duration_ms: *
234244
...
235245
1..4
236-
ok 23 - level 0a
246+
ok 24 - level 0a
237247
---
238248
duration_ms: *
239249
...
240250
# Subtest: invalid subtest - pass but subtest fails
241-
ok 24 - invalid subtest - pass but subtest fails
251+
ok 25 - invalid subtest - pass but subtest fails
242252
---
243253
duration_ms: *
244254
...
245255
# Subtest: sync skip option
246-
ok 25 - sync skip option # SKIP
256+
ok 26 - sync skip option # SKIP
247257
---
248258
duration_ms: *
249259
...
250260
# Subtest: sync skip option with message
251-
ok 26 - sync skip option with message # SKIP this is skipped
261+
ok 27 - sync skip option with message # SKIP this is skipped
252262
---
253263
duration_ms: *
254264
...
255265
# Subtest: sync skip option is false fail
256-
not ok 27 - sync skip option is false fail
266+
not ok 28 - sync skip option is false fail
257267
---
258268
duration_ms: *
259269
failureType: 'testCodeFailure'
@@ -269,67 +279,67 @@ not ok 27 - sync skip option is false fail
269279
*
270280
...
271281
# Subtest: <anonymous>
272-
ok 28 - <anonymous>
282+
ok 29 - <anonymous>
273283
---
274284
duration_ms: *
275285
...
276286
# Subtest: functionOnly
277-
ok 29 - functionOnly
287+
ok 30 - functionOnly
278288
---
279289
duration_ms: *
280290
...
281291
# Subtest: <anonymous>
282-
ok 30 - <anonymous>
292+
ok 31 - <anonymous>
283293
---
284294
duration_ms: *
285295
...
286296
# Subtest: test with only a name provided
287-
ok 31 - test with only a name provided
297+
ok 32 - test with only a name provided
288298
---
289299
duration_ms: *
290300
...
291301
# Subtest: <anonymous>
292-
ok 32 - <anonymous>
302+
ok 33 - <anonymous>
293303
---
294304
duration_ms: *
295305
...
296306
# Subtest: <anonymous>
297-
ok 33 - <anonymous> # SKIP
307+
ok 34 - <anonymous> # SKIP
298308
---
299309
duration_ms: *
300310
...
301311
# Subtest: test with a name and options provided
302-
ok 34 - test with a name and options provided # SKIP
312+
ok 35 - test with a name and options provided # SKIP
303313
---
304314
duration_ms: *
305315
...
306316
# Subtest: functionAndOptions
307-
ok 35 - functionAndOptions # SKIP
317+
ok 36 - functionAndOptions # SKIP
308318
---
309319
duration_ms: *
310320
...
311321
# Subtest: escaped description \\ \# \\\#\\
312-
ok 36 - escaped description \\ \# \\\#\\
322+
ok 37 - escaped description \\ \# \\\#\\
313323
---
314324
duration_ms: *
315325
...
316326
# Subtest: escaped skip message
317-
ok 37 - escaped skip message # SKIP \#skip
327+
ok 38 - escaped skip message # SKIP \#skip
318328
---
319329
duration_ms: *
320330
...
321331
# Subtest: escaped todo message
322-
ok 38 - escaped todo message # TODO \#todo
332+
ok 39 - escaped todo message # TODO \#todo
323333
---
324334
duration_ms: *
325335
...
326336
# Subtest: callback pass
327-
ok 39 - callback pass
337+
ok 40 - callback pass
328338
---
329339
duration_ms: *
330340
...
331341
# Subtest: callback fail
332-
not ok 40 - callback fail
342+
not ok 41 - callback fail
333343
---
334344
duration_ms: *
335345
failureType: 'testCodeFailure'
@@ -340,30 +350,30 @@ not ok 40 - callback fail
340350
*
341351
...
342352
# Subtest: sync t is this in test
343-
ok 41 - sync t is this in test
353+
ok 42 - sync t is this in test
344354
---
345355
duration_ms: *
346356
...
347357
# Subtest: async t is this in test
348-
ok 42 - async t is this in test
358+
ok 43 - async t is this in test
349359
---
350360
duration_ms: *
351361
...
352362
# Subtest: callback t is this in test
353-
ok 43 - callback t is this in test
363+
ok 44 - callback t is this in test
354364
---
355365
duration_ms: *
356366
...
357367
# Subtest: callback also returns a Promise
358-
not ok 44 - callback also returns a Promise
368+
not ok 45 - callback also returns a Promise
359369
---
360370
duration_ms: *
361371
failureType: 'callbackAndPromisePresent'
362372
error: 'passed a callback but also returned a Promise'
363373
code: 'ERR_TEST_FAILURE'
364374
...
365375
# Subtest: callback throw
366-
not ok 45 - callback throw
376+
not ok 46 - callback throw
367377
---
368378
duration_ms: *
369379
failureType: 'testCodeFailure'
@@ -379,7 +389,7 @@ not ok 45 - callback throw
379389
*
380390
...
381391
# Subtest: callback called twice
382-
not ok 46 - callback called twice
392+
not ok 47 - callback called twice
383393
---
384394
duration_ms: *
385395
failureType: 'multipleCallbackInvocations'
@@ -390,12 +400,12 @@ not ok 46 - callback called twice
390400
*
391401
...
392402
# Subtest: callback called twice in different ticks
393-
ok 47 - callback called twice in different ticks
403+
ok 48 - callback called twice in different ticks
394404
---
395405
duration_ms: *
396406
...
397407
# Subtest: callback called twice in future tick
398-
not ok 48 - callback called twice in future tick
408+
not ok 49 - callback called twice in future tick
399409
---
400410
duration_ms: *
401411
failureType: 'uncaughtException'
@@ -405,7 +415,7 @@ not ok 48 - callback called twice in future tick
405415
*
406416
...
407417
# Subtest: callback async throw
408-
not ok 49 - callback async throw
418+
not ok 50 - callback async throw
409419
---
410420
duration_ms: *
411421
failureType: 'uncaughtException'
@@ -416,20 +426,20 @@ not ok 49 - callback async throw
416426
*
417427
...
418428
# Subtest: callback async throw after done
419-
ok 50 - callback async throw after done
429+
ok 51 - callback async throw after done
420430
---
421431
duration_ms: *
422432
...
423433
# Subtest: custom inspect symbol fail
424-
not ok 51 - custom inspect symbol fail
434+
not ok 52 - custom inspect symbol fail
425435
---
426436
duration_ms: *
427437
failureType: 'testCodeFailure'
428438
error: 'customized'
429439
code: 'ERR_TEST_FAILURE'
430440
...
431441
# Subtest: custom inspect symbol that throws fail
432-
not ok 52 - custom inspect symbol that throws fail
442+
not ok 53 - custom inspect symbol that throws fail
433443
---
434444
duration_ms: *
435445
failureType: 'testCodeFailure'
@@ -480,7 +490,7 @@ not ok 52 - custom inspect symbol that throws fail
480490
*
481491
...
482492
1..2
483-
not ok 53 - subtest sync throw fails
493+
not ok 54 - subtest sync throw fails
484494
---
485495
duration_ms: *
486496
failureType: 'subtestsFailed'
@@ -497,7 +507,7 @@ not ok 53 - subtest sync throw fails
497507
code: 'ERR_TEST_FAILURE'
498508
...
499509
1..1
500-
not ok 54 - describe sync throw fails
510+
not ok 55 - describe sync throw fails
501511
---
502512
duration_ms: *
503513
failureType: 'testCodeFailure'
@@ -525,7 +535,7 @@ not ok 54 - describe sync throw fails
525535
code: 'ERR_TEST_FAILURE'
526536
...
527537
1..1
528-
not ok 55 - describe async throw fails
538+
not ok 56 - describe async throw fails
529539
---
530540
duration_ms: *
531541
failureType: 'testCodeFailure'
@@ -573,7 +583,7 @@ not ok 55 - describe async throw fails
573583
duration_ms: *
574584
...
575585
1..4
576-
not ok 56 - timeouts
586+
not ok 57 - timeouts
577587
---
578588
duration_ms: *
579589
failureType: 'subtestsFailed'
@@ -598,15 +608,15 @@ not ok 56 - timeouts
598608
*
599609
...
600610
1..2
601-
not ok 57 - successful thenable
611+
not ok 58 - successful thenable
602612
---
603613
duration_ms: *
604614
failureType: 'subtestsFailed'
605615
error: '1 subtest failed'
606616
code: 'ERR_TEST_FAILURE'
607617
...
608618
# Subtest: rejected thenable
609-
not ok 58 - rejected thenable
619+
not ok 59 - rejected thenable
610620
---
611621
duration_ms: *
612622
failureType: 'testCodeFailure'
@@ -616,7 +626,7 @@ not ok 58 - rejected thenable
616626
*
617627
...
618628
# Subtest: invalid subtest fail
619-
not ok 59 - invalid subtest fail
629+
not ok 60 - invalid subtest fail
620630
---
621631
duration_ms: *
622632
failureType: 'parentAlreadyFinished'
@@ -625,15 +635,15 @@ not ok 59 - invalid subtest fail
625635
stack: |-
626636
*
627637
...
628-
1..59
638+
1..60
629639
# Warning: Test "unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
630640
# Warning: Test "async unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from async unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
631641
# Warning: Test "immediate throw - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from immediate throw fail" and would have caused the test to fail, but instead triggered an uncaughtException event.
632642
# Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
633643
# Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.
634644
# Warning: Test "callback async throw after done" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
635-
# tests 59
636-
# pass 22
645+
# tests 60
646+
# pass 23
637647
# fail 23
638648
# cancelled 0
639649
# skipped 9

‎test/message/test_runner_output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ test('level 0a', { concurrency: 4 }, async (t) => {
119119
return p1a;
120120
});
121121

122-
t.test('level 1b', async (t) => {
122+
test('level 1b', async (t) => {
123123
const p1b = new Promise((resolve) => {
124124
resolve();
125125
});

0 commit comments

Comments
 (0)
Please sign in to comment.