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
15 changes: 12 additions & 3 deletions 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,11 +359,11 @@ class TestContext {
this.#test.todo(message);
}

test(name, options, fn) {
const overrides = {
#runTest(name, options, fn, extraOverrides) {
const overrides = ObjectAssign({
__proto__: null,
loc: getCallerLocation(),
};
}, extraOverrides);

const { plan } = this.#test;
if (plan !== null) {
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, opts, fn, { __proto__: null, expectFailure: true }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docs for this one

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

before(fn, options) {
this.#test.createHook('before', fn, {
__proto__: null,
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-runner-subtest-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'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) => {
const xfail = t.test.expectFailure('expectFailure subtest', () => { throw new Error('This should pass'); });
const only = t.test.only('only subtest');
const skip = t.test.skip('skip subtest');
const todo = t.test.todo('todo subtest');

assert.ok(isPromise(xfail));
assert.ok(isPromise(only));
assert.ok(isPromise(skip));
assert.ok(isPromise(todo));

await xfail;
await only;
await skip;
await todo;
});

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