|
| 1 | +import chalk from "chalk"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { runCommand } from "./runCommand.js"; |
| 5 | + |
| 6 | +const mockExecaCommand = vi.fn().mockRejectedValue("Oh no!"); |
| 7 | + |
| 8 | +vi.mock("execa", () => ({ |
| 9 | + get execaCommand() { |
| 10 | + return mockExecaCommand; |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +const mockLogLine = vi.fn(); |
| 15 | + |
| 16 | +vi.mock("../../shared/cli/lines.js", () => ({ |
| 17 | + get logLine() { |
| 18 | + return mockLogLine; |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("../../shared/cli/spinners.js", () => ({ |
| 23 | + withSpinner: vi.fn((callback: () => unknown) => callback()), |
| 24 | +})); |
| 25 | + |
| 26 | +describe("runCommand", () => { |
| 27 | + it("does not log when the command succeeds", async () => { |
| 28 | + mockExecaCommand.mockResolvedValue(undefined); |
| 29 | + |
| 30 | + await runCommand("command", "label"); |
| 31 | + |
| 32 | + expect(mockLogLine).not.toHaveBeenCalled(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("logs when the command failures", async () => { |
| 36 | + mockExecaCommand.mockRejectedValue(new Error("Oh no!")); |
| 37 | + |
| 38 | + await runCommand("command", "label"); |
| 39 | + |
| 40 | + expect(mockLogLine).toHaveBeenCalledWith( |
| 41 | + [ |
| 42 | + chalk.yellow(`⚠️ Running \``), |
| 43 | + chalk.yellowBright("command"), |
| 44 | + chalk.yellow(`\` failed. You should run it and fix its complaints.`), |
| 45 | + ].join("") |
| 46 | + ); |
| 47 | + }); |
| 48 | +}); |
0 commit comments