cmd/pkgserver: allow non-global js test suites

This commit is contained in:
Kat
2026-03-20 20:00:54 +11:00
parent e0f32ead04
commit 877be3308e
4 changed files with 94 additions and 16 deletions

View File

@@ -5,11 +5,32 @@ type TestTree = TestGroup | Test;
type TestGroup = { name: string, children: TestTree[] };
type Test = { name: string, test: (TestController) => void };
let TESTS: TestGroup[] = [];
export class TestRegistrar {
#suites: TestGroup[];
constructor() {
this.#suites = [];
}
suite(name: string, children: TestTree[]) {
checkDuplicates(name, children)
this.#suites.push({ name, children });
}
run(reporter: Reporter) {
reporter.register(this.#suites);
for (const suite of this.#suites) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
reporter.finalize();
}
}
export let TESTS = new TestRegistrar();
// Register a suite in the global registrar.
export function suite(name: string, children: TestTree[]) {
checkDuplicates(name, children);
TESTS.push({ name, children });
TESTS.suite(name, children);
}
export function context(name: string, children: TestTree[]): TestTree {
@@ -79,14 +100,6 @@ export interface TestResult {
logs: string[];
}
export function run(reporter: Reporter) {
reporter.register(TESTS);
for (const suite of TESTS) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
reporter.finalize();
}
function runTests(reporter: Reporter, parents: string[], node: TestTree) {
const path = [...parents, node.name];
if ("children" in node) {
@@ -124,6 +137,30 @@ export interface Reporter {
finalize(): void;
}
export class NoOpReporter implements Reporter {
suites: TestGroup[];
results: ({ path: string[] } & TestResult)[];
finalized: boolean;
constructor() {
this.suites = [];
this.results = [];
this.finalized = false;
}
register(suites: TestGroup[]) {
this.suites = suites;
}
update(path: string[], result: TestResult) {
this.results.push({ path, ...result });
}
finalize() {
this.finalized = true;
}
}
export interface Stream {
writeln(s: string): void;
}