-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.test.ts
205 lines (161 loc) · 5.64 KB
/
index.test.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import chalk from "chalk";
import { beforeEach, describe, expect, it, vi } from "vitest";
import z from "zod";
import { bin } from "./index.js";
import { getVersionFromPackageJson } from "./packageJson.js";
const mockCancel = vi.fn();
const mockOutro = vi.fn();
vi.mock("@clack/prompts", () => ({
get cancel() {
return mockCancel;
},
intro: vi.fn(),
log: {
info: vi.fn(),
},
get outro() {
return mockOutro;
},
}));
const mockLogLine = vi.fn();
vi.mock("../shared/cli/lines.js", () => ({
get logLine() {
return mockLogLine;
},
}));
const mockCreate = vi.fn();
vi.mock("../create/index.js", () => ({
get create() {
return mockCreate;
},
}));
const mockInitialize = vi.fn();
vi.mock("../initialize/index.js", () => ({
get initialize() {
return mockInitialize;
},
}));
const mockMigrate = vi.fn();
vi.mock("../migrate/index.js", () => ({
get migrate() {
return mockMigrate;
},
}));
const mockPromptForMode = vi.fn();
vi.mock("./promptForMode.js", () => ({
get promptForMode() {
return mockPromptForMode;
},
}));
describe("bin", () => {
beforeEach(() => {
vi.spyOn(console, "clear").mockImplementation(() => undefined);
vi.spyOn(console, "log").mockImplementation(() => undefined);
});
it("returns 1 when promptForMode returns an undefined mode", async () => {
mockPromptForMode.mockResolvedValue({});
const result = await bin([]);
expect(mockOutro).toHaveBeenCalledWith(
chalk.red("Operation cancelled. Exiting - maybe another time? 👋"),
);
expect(result).toBe(1);
});
it("returns 1 when promptForMode returns an error mode", async () => {
const error = new Error("Oh no!");
mockPromptForMode.mockResolvedValue({ mode: error });
const result = await bin([]);
expect(mockOutro).toHaveBeenCalledWith(chalk.red(error.message));
expect(result).toBe(1);
});
it("returns the success result of the corresponding runner without cancel logging when promptForMode returns a mode that succeeds", async () => {
const mode = "create";
const args = ["--owner", "abc123"];
const code = 0;
const promptedOptions = { directory: "." };
mockPromptForMode.mockResolvedValue({ mode, options: promptedOptions });
mockCreate.mockResolvedValue({ code, options: {} });
const result = await bin(args);
expect(mockCreate).toHaveBeenCalledWith(args, promptedOptions);
expect(mockCancel).not.toHaveBeenCalled();
expect(mockInitialize).not.toHaveBeenCalled();
expect(mockMigrate).not.toHaveBeenCalled();
expect(result).toEqual(code);
});
it("returns the cancel result of the corresponding runner and cancel logs when promptForMode returns a mode that cancels", async () => {
const mode = "create";
const args = ["--owner", "abc123"];
const code = 2;
const promptedOptions = { directory: "." };
mockPromptForMode.mockResolvedValue({ mode, options: promptedOptions });
mockCreate.mockResolvedValue({ code, options: {} });
const result = await bin(args);
expect(mockCreate).toHaveBeenCalledWith(args, promptedOptions);
expect(mockCancel).toHaveBeenCalledWith(
`Operation cancelled. Exiting - maybe another time? 👋`,
);
expect(result).toEqual(code);
});
it("returns the cancel result containing a zod error of the corresponding runner and output plus cancel logs when promptForMode returns a mode that cancels with a string error", async () => {
const mode = "initialize";
const args = ["--email", "abc123"];
const code = 2;
const error = "Oh no!";
mockPromptForMode.mockResolvedValue({ mode });
mockInitialize.mockResolvedValue({
code: 2,
error,
options: {},
});
const result = await bin(args);
expect(mockInitialize).toHaveBeenCalledWith(args, undefined);
expect(mockLogLine).toHaveBeenCalledWith(chalk.red(error));
expect(mockCancel).toHaveBeenCalledWith(
`Operation cancelled. Exiting - maybe another time? 👋`,
);
expect(result).toEqual(code);
});
it("returns the cancel result containing a zod error of the corresponding runner and output plus cancel logs when promptForMode returns a mode that cancels with a zod error", async () => {
const mode = "initialize";
const args = ["--email", "abc123"];
const code = 2;
const validationResult = z
.object({ email: z.string().email() })
.safeParse({ email: "abc123" });
mockPromptForMode.mockResolvedValue({ mode });
mockInitialize.mockResolvedValue({
code: 2,
error: (validationResult as z.SafeParseError<{ email: string }>).error,
options: {},
});
const result = await bin(args);
expect(mockInitialize).toHaveBeenCalledWith(args, undefined);
expect(mockLogLine).toHaveBeenCalledWith(
chalk.red('Validation error: Invalid email at "email"'),
);
expect(mockCancel).toHaveBeenCalledWith(
`Operation cancelled. Exiting - maybe another time? 👋`,
);
expect(result).toEqual(code);
});
it("returns the cancel result of the corresponding runner and cancel logs when promptForMode returns a mode that fails", async () => {
const mode = "create";
const args = ["--owner", "abc123"];
const code = 1;
const promptedOptions = { directory: "." };
mockPromptForMode.mockResolvedValue({ mode, options: promptedOptions });
mockCreate.mockResolvedValue({ code, options: {} });
const result = await bin(args);
expect(mockCreate).toHaveBeenCalledWith(args, promptedOptions);
expect(mockCancel).toHaveBeenCalledWith(
`Operation failed. Exiting - maybe another time? 👋`,
);
expect(result).toEqual(code);
});
it("prints the version when the --version flag is passed", async () => {
const args = ["--version"];
const version = await getVersionFromPackageJson();
const result = await bin(args);
expect(console.log).toHaveBeenCalledWith(version);
expect(result).toBe(0);
});
});