Skip to content

Commit debc0ad

Browse files
MoLowtargos
authored andcommitted
test_runner: avoid running twice tests in describe
PR-URL: #46888 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 0923cbc commit debc0ad

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
@@ -773,7 +773,8 @@ changes:
773773
to this function is a [`TestContext`][] object. If the test uses callbacks,
774774
the callback function is passed as the second argument. **Default:** A no-op
775775
function.
776-
* Returns: {Promise} Resolved with `undefined` once the test completes.
776+
* Returns: {Promise} Resolved with `undefined` once
777+
the test completes, or immediately if the test runs within [`describe()`][].
777778

778779
The `test()` function is the value imported from the `test` module. Each
779780
invocation of this function results in reporting the test to the {TestsStream}.
@@ -782,10 +783,12 @@ The `TestContext` object passed to the `fn` argument can be used to perform
782783
actions related to the current test. Examples include skipping the test, adding
783784
additional diagnostic information, or creating subtests.
784785

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

790793
```js
791794
test('top level test', async (t) => {
@@ -1745,6 +1748,7 @@ added:
17451748
[`context.diagnostic`]: #contextdiagnosticmessage
17461749
[`context.skip`]: #contextskipmessage
17471750
[`context.todo`]: #contexttodomessage
1751+
[`describe()`]: #describename-options-fn
17481752
[`run()`]: #runoptions
17491753
[`test()`]: #testname-options-fn
17501754
[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
} = primordials;
67
const {
@@ -187,31 +188,27 @@ async function startSubtest(subtest) {
187188
await subtest.start();
188189
}
189190

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

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

209206
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
210-
cb[keyword] = (name, options, fn) => {
207+
test[keyword] = (name, options, fn) => {
211208
run(name, options, fn, { [keyword]: true });
212209
};
213210
});
214-
return cb;
211+
return test;
215212
}
216213

217214
function hook(hook) {
@@ -223,7 +220,7 @@ function hook(hook) {
223220

224221
module.exports = {
225222
createTestTree,
226-
test,
223+
test: runInParentContext(Test, false),
227224
describe: runInParentContext(Suite),
228225
it: runInParentContext(Test),
229226
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)