|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import readline from "readline"; |
| 3 | +import { execSync } from "child_process"; |
| 4 | +import { askQuestion } from "."; |
| 5 | + |
| 6 | +vi.mock("child_process"); |
| 7 | + |
| 8 | +describe("askQuestion", () => { |
| 9 | + it("resolves with user input", async () => { |
| 10 | + const mockInterface = { |
| 11 | + question: vi.fn((_q: string, cb: (ans: string) => void) => cb("my-app")), |
| 12 | + close: vi.fn(), |
| 13 | + } as unknown as readline.Interface; |
| 14 | + |
| 15 | + vi.spyOn(readline, "createInterface").mockReturnValue(mockInterface); |
| 16 | + |
| 17 | + const answer = await askQuestion("Project name?"); |
| 18 | + expect(answer).toBe("my-app"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("resolves with empty string if input is empty", async () => { |
| 22 | + const mockInterface = { |
| 23 | + question: vi.fn((_q: string, cb: (ans: string) => void) => cb("")), |
| 24 | + close: vi.fn(), |
| 25 | + } as unknown as readline.Interface; |
| 26 | + |
| 27 | + vi.spyOn(readline, "createInterface").mockReturnValue(mockInterface); |
| 28 | + |
| 29 | + const answer = await askQuestion("Project name?"); |
| 30 | + expect(answer).toBe(""); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("error handling", () => { |
| 35 | + it("handles execSync errors gracefully", () => { |
| 36 | + (execSync as unknown as ReturnType<typeof vi.fn>).mockImplementation(() => { |
| 37 | + throw new Error("mock failure"); |
| 38 | + }); |
| 39 | + |
| 40 | + try { |
| 41 | + execSync("pnpm dlx create-turbo"); |
| 42 | + } catch (err: any) { |
| 43 | + expect(err.message).toContain("mock failure"); |
| 44 | + } |
| 45 | + }); |
| 46 | +}); |
0 commit comments