|
1 | 1 | import { TestBaseCli, getConsoleMock } from "@clerc/test-utils"; |
2 | | -import { afterAll, afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { NoSuchCommandError, friendlyErrorPlugin } from "clerc"; |
| 3 | +import * as kons from "kons"; |
| 4 | +import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; |
3 | 5 | import { mockConsole } from "vitest-console"; |
4 | 6 |
|
5 | 7 | import { helpPlugin } from "../src"; |
6 | 8 |
|
| 9 | +vi.mock("kons", () => ({ |
| 10 | + error: vi.fn(), |
| 11 | +})); |
| 12 | + |
7 | 13 | describe("plugin-help", () => { |
| 14 | + vi.spyOn(process, "exit").mockImplementation(() => ({}) as never); |
| 15 | + const spy = vi.spyOn(kons, "error").mockImplementation(() => {}); |
| 16 | + vi.mocked(kons.error).mockImplementation(() => {}); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + spy.mockClear(); |
| 20 | + }); |
| 21 | + |
8 | 22 | const { clearConsole, restoreConsole } = mockConsole({ quiet: true }); |
9 | 23 |
|
10 | 24 | afterEach(clearConsole); |
@@ -182,4 +196,25 @@ describe("plugin-help", () => { |
182 | 196 | expect(getConsoleMock("log").mock.calls).toMatchSnapshot(); |
183 | 197 | }); |
184 | 198 | }); |
| 199 | + |
| 200 | + it("should throw error when command not found", async () => { |
| 201 | + await expect(async () => { |
| 202 | + await TestBaseCli().use(helpPlugin()).parse(["not-exist", "--help"]); |
| 203 | + }).rejects.toThrow(NoSuchCommandError); |
| 204 | + |
| 205 | + await expect(async () => { |
| 206 | + await TestBaseCli().use(helpPlugin()).parse(["help", "not-exist"]); |
| 207 | + }).rejects.toThrow(NoSuchCommandError); |
| 208 | + }); |
| 209 | + |
| 210 | + it("should work with friendly-error", async () => { |
| 211 | + expect(async () => { |
| 212 | + await TestBaseCli() |
| 213 | + .use(helpPlugin()) |
| 214 | + .use(friendlyErrorPlugin()) |
| 215 | + .parse(["not-exist", "--help"]); |
| 216 | + |
| 217 | + expect(spy.mock.calls).toMatchSnapshot(); |
| 218 | + }).not.toThrow(); |
| 219 | + }); |
185 | 220 | }); |
0 commit comments