Skip to content

Commit c7c895a

Browse files
committed
test_runner: add describe.only and it.only shorthands
1 parent f46515c commit c7c895a

File tree

4 files changed

+125
-6
lines changed

4 files changed

+125
-6
lines changed

doc/api/test.md

+18
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,15 @@ Shorthand for skipping a suite, same as [`describe([name], { skip: true }[, fn])
808808
Shorthand for marking a suite as `TODO`, same as
809809
[`describe([name], { todo: true }[, fn])`][describe options].
810810

811+
## `describe.only([name][, options][, fn])`
812+
813+
<!-- YAML
814+
added: REPLACEME
815+
-->
816+
817+
Shorthand for marking a suite as `only`, same as
818+
[`describe([name], { only: true }[, fn])`][describe options].
819+
811820
## `it([name][, options][, fn])`
812821

813822
* `name` {string} The name of the test, which is displayed when reporting test
@@ -832,6 +841,15 @@ same as [`it([name], { skip: true }[, fn])`][it options].
832841
Shorthand for marking a test as `TODO`,
833842
same as [`it([name], { todo: true }[, fn])`][it options].
834843

844+
## `it.only([name][, options][, fn])`
845+
846+
<!-- YAML
847+
added: REPLACEME
848+
-->
849+
850+
Shorthand for marking a test as `only`,
851+
same as [`it([name], { only: true }[, fn])`][it options].
852+
835853
## `before([fn][, options])`
836854

837855
<!-- YAML

lib/internal/test_runner/harness.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ function runInParentContext(Factory) {
184184
run(name, options, fn);
185185
};
186186

187-
ArrayPrototypeForEach(['skip', 'todo'], (keyword) => {
187+
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
188188
cb[keyword] = (name, options, fn) => {
189189
run(name, options, fn, { [keyword]: true });
190190
};

test/message/test_runner_only_tests.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Flags: --no-warnings --test-only
22
'use strict';
33
require('../common');
4-
const test = require('node:test');
4+
const { test, describe, it } = require('node:test');
55

66
// These tests should be skipped based on the 'only' option.
77
test('only = undefined');
@@ -46,3 +46,37 @@ test('only = true, with subtests', { only: true }, async (t) => {
4646
await t.test('skipped subtest 3', { only: false });
4747
await t.test('skipped subtest 4', { skip: true });
4848
});
49+
50+
describe.only('describe only = true, with subtests', () => {
51+
it('`it` subtest 1 should run', () => {});
52+
53+
it('`it` subtest 2 should run', async () => {});
54+
});
55+
56+
describe.only('desribe only = true, with a mixture of subtests', () => {
57+
it.only('`it` subtest 1', () => {});
58+
59+
it.only('`it` async subtest 1', async () => {});
60+
61+
it('`it` subtest 2 only=true', { only: true });
62+
63+
it('`it` subtest 2 only=false', { only: false }, () => {
64+
throw new Error('This should not run');
65+
});
66+
67+
it.skip('`it` subtest 3 skip', () => {
68+
throw new Error('This should not run');
69+
});
70+
71+
it.todo('`it` subtest 4 todo', { only: false }, () => {
72+
throw new Error('This should not run');
73+
});
74+
});
75+
76+
describe.only('describe only = true, with subtests', () => {
77+
test('subtest should run', () => {});
78+
79+
test('async subtest should run', async () => {});
80+
81+
test('subtest should be skipped', { only: false }, () => {});
82+
});

test/message/test_runner_only_tests.out

+71-4
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,78 @@ ok 11 - only = true, with subtests
116116
---
117117
duration_ms: *
118118
...
119-
1..11
120-
# tests 11
121-
# pass 1
119+
# Subtest: describe only = true, with subtests
120+
# Subtest: `it` subtest 1 should run
121+
ok 1 - `it` subtest 1 should run
122+
---
123+
duration_ms: *
124+
...
125+
# Subtest: `it` subtest 2 should run
126+
ok 2 - `it` subtest 2 should run
127+
---
128+
duration_ms: *
129+
...
130+
1..2
131+
ok 12 - describe only = true, with subtests
132+
---
133+
duration_ms: *
134+
...
135+
# Subtest: desribe only = true, with a mixture of subtests
136+
# Subtest: `it` subtest 1
137+
ok 1 - `it` subtest 1
138+
---
139+
duration_ms: *
140+
...
141+
# Subtest: `it` async subtest 1
142+
ok 2 - `it` async subtest 1
143+
---
144+
duration_ms: *
145+
...
146+
# Subtest: `it` subtest 2 only=true
147+
ok 3 - `it` subtest 2 only=true
148+
---
149+
duration_ms: *
150+
...
151+
# Subtest: `it` subtest 2 only=false
152+
ok 4 - `it` subtest 2 only=false # SKIP 'only' option not set
153+
---
154+
duration_ms: *
155+
...
156+
# Subtest: `it` subtest 3 skip
157+
ok 5 - `it` subtest 3 skip # SKIP
158+
---
159+
duration_ms: *
160+
...
161+
# Subtest: `it` subtest 4 todo
162+
ok 6 - `it` subtest 4 todo # SKIP 'only' option not set
163+
---
164+
duration_ms: *
165+
...
166+
1..6
167+
ok 13 - desribe only = true, with a mixture of subtests
168+
---
169+
duration_ms: *
170+
...
171+
# Subtest: subtest should run
172+
ok 14 - subtest should run # SKIP 'only' option not set
173+
---
174+
duration_ms: *
175+
...
176+
# Subtest: async subtest should run
177+
ok 15 - async subtest should run # SKIP 'only' option not set
178+
---
179+
duration_ms: *
180+
...
181+
# Subtest: subtest should be skipped
182+
ok 16 - subtest should be skipped # SKIP 'only' option not set
183+
---
184+
duration_ms: *
185+
...
186+
1..17
187+
# tests 17
188+
# pass 4
122189
# fail 0
123190
# cancelled 0
124-
# skipped 10
191+
# skipped 13
125192
# todo 0
126193
# duration_ms *

0 commit comments

Comments
 (0)