diff --git a/test/cli/install/verdaccio-registry-lifecycle.test.ts b/test/cli/install/verdaccio-registry-lifecycle.test.ts new file mode 100644 index 000000000000..4433e9f53e6c --- /dev/null +++ b/test/cli/install/verdaccio-registry-lifecycle.test.ts @@ -0,0 +1,33 @@ +import { afterAll, expect, test } from "bun:test"; +import { VerdaccioRegistry } from "harness"; +import { once } from "node:events"; + +let registry: VerdaccioRegistry | undefined; + +afterAll(() => { + if (registry?.process && registry.process.exitCode === null && registry.process.signalCode === null) { + registry.process.kill("SIGKILL"); + } +}); + +// stop() previously called kill(0), which only probes for liveness and left the +// forked verdaccio running after every test file that used it. +test("VerdaccioRegistry.stop() terminates the forked process", async () => { + registry = new VerdaccioRegistry(); + await registry.start(); + + const child = registry.process!; + expect(child.pid).toBeGreaterThan(0); + expect(() => process.kill(child.pid!, 0)).not.toThrow(); + + const exited = once(child, "exit"); + registry.stop(); + + const result = await Promise.race([ + exited.then(() => "exited" as const), + Bun.sleep(3000).then(() => "still running" as const), + ]); + + expect(result).toBe("exited"); + expect(child.killed).toBe(true); +}); diff --git a/test/harness.ts b/test/harness.ts index 634f34e2e22e..80b0551a096a 100644 --- a/test/harness.ts +++ b/test/harness.ts @@ -1826,6 +1826,7 @@ export class VerdaccioRegistry { configPath: string; packagesPath: string; users: Record = {}; + #stopped = false; constructor(opts?: { configPath?: string; packagesPath?: string; verbose?: boolean }) { this.port = randomPort(); @@ -1834,6 +1835,7 @@ export class VerdaccioRegistry { } async start(silent: boolean = true) { + this.#stopped = false; await rm(join(dirname(this.configPath), "htpasswd"), { force: true }); this.process = fork(require.resolve("verdaccio/bin/verdaccio"), ["-c", this.configPath, "-l", `${this.port}`], { silent, @@ -1857,6 +1859,7 @@ export class VerdaccioRegistry { }); this.process.on("exit", (code, signal) => { + if (this.#stopped) return; if (code !== 0) { console.error(`Verdaccio exited with code ${code} and signal ${signal}`); } else { @@ -1878,8 +1881,9 @@ export class VerdaccioRegistry { } stop() { + this.#stopped = true; rmSync(join(dirname(this.configPath), "htpasswd"), { force: true }); - this.process?.kill(0); + this.process?.kill(); } /**