test_runner: avoid running twice tests in describe

PR-URL: https://github.com/nodejs/node/pull/46888
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
pull/47011/head
Moshe Atlow 2023-02-28 22:26:58 +02:00
parent c4103c1ccf
commit a37b72da87
No known key found for this signature in database
GPG Key ID: CB1159A696865149
3 changed files with 29 additions and 28 deletions

View File

@ -778,7 +778,8 @@ changes:
to this function is a [`TestContext`][] object. If the test uses callbacks, to this function is a [`TestContext`][] object. If the test uses callbacks,
the callback function is passed as the second argument. **Default:** A no-op the callback function is passed as the second argument. **Default:** A no-op
function. function.
* Returns: {Promise} Resolved with `undefined` once the test completes. * Returns: {Promise} Resolved with `undefined` once
the test completes, or immediately if the test runs within [`describe()`][].
The `test()` function is the value imported from the `test` module. Each The `test()` function is the value imported from the `test` module. Each
invocation of this function results in reporting the test to the {TestsStream}. invocation of this function results in reporting the test to the {TestsStream}.
@ -787,10 +788,12 @@ The `TestContext` object passed to the `fn` argument can be used to perform
actions related to the current test. Examples include skipping the test, adding actions related to the current test. Examples include skipping the test, adding
additional diagnostic information, or creating subtests. additional diagnostic information, or creating subtests.
`test()` returns a `Promise` that resolves once the test completes. The return `test()` returns a `Promise` that resolves once the test completes.
value can usually be discarded for top level tests. However, the return value if `test()` is called within a `describe()` block, it resolve immediately.
from subtests should be used to prevent the parent test from finishing first The return value can usually be discarded for top level tests.
and cancelling the subtest as shown in the following example. However, the return value from subtests should be used to prevent the parent
test from finishing first and cancelling the subtest
as shown in the following example.
```js ```js
test('top level test', async (t) => { test('top level test', async (t) => {
@ -1780,6 +1783,7 @@ added:
[`context.diagnostic`]: #contextdiagnosticmessage [`context.diagnostic`]: #contextdiagnosticmessage
[`context.skip`]: #contextskipmessage [`context.skip`]: #contextskipmessage
[`context.todo`]: #contexttodomessage [`context.todo`]: #contexttodomessage
[`describe()`]: #describename-options-fn
[`run()`]: #runoptions [`run()`]: #runoptions
[`test()`]: #testname-options-fn [`test()`]: #testname-options-fn
[describe options]: #describename-options-fn [describe options]: #describename-options-fn

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { const {
ArrayPrototypeForEach, ArrayPrototypeForEach,
PromiseResolve,
SafeMap, SafeMap,
SafeWeakSet, SafeWeakSet,
} = primordials; } = primordials;
@ -186,31 +187,27 @@ async function startSubtest(subtest) {
await subtest.start(); await subtest.start();
} }
function test(name, options, fn) { function runInParentContext(Factory, addShorthands = true) {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Test, name, options, fn);
return startSubtest(subtest);
}
function runInParentContext(Factory) {
function run(name, options, fn, overrides) { function run(name, options, fn, overrides) {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot(); const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Factory, name, options, fn, overrides); const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
if (parent === getGlobalRoot()) { if (!(parent instanceof Suite)) {
startSubtest(subtest); return startSubtest(subtest);
} }
return PromiseResolve();
} }
const cb = (name, options, fn) => { const test = (name, options, fn) => run(name, options, fn);
run(name, options, fn); if (!addShorthands) {
}; return test;
}
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => { ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
cb[keyword] = (name, options, fn) => { test[keyword] = (name, options, fn) => {
run(name, options, fn, { [keyword]: true }); run(name, options, fn, { [keyword]: true });
}; };
}); });
return cb; return test;
} }
function hook(hook) { function hook(hook) {
@ -222,7 +219,7 @@ function hook(hook) {
module.exports = { module.exports = {
createTestTree, createTestTree,
test, test: runInParentContext(Test, false),
describe: runInParentContext(Suite), describe: runInParentContext(Suite),
it: runInParentContext(ItTest), it: runInParentContext(ItTest),
before: hook('before'), before: hook('before'),

View File

@ -32,7 +32,7 @@ describe('describe hooks', () => {
}); });
it('1', () => testArr.push('1')); it('1', () => testArr.push('1'));
it('2', () => testArr.push('2')); test('2', () => testArr.push('2'));
describe('nested', () => { describe('nested', () => {
before(function() { before(function() {
@ -48,44 +48,44 @@ describe('describe hooks', () => {
testArr.push('afterEach ' + this.name); testArr.push('afterEach ' + this.name);
}); });
it('nested 1', () => testArr.push('nested 1')); it('nested 1', () => testArr.push('nested 1'));
it('nested 2', () => testArr.push('nested 2')); test('nested 2', () => testArr.push('nested 2'));
}); });
}); });
describe('before throws', () => { describe('before throws', () => {
before(() => { throw new Error('before'); }); before(() => { throw new Error('before'); });
it('1', () => {}); it('1', () => {});
it('2', () => {}); test('2', () => {});
}); });
describe('after throws', () => { describe('after throws', () => {
after(() => { throw new Error('after'); }); after(() => { throw new Error('after'); });
it('1', () => {}); it('1', () => {});
it('2', () => {}); test('2', () => {});
}); });
describe('beforeEach throws', () => { describe('beforeEach throws', () => {
beforeEach(() => { throw new Error('beforeEach'); }); beforeEach(() => { throw new Error('beforeEach'); });
it('1', () => {}); it('1', () => {});
it('2', () => {}); test('2', () => {});
}); });
describe('afterEach throws', () => { describe('afterEach throws', () => {
afterEach(() => { throw new Error('afterEach'); }); afterEach(() => { throw new Error('afterEach'); });
it('1', () => {}); it('1', () => {});
it('2', () => {}); test('2', () => {});
}); });
describe('afterEach when test fails', () => { describe('afterEach when test fails', () => {
afterEach(common.mustCall(2)); afterEach(common.mustCall(2));
it('1', () => { throw new Error('test'); }); it('1', () => { throw new Error('test'); });
it('2', () => {}); test('2', () => {});
}); });
describe('afterEach throws and test fails', () => { describe('afterEach throws and test fails', () => {
afterEach(() => { throw new Error('afterEach'); }); afterEach(() => { throw new Error('afterEach'); });
it('1', () => { throw new Error('test'); }); it('1', () => { throw new Error('test'); });
it('2', () => {}); test('2', () => {});
}); });
test('test hooks', async (t) => { test('test hooks', async (t) => {