Skip to content

Commit 0a690ef

Browse files
richiemccolljuanarbol
authored andcommitted
test_runner: add describe.only and it.only shorthands
PR-URL: #46604 Backport-PR-URL: #46839 Fixes: #46562 Reviewed-By: Moshe Atlow <[email protected]>
1 parent 28a1317 commit 0a690ef

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

doc/api/test.md

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

806+
## `describe.only([name][, options][, fn])`
807+
808+
<!-- YAML
809+
added: REPLACEME
810+
-->
811+
812+
Shorthand for marking a suite as `only`, same as
813+
[`describe([name], { only: true }[, fn])`][describe options].
814+
806815
## `it([name][, options][, fn])`
807816

808817
* `name` {string} The name of the test, which is displayed when reporting test
@@ -827,6 +836,15 @@ same as [`it([name], { skip: true }[, fn])`][it options].
827836
Shorthand for marking a test as `TODO`,
828837
same as [`it([name], { todo: true }[, fn])`][it options].
829838

839+
## `it.only([name][, options][, fn])`
840+
841+
<!-- YAML
842+
added: REPLACEME
843+
-->
844+
845+
Shorthand for marking a test as `only`,
846+
same as [`it([name], { only: true }[, fn])`][it options].
847+
830848
## `before([fn][, options])`
831849

832850
<!-- YAML

lib/internal/test_runner/harness.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function runInParentContext(Factory) {
195195
run(name, options, fn);
196196
};
197197

198-
ArrayPrototypeForEach(['skip', 'todo'], (keyword) => {
198+
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
199199
cb[keyword] = (name, options, fn) => {
200200
run(name, options, fn, { [keyword]: true });
201201
};

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('describe 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

+76-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,82 @@ 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: describe 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 - describe only = true, with a mixture of subtests
168+
---
169+
duration_ms: *
170+
...
171+
# Subtest: describe only = true, with subtests
172+
# Subtest: subtest should run
173+
ok 1 - subtest should run
174+
---
175+
duration_ms: *
176+
...
177+
# Subtest: async subtest should run
178+
ok 2 - async subtest should run
179+
---
180+
duration_ms: *
181+
...
182+
# Subtest: subtest should be skipped
183+
ok 3 - subtest should be skipped # SKIP 'only' option not set
184+
---
185+
duration_ms: *
186+
...
187+
1..3
188+
ok 14 - describe only = true, with subtests
189+
---
190+
duration_ms: *
191+
...
192+
1..14
193+
# tests 14
194+
# pass 4
122195
# fail 0
123196
# cancelled 0
124197
# skipped 10

0 commit comments

Comments
 (0)