From ddab45bba267a30fa0d3023f8f7a638872e92b0a Mon Sep 17 00:00:00 2001 From: Robert Gruen Date: Tue, 3 Nov 2026 00:02:24 -0700 Subject: [PATCH] test: discover TypeScript test suites automatically Keep the existing build and test-compilation stages, then use quoted native recursive globs for compiled JavaScript and runtime MJS suites. Regressions prove new flat and nested suites execute, helpers and raw TypeScript are ignored, and new test failures propagate. Document the supported test locations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- typescript/README.md | 13 +++- typescript/package.json | 2 +- typescript/tests/test-discovery.test.mjs | 92 ++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 typescript/tests/test-discovery.test.mjs diff --git a/typescript/README.md b/typescript/README.md index defb1fcd..d8a50ef9 100644 --- a/typescript/README.md +++ b/typescript/README.md @@ -36,8 +36,17 @@ npm install npm run build ``` -Run the test suite with `npm test`. The Zod schema tests also compile their generated -TypeScript schema text to check for syntax errors and unresolved type references. +Run the test suite with `npm test`. It builds the library, compiles TypeScript tests, +and recursively discovers compiled `*.test.js` suites under `out/` and runtime +`*.test.mjs` suites under `tests/`. Add TypeScript tests under +[test/](https://github.com/microsoft/TypeChat/tree/main/typescript/test) or runtime +tests under [tests/](https://github.com/microsoft/TypeChat/tree/main/typescript/tests); +no manual suite-list update is needed. +Quoted patterns let Node handle discovery consistently across platforms, including +nested directories, without executing helper files or uncompiled TypeScript. + +The Zod schema tests also compile their generated TypeScript schema text to check +for syntax errors and unresolved type references. Node.js CI runs this suite across its Node.js matrix after a clean dependency installation and workspace build. diff --git a/typescript/package.json b/typescript/package.json index 26f0daee..f9640e3b 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -21,7 +21,7 @@ }, "scripts": { "build": "tsc -p src", - "test": "npm run build && tsc -p test && node --test out/validate.test.js out/zod.test.js out/program.test.js out/failure.test.js out/metadata.test.js out/interactive.test.js out/interactive-stdin.test.js tests/model.test.mjs tests/examples.test.mjs tests/package.test.mjs", + "test": "npm run build && tsc -p test && node --test \"out/**/*.test.js\" \"tests/**/*.test.mjs\"", "build-all": "npm run build && npm run build --workspaces", "prepare": "npm run build-all", "prepublishOnly": "node -e \"require('fs').copyFileSync('../SECURITY.md','SECURITY.md')\"", diff --git a/typescript/tests/test-discovery.test.mjs b/typescript/tests/test-discovery.test.mjs new file mode 100644 index 00000000..0420251d --- /dev/null +++ b/typescript/tests/test-discovery.test.mjs @@ -0,0 +1,92 @@ +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import test from "node:test"; + +const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); + +function writeFixtureFile(root, relativePath, contents) { + const file = path.join(root, relativePath); + mkdirSync(path.dirname(file), { recursive: true }); + writeFileSync(file, contents); +} + +function runTestCommand(root) { + const manifest = JSON.parse(readFileSync(path.join(packageRoot, "package.json"), "utf8")); + const command = manifest.scripts.test.split(/\s*&&\s*/).at(-1); + assert.match(command, /^node --test(?:\s|$)/); + const args = [...command.matchAll(/"([^"]*)"|(\S+)/g)].slice(1) + .map(match => match[1] ?? match[2]); + const env = { ...process.env }; + // The nested runner needs its own reporting context. + delete env.NODE_TEST_CONTEXT; + const result = spawnSync(process.execPath, args, { + cwd: root, + env, + encoding: "utf8", + timeout: 30_000, + }); + assert.ifError(result.error); + return result; +} + +test("test command discovers new flat and nested compiled and runtime suites", () => { + const root = mkdtempSync(path.join(os.tmpdir(), "typechat-test-discovery-")); + const suites = [ + "out/unlisted.test.js", + "out/nested/unlisted.test.js", + "tests/unlisted.test.mjs", + "tests/nested/unlisted.test.mjs", + ]; + try { + writeFixtureFile(root, "package.json", JSON.stringify({ private: true, type: "commonjs" })); + for (const [index, suite] of suites.entries()) { + const marker = path.join(root, `executed-${index}`); + const imports = suite.endsWith(".mjs") + ? 'import test from "node:test"; import { writeFileSync } from "node:fs";' + : 'const test = require("node:test"); const { writeFileSync } = require("node:fs");'; + writeFixtureFile(root, suite, `${imports} +test(${JSON.stringify(suite)}, () => { + writeFileSync(${JSON.stringify(marker)}, "executed"); +}); +`); + } + for (const helper of ["out/helper.js", "tests/helper.mjs", "test/uncompiled.test.ts"]) { + writeFixtureFile(root, helper, 'throw new Error("Non-suite files must not run");'); + } + + const result = runTestCommand(root); + assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); + for (const index of suites.keys()) { + assert.equal(readFileSync(path.join(root, `executed-${index}`), "utf8"), "executed"); + } + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +test("test command propagates a newly discovered suite failure", () => { + const root = mkdtempSync(path.join(os.tmpdir(), "typechat-test-discovery-")); + try { + writeFixtureFile(root, "package.json", JSON.stringify({ private: true, type: "commonjs" })); + writeFixtureFile(root, "out/nested/unlisted.test.js", ` +require("node:test")("newly discovered suite failure", () => { + throw new Error("new suite regression"); +}); +`); + writeFixtureFile(root, "tests/unlisted.test.mjs", ` +import test from "node:test"; +test("new runtime suite", () => {}); +`); + + const result = runTestCommand(root); + assert.equal(result.status, 1, `${result.stdout}\n${result.stderr}`); + assert.match(result.stdout, /newly discovered suite failure/); + assert.match(result.stdout, /new suite regression/); + } finally { + rmSync(root, { recursive: true, force: true }); + } +});