Skip to content

Commit f8775f9

Browse files
committed
test_runner: add skip, todo, only to subtest context
The test context passed to subtests was missing the `.skip`, `.todo`, and `.only` variants on `t.test`. These were available on the top-level `test` function but not on the subtest context. Move the test function from a class method to an arrow function in the constructor so that `.skip`, `.todo`, and `.only` properties can be attached to it. Fixes: #50665
1 parent c6a56b4 commit f8775f9

File tree

2 files changed

+88
-19
lines changed

2 files changed

+88
-19
lines changed

lib/internal/test_runner/test.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const {
3+
ArrayPrototypeForEach,
34
ArrayPrototypePush,
45
ArrayPrototypePushApply,
56
ArrayPrototypeShift,
@@ -254,6 +255,47 @@ class TestContext {
254255

255256
constructor(test) {
256257
this.#test = test;
258+
259+
this.test = (name, options, fn) => {
260+
const overrides = {
261+
__proto__: null,
262+
loc: getCallerLocation(),
263+
};
264+
265+
const { plan } = this.#test;
266+
if (plan !== null) {
267+
plan.count();
268+
}
269+
270+
const subtest = this.#test.createSubtest(
271+
// eslint-disable-next-line no-use-before-define
272+
Test, name, options, fn, overrides,
273+
);
274+
275+
return subtest.start();
276+
};
277+
278+
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
279+
this.test[keyword] = (name, options, fn) => {
280+
const overrides = {
281+
__proto__: null,
282+
[keyword]: true,
283+
loc: getCallerLocation(),
284+
};
285+
286+
const { plan } = this.#test;
287+
if (plan !== null) {
288+
plan.count();
289+
}
290+
291+
const subtest = this.#test.createSubtest(
292+
// eslint-disable-next-line no-use-before-define
293+
Test, name, options, fn, overrides,
294+
);
295+
296+
return subtest.start();
297+
};
298+
});
257299
}
258300

259301
get signal() {
@@ -348,25 +390,6 @@ class TestContext {
348390
this.#test.todo(message);
349391
}
350392

351-
test(name, options, fn) {
352-
const overrides = {
353-
__proto__: null,
354-
loc: getCallerLocation(),
355-
};
356-
357-
const { plan } = this.#test;
358-
if (plan !== null) {
359-
plan.count();
360-
}
361-
362-
const subtest = this.#test.createSubtest(
363-
// eslint-disable-next-line no-use-before-define
364-
Test, name, options, fn, overrides,
365-
);
366-
367-
return subtest.start();
368-
}
369-
370393
before(fn, options) {
371394
this.#test.createHook('before', fn, {
372395
__proto__: null,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('node:assert');
4+
const { test } = require('node:test');
5+
const { isPromise } = require('node:util/types');
6+
7+
// Regression test for https://github.com/nodejs/node/issues/50665
8+
// Ensures that t.test.skip, t.test.todo, and t.test.only are available
9+
// on the subtest context.
10+
11+
test('subtest context should have test variants', async (t) => {
12+
assert.strictEqual(typeof t.test, 'function');
13+
assert.strictEqual(typeof t.test.skip, 'function');
14+
assert.strictEqual(typeof t.test.todo, 'function');
15+
assert.strictEqual(typeof t.test.only, 'function');
16+
});
17+
18+
test('subtest variants should return promises', async (t) => {
19+
const normal = t.test('normal subtest');
20+
const skipped = t.test.skip('skipped subtest');
21+
const todo = t.test.todo('todo subtest');
22+
23+
assert(isPromise(normal));
24+
assert(isPromise(skipped));
25+
assert(isPromise(todo));
26+
27+
await normal;
28+
await skipped;
29+
await todo;
30+
});
31+
32+
test('nested subtests should have test variants', async (t) => {
33+
await t.test('level 1', async (t) => {
34+
assert.strictEqual(typeof t.test, 'function');
35+
assert.strictEqual(typeof t.test.skip, 'function');
36+
assert.strictEqual(typeof t.test.todo, 'function');
37+
assert.strictEqual(typeof t.test.only, 'function');
38+
39+
await t.test('level 2', async (t) => {
40+
assert.strictEqual(typeof t.test, 'function');
41+
assert.strictEqual(typeof t.test.skip, 'function');
42+
assert.strictEqual(typeof t.test.todo, 'function');
43+
assert.strictEqual(typeof t.test.only, 'function');
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)