|
| 1 | +import { describe, it, expect, vi, afterAll } from "vitest"; |
| 2 | +import consola from "consola"; |
| 3 | +import { |
| 4 | + createMain, |
| 5 | + defineCommand, |
| 6 | + renderUsage, |
| 7 | + runMain, |
| 8 | + showUsage, |
| 9 | +} from "../src"; |
| 10 | +import * as mainModule from "../src/main"; |
| 11 | +import * as commandModule from "../src/command"; |
| 12 | + |
| 13 | +describe("runMain", () => { |
| 14 | + vi.spyOn(process, "exit").mockImplementation(() => 0 as never); |
| 15 | + |
| 16 | + const consoleMock = vi |
| 17 | + .spyOn(consola, "log") |
| 18 | + .mockImplementation(() => undefined); |
| 19 | + |
| 20 | + const consolaErrorMock = vi |
| 21 | + .spyOn(consola, "error") |
| 22 | + .mockImplementation(() => undefined); |
| 23 | + |
| 24 | + afterAll(() => { |
| 25 | + consoleMock.mockReset(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("shows version with flag `--version`", async () => { |
| 29 | + const command = defineCommand({ |
| 30 | + meta: { |
| 31 | + version: "1.0.0", |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + await runMain(command, { rawArgs: ["--version"] }); |
| 36 | + |
| 37 | + expect(consoleMock).toHaveBeenCalledWith("1.0.0"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("shows version with flag `--version` with meta specified as async function", async () => { |
| 41 | + const command = defineCommand({ |
| 42 | + // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject, require-await |
| 43 | + meta: async () => Promise.resolve({ version: "1.0.0" }), |
| 44 | + }); |
| 45 | + |
| 46 | + await runMain(command, { rawArgs: ["--version"] }); |
| 47 | + |
| 48 | + expect(consoleMock).toHaveBeenCalledWith("1.0.0"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("shows usage with flag `--version` but without version specified", async () => { |
| 52 | + const command = defineCommand({ |
| 53 | + meta: { |
| 54 | + name: "test", |
| 55 | + description: "Test command", |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + await runMain(command, { rawArgs: ["--version"] }); |
| 60 | + |
| 61 | + const usage = await renderUsage(command); |
| 62 | + expect(consoleMock).toHaveBeenCalledWith(usage + "\n"); |
| 63 | + expect(consolaErrorMock).toHaveBeenCalledWith("No version specified"); |
| 64 | + }); |
| 65 | + |
| 66 | + it.each([["--help"], ["-h"]])("shows usage with flag `%s`", async (flag) => { |
| 67 | + const command = defineCommand({ |
| 68 | + meta: { |
| 69 | + name: "test", |
| 70 | + description: "Test command", |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + await runMain(command, { rawArgs: [flag] }); |
| 75 | + |
| 76 | + const usage = await renderUsage(command); |
| 77 | + expect(consoleMock).toHaveBeenCalledWith(usage + "\n"); |
| 78 | + }); |
| 79 | + |
| 80 | + it.each([["--help"], ["-h"]])( |
| 81 | + "can show custom usage with flag `%s`", |
| 82 | + async (flag) => { |
| 83 | + const command = defineCommand({ |
| 84 | + meta: { |
| 85 | + name: "test", |
| 86 | + description: "Test command", |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + // eslint-disable-next-line require-await, unicorn/consistent-function-scoping |
| 91 | + const customUsage: typeof showUsage = async () => { |
| 92 | + consola.log("Custom usage"); |
| 93 | + }; |
| 94 | + |
| 95 | + await runMain(command, { rawArgs: [flag], showUsage: customUsage }); |
| 96 | + |
| 97 | + expect(consoleMock).toHaveBeenCalledWith("Custom usage"); |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + it("runs the command", async () => { |
| 102 | + const mockRunCommand = vi.spyOn(commandModule, "runCommand"); |
| 103 | + |
| 104 | + const command = defineCommand({}); |
| 105 | + |
| 106 | + await runMain(command); |
| 107 | + |
| 108 | + expect(mockRunCommand).toHaveBeenCalledWith(command, { rawArgs: [] }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("runs the command with raw arguments", async () => { |
| 112 | + const mockRunCommand = vi.spyOn(commandModule, "runCommand"); |
| 113 | + |
| 114 | + const command = defineCommand({}); |
| 115 | + |
| 116 | + const rawArgs = ["--foo", "bar"]; |
| 117 | + |
| 118 | + await runMain(command, { rawArgs }); |
| 119 | + |
| 120 | + expect(mockRunCommand).toHaveBeenCalledWith(command, { rawArgs }); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe("createMain", () => { |
| 125 | + it("creates and returns a function", () => { |
| 126 | + const main = createMain(defineCommand({})); |
| 127 | + |
| 128 | + expect(main).toBeInstanceOf(Function); |
| 129 | + }); |
| 130 | +}); |
0 commit comments