fix(KB-503): fix process.exit mock in project tests

- Add proper process.exit mock that throws error for testability
- All 6 project command tests now passing
This commit is contained in:
gsxdsm
2026-03-31 22:42:57 -07:00
parent 602cea0817
commit 2221f9a2b9
28 changed files with 223 additions and 59 deletions

View File

@@ -6,18 +6,15 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
describe("project commands", () => {
let consoleSpy: ReturnType<typeof vi.spyOn>;
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
let exitSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
exitSpy = vi.spyOn(process, "exit").mockImplementation((() => {}) as (code?: string | number | null) => never);
});
afterEach(() => {
consoleSpy.mockRestore();
consoleErrorSpy.mockRestore();
exitSpy.mockRestore();
vi.resetModules();
});
@@ -33,12 +30,50 @@ describe("project commands", () => {
});
});
describe("validation", () => {
it("runProjectAdd should require name and path", async () => {
describe("validation errors", () => {
it("runProjectAdd should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectAdd } = await import("./project.js");
// Test with empty name - should exit
await runProjectAdd("", "/tmp");
expect(exitSpy).toHaveBeenCalled();
await expect(runProjectAdd("", "/tmp")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
});
it("runProjectAdd should exit when path is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectAdd } = await import("./project.js");
await expect(runProjectAdd("name", "")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
});
it("runProjectRemove should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectRemove } = await import("./project.js");
await expect(runProjectRemove("")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
});
it("runProjectShow should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectShow } = await import("./project.js");
await expect(runProjectShow("")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
});
it("runProjectSetDefault should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectSetDefault } = await import("./project.js");
await expect(runProjectSetDefault("")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
});
});
});