-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli-api-error.test.ts
More file actions
41 lines (35 loc) · 1.31 KB
/
cli-api-error.test.ts
File metadata and controls
41 lines (35 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Command } from "commander"
import { afterAll, expect, it, vi } from "vitest"
import { OpenSeaAPIError } from "../src/client.js"
vi.mock("../src/commands/index.js", () => ({
accountsCommand: () => new Command("accounts"),
collectionsCommand: () => new Command("collections"),
eventsCommand: () => new Command("events"),
listingsCommand: () => new Command("listings"),
nftsCommand: () => new Command("nfts"),
offersCommand: () => new Command("offers"),
searchCommand: () => new Command("search"),
swapsCommand: () => new Command("swaps"),
tokensCommand: () => new Command("tokens"),
}))
const exitSpy = vi
.spyOn(process, "exit")
.mockImplementation(() => undefined as never)
const stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {})
vi.spyOn(Command.prototype, "parseAsync").mockRejectedValue(
new OpenSeaAPIError(404, "Not Found", "/api/v2/missing"),
)
afterAll(() => {
vi.restoreAllMocks()
})
it("exits with code 1 and 'API Error' label on non-429 API error", async () => {
await import("../src/cli.js")
await vi.waitFor(() => {
expect(exitSpy).toHaveBeenCalled()
})
expect(exitSpy).toHaveBeenCalledWith(1)
const output = stderrSpy.mock.calls[0][0] as string
const parsed = JSON.parse(output)
expect(parsed.error).toBe("API Error")
expect(parsed.status).toBe(404)
})