Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
MathMax,
Number,
NumberPrototypeToFixed,
ObjectAssign,
ObjectKeys,
ObjectSeal,
Promise,
Expand Down Expand Up @@ -358,7 +359,7 @@ class TestContext {
this.#test.todo(message);
}

test(name, options, fn) {
#runTest(name, options, fn) {
const overrides = {
__proto__: null,
loc: getCallerLocation(),
Expand All @@ -377,6 +378,14 @@ class TestContext {
return subtest.start();
}

test = ObjectAssign((...args) => this.#runTest(...args), {
__proto__: null,
expectFailure: (name, opts, fn) => this.#runTest(name, { __proto__: null, ...opts, expectFailure: true }, fn),
only: (name, opts, fn) => this.#runTest(name, { __proto__: null, ...opts, only: true }, fn),
skip: (name, opts, fn) => this.#runTest(name, { __proto__: null, ...opts, skip: true }, fn),
todo: (name, opts, fn) => this.#runTest(name, { __proto__: null, ...opts, todo: true }, fn),
});

before(fn, options) {
this.#test.createHook('before', fn, {
__proto__: null,
Expand Down
53 changes: 53 additions & 0 deletions test/parallel/test-runner-subtest-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

require('../common');
const assert = require('node:assert');
const { test } = require('node:test');
const { isPromise } = require('node:util/types');

test('subtest context should have test variants', async (t) => {
assert.strictEqual(typeof t.test, 'function');
assert.strictEqual(typeof t.test.expectFailure, 'function');
assert.strictEqual(typeof t.test.only, 'function');
assert.strictEqual(typeof t.test.skip, 'function');
assert.strictEqual(typeof t.test.todo, 'function');
});

test('subtest should return a promise', async (t) => {
const normal = t.test('normal subtest');
assert.ok(isPromise(normal));
await normal;
});

test('t.test[variant]() should return a promise', async (t) => {
assert.ok(isPromise(
t.test.expectFailure('expectFailure subtest', () => { throw new Error('This should pass'); })
));
assert.ok(isPromise(
t.test.only('only subtest')
));
assert.ok(isPromise(
t.test.skip('skip subtest')
));
assert.ok(isPromise(
t.test.todo('todo subtest')
));
});

test('nested subtests should have test variants', async (t) => {
await t.test('level 1', async (t) => {
assert.strictEqual(typeof t.test, 'function');
assert.strictEqual(typeof t.test.expectFailure, 'function');
assert.strictEqual(typeof t.test.only, 'function');
assert.strictEqual(typeof t.test.skip, 'function');
assert.strictEqual(typeof t.test.todo, 'function');

await t.test('level 2', async (t) => {
assert.strictEqual(typeof t.test, 'function');
assert.strictEqual(typeof t.test.expectFailure, 'function');
assert.strictEqual(typeof t.test.skip, 'function');
assert.strictEqual(typeof t.test.todo, 'function');
assert.strictEqual(typeof t.test.only, 'function');
});
});
});
Loading