fix(test): stabilize watch-node shutdown tests

This commit is contained in:
Vincent Koc
2026-06-01 14:50:41 +02:00
parent 671909d6d3
commit 408fa6e951
2 changed files with 23 additions and 18 deletions

View File

@@ -352,6 +352,9 @@ export async function runWatchMain(params = {}) {
});
watchProcess.on("exit", (exitCode, exitSignal) => {
watchProcess = null;
if (settled) {
return;
}
if (settleIfShuttingDown()) {
return;
}
@@ -422,6 +425,9 @@ export async function runWatchMain(params = {}) {
});
watchProcess.on("exit", (exitCode, exitSignal) => {
watchProcess = null;
if (settled) {
return;
}
if (settleIfShuttingDown()) {
return;
}

View File

@@ -34,10 +34,19 @@ const createFakeProcess = () =>
execPath: "/usr/local/bin/node",
}) as unknown as NodeJS.Process;
const createWatchHarness = () => {
const createKillableChild = () => {
const child = Object.assign(new EventEmitter(), {
kill: vi.fn(() => {}),
kill: vi.fn(),
});
child.kill.mockImplementation((signal: NodeJS.Signals = "SIGTERM") => {
queueMicrotask(() => child.emit("exit", null, signal));
return true;
});
return child;
};
const createWatchHarness = () => {
const child = createKillableChild();
const spawn = vi.fn(() => child);
const watcher = Object.assign(new EventEmitter(), {
close: vi.fn(async () => {}),
@@ -220,9 +229,7 @@ describe("watch-node script", () => {
});
it("starts the runner before loading chokidar", async () => {
const child = Object.assign(new EventEmitter(), {
kill: vi.fn(() => {}),
});
const child = createKillableChild();
const spawn = vi.fn(() => child);
const watcher = Object.assign(new EventEmitter(), {
close: vi.fn(async () => {}),
@@ -329,7 +336,7 @@ describe("watch-node script", () => {
it("runs doctor once and restarts when gateway exits nonzero", async () => {
const gatewayA = Object.assign(new EventEmitter(), { kill: vi.fn() });
const doctor = Object.assign(new EventEmitter(), { kill: vi.fn() });
const gatewayB = Object.assign(new EventEmitter(), { kill: vi.fn() });
const gatewayB = createKillableChild();
const spawn = vi
.fn()
.mockReturnValueOnce(gatewayA)
@@ -395,9 +402,7 @@ describe("watch-node script", () => {
const childA = Object.assign(new EventEmitter(), {
kill: vi.fn(),
});
const childB = Object.assign(new EventEmitter(), {
kill: vi.fn(() => {}),
});
const childB = createKillableChild();
const spawn = vi.fn().mockReturnValueOnce(childA).mockReturnValueOnce(childB);
const { watcher, fakeProcess, runPromise } = startWatchRun({ spawn });
@@ -447,9 +452,7 @@ describe("watch-node script", () => {
const childA = createAutoExitChild();
const childB = createAutoExitChild();
const childC = createAutoExitChild();
const childD = Object.assign(new EventEmitter(), {
kill: vi.fn(() => {}),
});
const childD = createKillableChild();
const spawn = vi
.fn()
.mockReturnValueOnce(childA)
@@ -538,9 +541,7 @@ describe("watch-node script", () => {
),
{ code: "ERR_INVALID_PACKAGE_CONFIG" },
);
const child = Object.assign(new EventEmitter(), {
kill: vi.fn(() => {}),
});
const child = createKillableChild();
const spawn = vi.fn(() => child);
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
@@ -583,9 +584,7 @@ describe("watch-node script", () => {
const error = Object.assign(new Error("Cannot find package 'chokidar'"), {
code: "ERR_MODULE_NOT_FOUND",
});
const child = Object.assign(new EventEmitter(), {
kill: vi.fn(() => {}),
});
const child = createKillableChild();
const spawn = vi.fn(() => child);
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});