Skip to content

Commit 409acd5

Browse files
atlowChemidanielleadams
authored andcommitted
test_runner: add shorthands to test
PR-URL: #47909 Fixes: #47897 Reviewed-By: Moshe Atlow <[email protected]>
1 parent 4ed61c2 commit 409acd5

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

doc/api/test.md

+18
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,9 @@ run({ files: [path.resolve('./tests/test.js')] })
770770
<!-- YAML
771771
added: v18.0.0
772772
changes:
773+
- version: REPLACEME
774+
pr-url: https://github.com/nodejs/node/pull/47909
775+
description: Added the `skip`, `todo`, and `only` shorthands.
773776
- version: v18.8.0
774777
pr-url: https://github.com/nodejs/node/pull/43554
775778
description: Add a `signal` option.
@@ -841,6 +844,21 @@ The `timeout` option can be used to fail the test if it takes longer than
841844
canceling tests because a running test might block the application thread and
842845
thus prevent the scheduled cancellation.
843846

847+
## `test.skip([name][, options][, fn])`
848+
849+
Shorthand for skipping a test,
850+
same as [`test([name], { skip: true }[, fn])`][it options].
851+
852+
## `test.todo([name][, options][, fn])`
853+
854+
Shorthand for marking a test as `TODO`,
855+
same as [`test([name], { todo: true }[, fn])`][it options].
856+
857+
## `test.only([name][, options][, fn])`
858+
859+
Shorthand for marking a test as `only`,
860+
same as [`test([name], { only: true }[, fn])`][it options].
861+
844862
## `describe([name][, options][, fn])`
845863

846864
* `name` {string} The name of the suite, which is displayed when reporting test

lib/internal/test_runner/harness.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ async function startSubtest(subtest) {
201201
await subtest.start();
202202
}
203203

204-
function runInParentContext(Factory, addShorthands = true) {
204+
function runInParentContext(Factory) {
205205
function run(name, options, fn, overrides) {
206206
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
207207
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
@@ -212,10 +212,6 @@ function runInParentContext(Factory, addShorthands = true) {
212212
}
213213

214214
const test = (name, options, fn) => run(name, options, fn);
215-
if (!addShorthands) {
216-
return test;
217-
}
218-
219215
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
220216
test[keyword] = (name, options, fn) => {
221217
run(name, options, fn, { [keyword]: true });
@@ -233,7 +229,7 @@ function hook(hook) {
233229

234230
module.exports = {
235231
createTestTree,
236-
test: runInParentContext(Test, false),
232+
test: runInParentContext(Test),
237233
describe: runInParentContext(Suite),
238234
it: runInParentContext(Test),
239235
before: hook('before'),

test/fixtures/test-runner/output/only_tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ describe.only('describe only = true, with a mixture of subtests', () => {
7171
it.todo('`it` subtest 4 todo', { only: false }, () => {
7272
throw new Error('This should not run');
7373
});
74+
75+
test.only('`test` subtest 1', () => {});
76+
77+
test.only('`test` async subtest 1', async () => {});
78+
79+
test('`test` subtest 2 only=true', { only: true });
80+
81+
test('`test` subtest 2 only=false', { only: false }, () => {
82+
throw new Error('This should not run');
83+
});
84+
85+
test.skip('`test` subtest 3 skip', () => {
86+
throw new Error('This should not run');
87+
});
88+
89+
test.todo('`test` subtest 4 todo', { only: false }, () => {
90+
throw new Error('This should not run');
91+
});
7492
});
7593

7694
describe.only('describe only = true, with subtests', () => {

test/fixtures/test-runner/output/only_tests.snapshot

+34-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,37 @@ ok 12 - describe only = true, with subtests
164164
---
165165
duration_ms: *
166166
...
167-
1..6
167+
# Subtest: `test` subtest 1
168+
ok 7 - `test` subtest 1
169+
---
170+
duration_ms: *
171+
...
172+
# Subtest: `test` async subtest 1
173+
ok 8 - `test` async subtest 1
174+
---
175+
duration_ms: *
176+
...
177+
# Subtest: `test` subtest 2 only=true
178+
ok 9 - `test` subtest 2 only=true
179+
---
180+
duration_ms: *
181+
...
182+
# Subtest: `test` subtest 2 only=false
183+
ok 10 - `test` subtest 2 only=false # SKIP 'only' option not set
184+
---
185+
duration_ms: *
186+
...
187+
# Subtest: `test` subtest 3 skip
188+
ok 11 - `test` subtest 3 skip # SKIP
189+
---
190+
duration_ms: *
191+
...
192+
# Subtest: `test` subtest 4 todo
193+
ok 12 - `test` subtest 4 todo # SKIP 'only' option not set
194+
---
195+
duration_ms: *
196+
...
197+
1..12
168198
ok 13 - describe only = true, with a mixture of subtests
169199
---
170200
duration_ms: *
@@ -193,11 +223,11 @@ ok 14 - describe only = true, with subtests
193223
type: 'suite'
194224
...
195225
1..14
196-
# tests 34
226+
# tests 40
197227
# suites 3
198-
# pass 14
228+
# pass 17
199229
# fail 0
200230
# cancelled 0
201-
# skipped 20
231+
# skipped 23
202232
# todo 0
203233
# duration_ms *

0 commit comments

Comments
 (0)