Skip to content

Commit

Permalink
test_runner: allow nesting test within describe
Browse files Browse the repository at this point in the history
PR-URL: #46544
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>
  • Loading branch information
MoLow authored Feb 17, 2023
1 parent 4c1a277 commit 2787e2d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 62 deletions.
3 changes: 2 additions & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ function getGlobalRoot() {
}

function test(name, options, fn) {
const subtest = getGlobalRoot().createSubtest(Test, name, options, fn);
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Test, name, options, fn);
return subtest.start();
}

Expand Down
17 changes: 10 additions & 7 deletions test/message/test_runner_describe_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';
require('../common');
const assert = require('node:assert');
const { describe, it } = require('node:test');
const { describe, it, test } = require('node:test');
const util = require('util');


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

});

test('mixing describe/it and test should work', () => {});

it('async throw fail', async () => {
throw new Error('thrown from async throw fail');
});
Expand Down Expand Up @@ -95,6 +97,7 @@ describe('subtest sync throw fail', () => {
it('+sync throw fail', () => {
throw new Error('thrown from subtest sync throw fail');
});
test('mixing describe/it and test should work', () => {});
});

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

return p1a;
Expand All @@ -124,7 +127,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p1c = new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 2000);
}, 200);
});

return p1c;
Expand All @@ -134,7 +137,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p1c = new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1500);
}, 150);
});

return p1c;
Expand All @@ -143,7 +146,7 @@ describe('level 0a', { concurrency: 4 }, () => {
const p0a = new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 3000);
}, 300);
});

return p0a;
Expand Down Expand Up @@ -309,12 +312,12 @@ describe('describe async throw fails', async () => {
describe('timeouts', () => {
it('timed out async test', { timeout: 5 }, async () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000);
setTimeout(resolve, 100);
});
});

it('timed out callback test', { timeout: 5 }, (done) => {
setTimeout(done, 1000);
setTimeout(done, 100);
});


Expand Down
Loading

0 comments on commit 2787e2d

Please sign in to comment.