Skip to content

Commit 9f9585a

Browse files
committed
test_runner: describe it only shorthand
1 parent 214b00d commit 9f9585a

File tree

4 files changed

+124
-6
lines changed

4 files changed

+124
-6
lines changed

doc/api/test.md

+10
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,11 @@ 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+
Shorthand for marking a suite as `only`, same as
814+
[`describe([name], { only: true }[, fn])`][describe options].
815+
811816
## `it([name][, options][, fn])`
812817

813818
* `name` {string} The name of the test, which is displayed when reporting test
@@ -832,6 +837,11 @@ same as [`it([name], { skip: true }[, fn])`][it options].
832837
Shorthand for marking a test as `TODO`,
833838
same as [`it([name], { todo: true }[, fn])`][it options].
834839

840+
## `it.only([name][, options][, fn])`
841+
842+
Shorthand for marking a test as `only`,
843+
same as [`it([name], { only: true }[, fn])`][it options].
844+
835845
## `before([fn][, options])`
836846

837847
<!-- 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

+47-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,49 @@ 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=false', { only: false }, () => {
62+
throw new Error('This should not run');
63+
});
64+
65+
it.skip('`it` subtest 3 skip', () => {
66+
throw new Error('This should not run');
67+
});
68+
69+
it.todo('`it` subtest 4 todo', { only: false }, () => {
70+
throw new Error('This should not run');
71+
});
72+
});
73+
74+
describe.only('describe only = true, with subtests', () => {
75+
test('subtest should run', () => {});
76+
77+
test('async subtest should run', async () => {});
78+
79+
test('subtest should be skipped', { only: false }, () => {});
80+
});
81+
82+
// These two scenarios *should* work but don't as the parent
83+
// has no `only` flag set.
84+
describe('describe only - false with only subtest', () => {
85+
it.only('`it` subtest should run', () => {
86+
throw new Error('This should throw');
87+
});
88+
});
89+
90+
describe('describe only - false with only subtest', () => {
91+
test.only('subtest should run', () => {
92+
throw new Error('This should throw');
93+
});
94+
});

test/message/test_runner_only_tests.out

+66-4
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,73 @@ 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=false
147+
ok 3 - `it` subtest 2 only=false # SKIP 'only' option not set
148+
---
149+
duration_ms: *
150+
...
151+
# Subtest: `it` subtest 3 skip
152+
ok 4 - `it` subtest 3 skip # SKIP
153+
---
154+
duration_ms: *
155+
...
156+
# Subtest: `it` subtest 4 todo
157+
ok 5 - `it` subtest 4 todo # SKIP 'only' option not set
158+
---
159+
duration_ms: *
160+
...
161+
1..5
162+
ok 13 - desribe only = true, with a mixture of subtests
163+
---
164+
duration_ms: *
165+
...
166+
# Subtest: subtest should run
167+
ok 14 - subtest should run # SKIP 'only' option not set
168+
---
169+
duration_ms: *
170+
...
171+
# Subtest: async subtest should run
172+
ok 15 - async subtest should run # SKIP 'only' option not set
173+
---
174+
duration_ms: *
175+
...
176+
# Subtest: subtest should be skipped
177+
ok 16 - subtest should be skipped # SKIP 'only' option not set
178+
---
179+
duration_ms: *
180+
...
181+
1..19
182+
# tests 19
183+
# pass 4
122184
# fail 0
123185
# cancelled 0
124-
# skipped 10
186+
# skipped 15
125187
# todo 0
126188
# duration_ms *

0 commit comments

Comments
 (0)