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: 11 additions & 2 deletions typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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')\"",
Expand Down
92 changes: 92 additions & 0 deletions typescript/tests/test-discovery.test.mjs
Original file line number Diff line number Diff line change
@@ -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 });
}
});
Loading