|
| 1 | +import { cmd, reportCmd } from "../cmd.js" |
| 2 | +import { exec } from "../exec-promise.js" |
| 3 | +import { report, exitWithError } from "../reporter.js" |
| 4 | + |
| 5 | +jest.mock("../exec-promise.js", () => ({ |
| 6 | + exec: jest.fn(), |
| 7 | +})) |
| 8 | +jest.mock("../reporter.js", () => ({ |
| 9 | + report: jest.fn(), |
| 10 | + exitWithError: jest.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +const baseConfig = { |
| 14 | + dryRun: false, |
| 15 | + verbose: false, |
| 16 | +} |
| 17 | + |
| 18 | +describe("cmd", () => { |
| 19 | + it("calls given command", async () => { |
| 20 | + // Given |
| 21 | + const command = "test" |
| 22 | + // When |
| 23 | + await cmd(command, baseConfig) |
| 24 | + // Then |
| 25 | + expect(exec).toBeCalledWith(command) |
| 26 | + }) |
| 27 | + |
| 28 | + it("exits with error if command fails", async () => { |
| 29 | + // Given |
| 30 | + exec.mockImplementation(() => { |
| 31 | + throw new Error("fail") |
| 32 | + }) |
| 33 | + const command = "test" |
| 34 | + // When |
| 35 | + await cmd(command, baseConfig) |
| 36 | + // Then |
| 37 | + expect(exitWithError).toBeCalledWith( |
| 38 | + expect.any(Object), |
| 39 | + `Unable to complete command: ${command}` |
| 40 | + ) |
| 41 | + }) |
| 42 | + |
| 43 | + describe("reports", () => { |
| 44 | + it("reports command if dry run and verbose", async () => { |
| 45 | + // Given |
| 46 | + const command = "test" |
| 47 | + // When |
| 48 | + await cmd(command, { dryRun: true, verbose: true }) |
| 49 | + // Then |
| 50 | + expect(report).toBeCalledWith( |
| 51 | + expect.objectContaining({ m: command, type: "info", indent: false }) |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it("reports command if verbose", async () => { |
| 56 | + // Given |
| 57 | + const command = "test" |
| 58 | + // When |
| 59 | + await cmd(command, { verbose: true }) |
| 60 | + // Then |
| 61 | + expect(report).toBeCalledWith( |
| 62 | + expect.objectContaining({ m: command, type: "info", indent: false }) |
| 63 | + ) |
| 64 | + }) |
| 65 | + }) |
| 66 | +}) |
| 67 | + |
| 68 | +describe("reportCmd", () => { |
| 69 | + describe("reports", () => { |
| 70 | + it("reports start", async () => { |
| 71 | + // Given |
| 72 | + const step = "Test" |
| 73 | + const command = "test" |
| 74 | + // When |
| 75 | + await reportCmd(command, { ...baseConfig, step }) |
| 76 | + // Then |
| 77 | + expect(report).toBeCalledWith( |
| 78 | + expect.objectContaining({ |
| 79 | + m: step, |
| 80 | + type: "start", |
| 81 | + indent: false, |
| 82 | + }) |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it("reports success", async () => { |
| 87 | + // Given |
| 88 | + const step = "Test" |
| 89 | + const command = "test" |
| 90 | + // When |
| 91 | + await reportCmd(command, { ...baseConfig, step }) |
| 92 | + // Then |
| 93 | + expect(report).toBeCalledWith( |
| 94 | + expect.objectContaining({ |
| 95 | + m: `${step} successful`, |
| 96 | + type: "succeed", |
| 97 | + indent: false, |
| 98 | + }) |
| 99 | + ) |
| 100 | + }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments