Skip to content
Closed
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
33 changes: 33 additions & 0 deletions test/cli/install/verdaccio-registry-lifecycle.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
6 changes: 5 additions & 1 deletion test/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,7 @@ export class VerdaccioRegistry {
configPath: string;
packagesPath: string;
users: Record<string, string> = {};
#stopped = false;

constructor(opts?: { configPath?: string; packagesPath?: string; verbose?: boolean }) {
this.port = randomPort();
Expand All @@ -1834,6 +1835,7 @@ export class VerdaccioRegistry {
}

async start(silent: boolean = true) {
this.#stopped = false;
Comment thread
robobun marked this conversation as resolved.
Resolved
await rm(join(dirname(this.configPath), "htpasswd"), { force: true });
this.process = fork(require.resolve("verdaccio/bin/verdaccio"), ["-c", this.configPath, "-l", `${this.port}`], {
silent,
Expand All @@ -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 {
Expand All @@ -1878,8 +1881,9 @@ export class VerdaccioRegistry {
}

stop() {
this.#stopped = true;
Comment thread
robobun marked this conversation as resolved.
Resolved
rmSync(join(dirname(this.configPath), "htpasswd"), { force: true });
this.process?.kill(0);
this.process?.kill();
}

/**
Expand Down
Loading