Skip to content

Commit 17befe0

Browse files
atlowChemitargos
authored andcommitted
test_runner: add shorthands to test
PR-URL: #47909 Fixes: #47897 Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 060c1d5 commit 17befe0

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
@@ -789,6 +789,9 @@ added:
789789
- v18.0.0
790790
- v16.17.0
791791
changes:
792+
- version: REPLACEME
793+
pr-url: https://github.com/nodejs/node/pull/47909
794+
description: Added the `skip`, `todo`, and `only` shorthands.
792795
- version:
793796
- v18.8.0
794797
- v16.18.0
@@ -864,6 +867,21 @@ The `timeout` option can be used to fail the test if it takes longer than
864867
canceling tests because a running test might block the application thread and
865868
thus prevent the scheduled cancellation.
866869

870+
## `test.skip([name][, options][, fn])`
871+
872+
Shorthand for skipping a test,
873+
same as [`test([name], { skip: true }[, fn])`][it options].
874+
875+
## `test.todo([name][, options][, fn])`
876+
877+
Shorthand for marking a test as `TODO`,
878+
same as [`test([name], { todo: true }[, fn])`][it options].
879+
880+
## `test.only([name][, options][, fn])`
881+
882+
Shorthand for marking a test as `only`,
883+
same as [`test([name], { only: true }[, fn])`][it options].
884+
867885
## `describe([name][, options][, fn])`
868886

869887
* `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
@@ -203,7 +203,7 @@ async function startSubtest(subtest) {
203203
await subtest.start();
204204
}
205205

206-
function runInParentContext(Factory, addShorthands = true) {
206+
function runInParentContext(Factory) {
207207
function run(name, options, fn, overrides) {
208208
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
209209
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
@@ -214,10 +214,6 @@ function runInParentContext(Factory, addShorthands = true) {
214214
}
215215

216216
const test = (name, options, fn) => run(name, options, fn);
217-
if (!addShorthands) {
218-
return test;
219-
}
220-
221217
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
222218
test[keyword] = (name, options, fn) => {
223219
run(name, options, fn, { [keyword]: true });
@@ -235,7 +231,7 @@ function hook(hook) {
235231

236232
module.exports = {
237233
createTestTree,
238-
test: runInParentContext(Test, false),
234+
test: runInParentContext(Test),
239235
describe: runInParentContext(Suite),
240236
it: runInParentContext(Test),
241237
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)