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
40 changes: 38 additions & 2 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,10 +359,10 @@ class TestContext {
this.#test.todo(message);
}

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

const { plan } = this.#test;
Expand All @@ -377,6 +378,41 @@ class TestContext {
return subtest.start();
}

// [1] `getCallerLocation` cannot be nested (that causes it to cite the nested location instead).

test = ObjectAssign((name, options, fn) => this.#runTest(
name,
options,
fn,
getCallerLocation(), // [1]
), {
__proto__: null,
expectFailure: (name, opts, fn) => this.#runTest(
name,
{ __proto__: null, ...opts, expectFailure: true },
fn,
getCallerLocation(), // [1]
),
only: (name, opts, fn) => this.#runTest(
name,
{ __proto__: null, ...opts, only: true },
fn,
getCallerLocation(), // [1]
),
skip: (name, opts, fn) => this.#runTest(
name,
{ __proto__: null, ...opts, skip: true },
fn,
getCallerLocation(), // [1]
),
todo: (name, opts, fn) => this.#runTest(
name,
{ __proto__: null, ...opts, todo: true },
fn,
getCallerLocation(), // [1]
),
});

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