Skip to content

Commit e611323

Browse files
JakobJingleheimerheathduttonFelipeness
committed
test_runner: add context subtests expectFailure only skip todo
Co-Authored-By: Heath Dutton🕴️ <heathdutton@linux.com> Co-Authored-By: Felipe <60716370+felipeness@users.noreply.github.com>
1 parent 1989f4d commit e611323

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

lib/internal/test_runner/test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
MathMax,
1515
Number,
1616
NumberPrototypeToFixed,
17+
ObjectAssign,
1718
ObjectKeys,
1819
ObjectSeal,
1920
Promise,
@@ -358,11 +359,11 @@ class TestContext {
358359
this.#test.todo(message);
359360
}
360361

361-
test(name, options, fn) {
362-
const overrides = {
362+
#runTest(name, options, fn, extraOverrides) {
363+
const overrides = ObjectAssign({
363364
__proto__: null,
364365
loc: getCallerLocation(),
365-
};
366+
}, extraOverrides);
366367

367368
const { plan } = this.#test;
368369
if (plan !== null) {
@@ -377,6 +378,14 @@ class TestContext {
377378
return subtest.start();
378379
}
379380

381+
test = ObjectAssign((...args) => this.#runTest(...args), {
382+
__proto__: null,
383+
expectFailure: (name, opts, fn) => this.#runTest(name, opts, fn, { __proto__: null, expectFailure: true }),
384+
only: (name, opts, fn) => this.#runTest(name, opts, fn, { __proto__: null, only: true }),
385+
skip: (name, opts, fn) => this.#runTest(name, opts, fn, { __proto__: null, skip: true }),
386+
todo: (name, opts, fn) => this.#runTest(name, opts, fn, { __proto__: null, todo: true }),
387+
});
388+
380389
before(fn, options) {
381390
this.#test.createHook('before', fn, {
382391
__proto__: null,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('node:assert');
5+
const { test } = require('node:test');
6+
const { isPromise } = require('node:util/types');
7+
8+
test('subtest context should have test variants', async (t) => {
9+
assert.strictEqual(typeof t.test, 'function');
10+
assert.strictEqual(typeof t.test.expectFailure, 'function');
11+
assert.strictEqual(typeof t.test.only, 'function');
12+
assert.strictEqual(typeof t.test.skip, 'function');
13+
assert.strictEqual(typeof t.test.todo, 'function');
14+
});
15+
16+
test('subtest should return a promise', async (t) => {
17+
const normal = t.test('normal subtest');
18+
assert.ok(isPromise(normal));
19+
await normal;
20+
});
21+
22+
test('t.test[variant]() should return a promise', async (t) => {
23+
assert.ok(isPromise(
24+
t.test.expectFailure('expectFailure subtest', () => { throw new Error('This should pass'); })
25+
));
26+
assert.ok(isPromise(
27+
t.test.only('only subtest')
28+
));
29+
assert.ok(isPromise(
30+
t.test.skip('skip subtest')
31+
));
32+
assert.ok(isPromise(
33+
t.test.todo('todo subtest')
34+
));
35+
});
36+
37+
test('nested subtests should have test variants', async (t) => {
38+
await t.test('level 1', async (t) => {
39+
assert.strictEqual(typeof t.test, 'function');
40+
assert.strictEqual(typeof t.test.expectFailure, 'function');
41+
assert.strictEqual(typeof t.test.only, 'function');
42+
assert.strictEqual(typeof t.test.skip, 'function');
43+
assert.strictEqual(typeof t.test.todo, 'function');
44+
45+
await t.test('level 2', async (t) => {
46+
assert.strictEqual(typeof t.test, 'function');
47+
assert.strictEqual(typeof t.test.expectFailure, 'function');
48+
assert.strictEqual(typeof t.test.skip, 'function');
49+
assert.strictEqual(typeof t.test.todo, 'function');
50+
assert.strictEqual(typeof t.test.only, 'function');
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)