Skip to content

Commit 95d8d57

Browse files
committed
test_runner: avoid running twice tests in describe
1 parent 3803b02 commit 95d8d57

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

doc/api/test.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,8 @@ changes:
778778
to this function is a [`TestContext`][] object. If the test uses callbacks,
779779
the callback function is passed as the second argument. **Default:** A no-op
780780
function.
781-
* Returns: {Promise} Resolved with `undefined` once the test completes.
781+
* Returns: {Promise} Resolved with `undefined` once
782+
the test completes, or immediately if the test runs within [`describe()`][].
782783

783784
The `test()` function is the value imported from the `test` module. Each
784785
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
787788
actions related to the current test. Examples include skipping the test, adding
788789
additional diagnostic information, or creating subtests.
789790

790-
`test()` returns a `Promise` that resolves once the test completes. The return
791-
value can usually be discarded for top level tests. However, the return value
792-
from subtests should be used to prevent the parent test from finishing first
793-
and cancelling the subtest as shown in the following example.
791+
`test()` returns a `Promise` that resolves once the test completes.
792+
if `test()` is called within a `describe()` block, it resolve immediately.
793+
The return value can usually be discarded for top level tests.
794+
However, the return value from subtests should be used to prevent the parent
795+
test from finishing first and cancelling the subtest
796+
as shown in the following example.
794797

795798
```js
796799
test('top level test', async (t) => {
@@ -1780,6 +1783,7 @@ added:
17801783
[`context.diagnostic`]: #contextdiagnosticmessage
17811784
[`context.skip`]: #contextskipmessage
17821785
[`context.todo`]: #contexttodomessage
1786+
[`describe()`]: #describename-options-fn
17831787
[`run()`]: #runoptions
17841788
[`test()`]: #testname-options-fn
17851789
[describe options]: #describename-options-fn

lib/internal/test_runner/harness.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const {
33
ArrayPrototypeForEach,
4+
PromiseResolve,
45
SafeMap,
56
SafeWeakSet,
67
} = primordials;
@@ -186,31 +187,27 @@ async function startSubtest(subtest) {
186187
await subtest.start();
187188
}
188189

189-
function test(name, options, fn) {
190-
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
191-
const subtest = parent.createSubtest(Test, name, options, fn);
192-
return startSubtest(subtest);
193-
}
194-
195-
function runInParentContext(Factory) {
190+
function runInParentContext(Factory, addShorthands = true) {
196191
function run(name, options, fn, overrides) {
197192
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
198193
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
199-
if (parent === getGlobalRoot()) {
200-
startSubtest(subtest);
194+
if (!(parent instanceof Suite)) {
195+
return startSubtest(subtest);
201196
}
197+
return PromiseResolve();
202198
}
203199

204-
const cb = (name, options, fn) => {
205-
run(name, options, fn);
206-
};
200+
const test = (name, options, fn) => run(name, options, fn);
201+
if (!addShorthands) {
202+
return test;
203+
}
207204

208205
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
209-
cb[keyword] = (name, options, fn) => {
206+
test[keyword] = (name, options, fn) => {
210207
run(name, options, fn, { [keyword]: true });
211208
};
212209
});
213-
return cb;
210+
return test;
214211
}
215212

216213
function hook(hook) {
@@ -222,7 +219,7 @@ function hook(hook) {
222219

223220
module.exports = {
224221
createTestTree,
225-
test,
222+
test: runInParentContext(Test, false),
226223
describe: runInParentContext(Suite),
227224
it: runInParentContext(ItTest),
228225
before: hook('before'),

test/message/test_runner_hooks.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('describe hooks', () => {
3232
});
3333

3434
it('1', () => testArr.push('1'));
35-
it('2', () => testArr.push('2'));
35+
test('2', () => testArr.push('2'));
3636

3737
describe('nested', () => {
3838
before(function() {
@@ -48,44 +48,44 @@ describe('describe hooks', () => {
4848
testArr.push('afterEach ' + this.name);
4949
});
5050
it('nested 1', () => testArr.push('nested 1'));
51-
it('nested 2', () => testArr.push('nested 2'));
51+
test('nested 2', () => testArr.push('nested 2'));
5252
});
5353
});
5454

5555
describe('before throws', () => {
5656
before(() => { throw new Error('before'); });
5757
it('1', () => {});
58-
it('2', () => {});
58+
test('2', () => {});
5959
});
6060

6161
describe('after throws', () => {
6262
after(() => { throw new Error('after'); });
6363
it('1', () => {});
64-
it('2', () => {});
64+
test('2', () => {});
6565
});
6666

6767
describe('beforeEach throws', () => {
6868
beforeEach(() => { throw new Error('beforeEach'); });
6969
it('1', () => {});
70-
it('2', () => {});
70+
test('2', () => {});
7171
});
7272

7373
describe('afterEach throws', () => {
7474
afterEach(() => { throw new Error('afterEach'); });
7575
it('1', () => {});
76-
it('2', () => {});
76+
test('2', () => {});
7777
});
7878

7979
describe('afterEach when test fails', () => {
8080
afterEach(common.mustCall(2));
8181
it('1', () => { throw new Error('test'); });
82-
it('2', () => {});
82+
test('2', () => {});
8383
});
8484

8585
describe('afterEach throws and test fails', () => {
8686
afterEach(() => { throw new Error('afterEach'); });
8787
it('1', () => { throw new Error('test'); });
88-
it('2', () => {});
88+
test('2', () => {});
8989
});
9090

9191
test('test hooks', async (t) => {

0 commit comments

Comments
 (0)