Skip to content
Merged
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
13 changes: 2 additions & 11 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
},
} = require('internal/errors');
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const { kCancelledByParent, Test, Suite, TestContext, SuiteContext } = require('internal/test_runner/test');
const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test');
const {
parseCommandLine,
reporterScope,
Expand Down Expand Up @@ -439,16 +439,7 @@ function getTestContext() {
if (test === undefined || test === reporterScope) {
return undefined;
}
// For hooks (hookType is set), return the test/suite being hooked (the parent)
const actualTest = test.hookType !== undefined ? test.parent : test;
if (actualTest === undefined) {
return undefined;
}
// Return SuiteContext for suites, TestContext for tests
if (actualTest instanceof Suite) {
return new SuiteContext(actualTest);
}
return new TestContext(actualTest);
return test.getCtx();
}

module.exports = {
Expand Down
21 changes: 18 additions & 3 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,14 @@ class Test extends AsyncResource {
}
}

#ctx;
getCtx() {
this.#ctx ??= new TestContext(this);
return this.#ctx;
}

getRunArgs() {
const ctx = new TestContext(this);
const ctx = this.getCtx();
return { __proto__: null, ctx, args: [ctx] };
}

Expand Down Expand Up @@ -1699,6 +1705,11 @@ class TestHook extends Test {
this.#args = args;
return super.run();
}

getCtx() {
return this.parentTest.getCtx();
}

getRunArgs() {
return this.#args;
}
Expand Down Expand Up @@ -1785,6 +1796,12 @@ class Suite extends Test {
this.buildPhaseFinished = true;
}

#ctx;
getCtx() {
this.#ctx ??= new TestContext(this);
return this.#ctx;
}

getRunArgs() {
const ctx = new SuiteContext(this);
return { __proto__: null, ctx, args: [ctx] };
Expand Down Expand Up @@ -1857,6 +1874,4 @@ module.exports = {
kUnwrapErrors,
Suite,
Test,
TestContext,
SuiteContext,
};
70 changes: 68 additions & 2 deletions test/parallel/test-runner-get-test-context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('node:assert');
const { test, getTestContext, describe, it } = require('node:test');
const {
test,
getTestContext,
describe,
it,
before,
after,
beforeEach,
afterEach,
} = require('node:test');

// Outside a test — must return undefined
assert.strictEqual(getTestContext(), undefined);
Expand Down Expand Up @@ -40,6 +49,63 @@ describe('getTestContext returns SuiteContext in suite', () => {
});
});

describe('getTestContext inside hooks', () => {
const suiteName = 'getTestContext inside hooks';

before(common.mustCall((t) => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, suiteName);
assert.strictEqual(ctx.name, t.name);
}));

beforeEach(common.mustCall(() => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, suiteName);
}));

afterEach(common.mustCall(() => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, suiteName);
}));

after(common.mustCall((t) => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, suiteName);
assert.strictEqual(ctx.name, t.name);
}));

it('runs inside the suite', () => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, 'runs inside the suite');
});
});

test('getTestContext inside test-level hooks returns the parent test', async (t) => {
const parentName = t.name;
t.beforeEach(common.mustCall(() => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, parentName);
}));

t.afterEach(common.mustCall(() => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, parentName);
}));

await t.test('child', () => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, 'child');
});
});

test('getTestContext works in test body during async operations', async (t) => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
Expand Down
4 changes: 3 additions & 1 deletion tools/eslint-rules/must-call-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ function isMustCallOrMustCallAtLeast(str) {
}

function isMustCallOrTest(str) {
return str === 'test' || str === 'it' || isMustCallOrMustCallAtLeast(str);
return str === 'test' || str === 'it' || str === 'describe' || str === 'suite' ||
str === 'before' || str === 'after' || str === 'beforeEach' || str === 'afterEach' ||
isMustCallOrMustCallAtLeast(str);
}

module.exports = {
Expand Down
Loading