Skip to content

Commit db3aedb

Browse files
committed
fix: restore a working test-watch
Pointing test-watch straight at vitest was wrong: vitest runs the compiled output, so a .ts edit produces nothing for it to react to. The watch loop needs tsc re-emitting alongside it, which is what the old dev/tsc-to-mocha-watch.js was doing. dev/tsc-to-vitest-watch.js runs both, and is smaller than its predecessor because vitest reruns itself once the .js changes - it doesn't need to be driven the way mocha did. It passes --watch explicitly, since vitest only infers watch mode from a TTY and would otherwise run once and exit, taking tsc down with it. Also un-ignores dev/*.js, which .gitignore's blanket *.js rule would otherwise have kept out of the repo.
1 parent 6919a8a commit db3aedb

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ lib/common/test-reports.xml
8888
!lib/common/scripts/**
8989
config/test-deps-versions-generated.json
9090
!scripts/*.js
91+
!dev/*.js
92+
9193
# build output
9294
/dist

dev/tsc-to-vitest-watch.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Run "tsc" in watch mode alongside vitest. Vitest runs the compiled output,
2+
// so on its own it never notices a .ts edit - tsc has to re-emit first, and
3+
// vitest picks the change up from there.
4+
5+
const { spawn } = require("child_process");
6+
7+
// shell: true so the node_modules/.bin shims resolve on Windows as well
8+
const spawnOptions = { stdio: "inherit", shell: true };
9+
10+
const children = [
11+
// --preserveWatchOutput keeps tsc from clearing the screen and wiping the
12+
// test results out from under you on every recompile
13+
spawn("tsc", ["--watch", "--preserveWatchOutput"], spawnOptions),
14+
// --watch explicitly: vitest only infers watch mode when stdout is a TTY,
15+
// and would otherwise run once and exit, taking tsc down with it
16+
spawn("vitest", ["--watch"], spawnOptions),
17+
];
18+
19+
let shuttingDown = false;
20+
21+
function shutdown() {
22+
if (shuttingDown) {
23+
return;
24+
}
25+
shuttingDown = true;
26+
for (const child of children) {
27+
child.kill("SIGINT");
28+
}
29+
}
30+
31+
process.on("SIGINT", shutdown);
32+
process.on("SIGTERM", shutdown);
33+
34+
for (const child of children) {
35+
// if either side dies, don't leave the other running in the background
36+
child.on("exit", shutdown);
37+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"prepack": "node scripts/guard-root-pack.js",
2727
"docs-jekyll": "node scripts/build-docs.js",
2828
"tsc": "tsc",
29-
"test-watch": "vitest",
29+
"test-watch": "node ./dev/tsc-to-vitest-watch.js",
3030
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
3131
"prettier": "prettier --write ./lib/**/*{.ts,.d.ts} ./test/**/*{.ts,.d.ts}",
3232
"build.release": "npm run clean.build && tsc -p tsconfig.release.json && node scripts/generate-test-deps.js && node scripts/copy-assets.js --release && node scripts/set-ga-id.js live --dir dist && node scripts/set-ga-id.js verify --dir dist",

0 commit comments

Comments
 (0)