Skip to content

Commit

Permalink
test_runner: avoid running twice tests in describe
Browse files Browse the repository at this point in the history
PR-URL: #46888
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
MoLow authored and danielleadams committed Apr 11, 2023
1 parent 524eaf5 commit 413493c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
14 changes: 9 additions & 5 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,8 @@ changes:
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
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
invocation of this function results in reporting the test to the {TestsStream}.
Expand All @@ -776,10 +777,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
additional diagnostic information, or creating subtests.

`test()` returns a `Promise` that resolves once the test completes. The return
value can usually be discarded for top level tests. 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.
`test()` returns a `Promise` that resolves once the test completes.
if `test()` is called within a `describe()` block, it resolve immediately.
The return value can usually be discarded for top level tests.
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
test('top level test', async (t) => {
Expand Down Expand Up @@ -1700,6 +1703,7 @@ added: v18.7.0
[`context.diagnostic`]: #contextdiagnosticmessage
[`context.skip`]: #contextskipmessage
[`context.todo`]: #contexttodomessage
[`describe()`]: #describename-options-fn
[`run()`]: #runoptions
[`test()`]: #testname-options-fn
[describe options]: #describename-options-fn
Expand Down
27 changes: 12 additions & 15 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {
ArrayPrototypeForEach,
PromiseResolve,
SafeMap,
} = primordials;
const {
Expand Down Expand Up @@ -185,31 +186,27 @@ async function startSubtest(subtest) {
await subtest.start();
}

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

function runInParentContext(Factory) {
function runInParentContext(Factory, addShorthands = true) {
function run(name, options, fn, overrides) {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
if (parent === getGlobalRoot()) {
startSubtest(subtest);
if (!(parent instanceof Suite)) {
return startSubtest(subtest);
}
return PromiseResolve();
}

const cb = (name, options, fn) => {
run(name, options, fn);
};
const test = (name, options, fn) => run(name, options, fn);
if (!addShorthands) {
return test;
}

ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
cb[keyword] = (name, options, fn) => {
test[keyword] = (name, options, fn) => {
run(name, options, fn, { [keyword]: true });
};
});
return cb;
return test;
}

function hook(hook) {
Expand All @@ -221,7 +218,7 @@ function hook(hook) {

module.exports = {
createTestTree,
test,
test: runInParentContext(Test, false),
describe: runInParentContext(Suite),
it: runInParentContext(Test),
before: hook('before'),
Expand Down
16 changes: 8 additions & 8 deletions test/message/test_runner_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('describe hooks', () => {
});

it('1', () => testArr.push('1'));
it('2', () => testArr.push('2'));
test('2', () => testArr.push('2'));

describe('nested', () => {
before(function() {
Expand All @@ -48,44 +48,44 @@ describe('describe hooks', () => {
testArr.push('afterEach ' + this.name);
});
it('nested 1', () => testArr.push('nested 1'));
it('nested 2', () => testArr.push('nested 2'));
test('nested 2', () => testArr.push('nested 2'));
});
});

describe('before throws', () => {
before(() => { throw new Error('before'); });
it('1', () => {});
it('2', () => {});
test('2', () => {});
});

describe('after throws', () => {
after(() => { throw new Error('after'); });
it('1', () => {});
it('2', () => {});
test('2', () => {});
});

describe('beforeEach throws', () => {
beforeEach(() => { throw new Error('beforeEach'); });
it('1', () => {});
it('2', () => {});
test('2', () => {});
});

describe('afterEach throws', () => {
afterEach(() => { throw new Error('afterEach'); });
it('1', () => {});
it('2', () => {});
test('2', () => {});
});

describe('afterEach when test fails', () => {
afterEach(common.mustCall(2));
it('1', () => { throw new Error('test'); });
it('2', () => {});
test('2', () => {});
});

describe('afterEach throws and test fails', () => {
afterEach(() => { throw new Error('afterEach'); });
it('1', () => { throw new Error('test'); });
it('2', () => {});
test('2', () => {});
});

test('test hooks', async (t) => {
Expand Down

0 comments on commit 413493c

Please sign in to comment.