Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: avoid running twice tests in describe #46888

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,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 @@ -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
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 @@ -1780,6 +1783,7 @@ added:
[`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,
SafeWeakSet,
} = primordials;
Expand Down Expand Up @@ -186,31 +187,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function run(name, options, fn, overrides) {
async function run(name, options, fn, overrides) {

doing this instead would ensure it always returns a promise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb change has caused invalid arguments to reject instead of throw so I will revert this

const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
if (parent === getGlobalRoot()) {
startSubtest(subtest);
if (!(parent instanceof Suite)) {
MoLow marked this conversation as resolved.
Show resolved Hide resolved
return startSubtest(subtest);
}
return PromiseResolve();
MoLow marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -222,7 +219,7 @@ function hook(hook) {

module.exports = {
createTestTree,
test,
test: runInParentContext(Test, false),
describe: runInParentContext(Suite),
it: runInParentContext(ItTest),
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