import { afterEach, describe, expect, it, vi } from "vitest"; import { readFileSync } from "node:fs"; import { VERSION } from "../src/index.js"; const packageVersion = ( JSON.parse( readFileSync(new URL("../package.json", import.meta.url), "utf-8"), ) as { version: string } ).version; class ExitCalled extends Error {} describe("version", () => { const argv = process.argv; afterEach(() => { process.argv = argv; vi.restoreAllMocks(); }); it("exports the version from package.json", () => { expect(VERSION).toBe(packageVersion); }); // Runs bin/quak.ts with --version. commander prints the version and then // calls process.exit, which is stubbed to throw so the test survives. it("reports the version from package.json in quak --version", async () => { const printed: string[] = []; vi.spyOn(process.stdout, "write").mockImplementation((chunk) => { printed.push(String(chunk)); return true; }); vi.spyOn(process, "exit").mockImplementation(() => { throw new ExitCalled(); }); process.argv = ["node", "quak", "--version"]; await expect(import("../bin/quak.js")).rejects.toBeInstanceOf( ExitCalled, ); expect(printed.join("").trim()).toBe(packageVersion); }); });