Files
scriptc/tests/fixtures/node-test/cases/suite-context.ts
T
Chris Tate c73f363906 Pass the SuiteContext and hook contexts
node:test changes, matching Node 24.22:

- A describe or suite body may take its SuiteContext, with name,
  fullName, filePath, attempt, passed and diagnostic. Its passed is false
  until the suite has finished, after its after hooks.
- before and after hooks may take the suite's context, and beforeEach
  and afterEach hooks the test's. A test's t.before, t.after,
  t.beforeEach and t.afterEach hooks may take the test's context.
- SuiteContext maps to the runner node that TestContext maps to, so the
  TestContext members that SuiteContext shares lower the same way.

The suite-context fixture compares the reporter output and the order of
the bodies and hooks with Node, plain and with SCRIPTC_SAN=1.
2026-10-18 10:31:11 -05:00

58 lines
1.7 KiB
TypeScript

// The SuiteContext a describe body receives (name, fullName, filePath,
// attempt, passed, diagnostic), and the context hooks receive: before and
// after get the suite's, beforeEach and afterEach the test's, and a test's
// t.before and t.after hooks the test's.
import { after, afterEach, before, beforeEach, describe, it, test } from "node:test";
import * as assert from "node:assert";
const seen: string[] = [];
let outerPassed = (): boolean => false;
describe("outer", (s) => {
const file = (s.filePath ?? "").endsWith("tests/fixtures/node-test/cases/suite-context.ts");
seen.push(`body: ${s.name} | ${s.fullName} | ${s.passed} | ${s.attempt} | ${file}`);
s.diagnostic("suite diagnostic");
outerPassed = () => s.passed;
before((c) => {
seen.push(`before: ${c.name}`);
});
after((c) => {
seen.push(`after: ${c.name} ${c.passed}`);
});
beforeEach((c) => {
seen.push(`beforeEach: ${c.name}`);
});
afterEach((c) => {
seen.push(`afterEach: ${c.name} ${c.passed}`);
});
describe("inner", (s2) => {
seen.push(`inner: ${s2.fullName}`);
it("x", () => {});
});
it("y", (t) => {
t.before((c) => {
seen.push(`t.before: ${c.name}`);
});
t.after((c) => {
seen.push(`t.after: ${c.name} ${c.passed}`);
});
});
});
test("report", () => {
seen.push(`outer afterwards: ${outerPassed()}`);
assert.deepStrictEqual(seen, [
"body: outer | outer | false | 0 | true",
"inner: outer > inner",
"before: outer",
"beforeEach: x",
"afterEach: x true",
"beforeEach: y",
"t.before: y",
"afterEach: y true",
"t.after: y true",
"after: outer false",
"outer afterwards: true",
]);
});