Repository navigation
Expand file tree
/
Copy pathcli.test.ts
More file actions
235 lines (200 loc) · 6.69 KB
/
Copy pathcli.test.ts
File metadata and controls
235 lines (200 loc) · 6.69 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import cli from './cli';
import { commands, commandMap } from './commands';
import * as utils from './utils';
jest.mock('./utils');
/**
* Returns a mock `process.argv` array with the provided arguments. Includes
* default values for `process.argv[0]` and `process.argv[1]`.
*
* @param args - The arguments to include in the mock argv array.
* @returns The mock argv array.
*/
function getMockArgv(...args: string[]): string[] {
return ['/mock/path', '/mock/entry/path', ...args];
}
/**
* Returns the parsed `yargs.Arguments` object for a given package name,
* description, and type.
*
* @param name - The package name.
* @param description - The package description.
* @param type - The package type.
* @returns The parsed argv object.
*/
function getParsedArgv(
name: string,
description: string,
type: string,
): {
_: [];
$0: 'create-package';
name: `@metamask/${string}`;
description: string;
type: string;
} {
return {
_: [],
$0: 'create-package',
name: `@metamask/${name}`,
description,
type,
};
}
describe('create-package/cli', () => {
beforeEach(() => {
// yargs calls process.exit() with 1 on failure and sometimes 0 on success.
// We have to intercept it.
jest
.spyOn(process, 'exit')
.mockImplementation((code?: number | string | null) => {
if (code === 1) {
throw new Error('exit: 1');
} else {
return undefined as never;
}
});
// We actually check these.
jest.spyOn(console, 'error');
jest.spyOn(console, 'log');
});
afterEach(() => {
delete process.exitCode;
});
it('should error if a string option contains only whitespace', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
await expect(
cli(getMockArgv('--name', ' ', '--type', 'snap'), commands),
).rejects.toThrow('exit: 1');
expect(console.error).toHaveBeenCalledWith(
'The argument "name" was processed to an empty string. Please provide a value with non-whitespace characters.',
);
});
describe('command: $0', () => {
it('should call the command handler with the correct arguments', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler');
jest.spyOn(utils, 'readMonorepoFiles').mockResolvedValue({
nodeVersions: '>=18.0.0',
});
jest.spyOn(utils, 'finalizeAndWriteData').mockResolvedValue();
expect(
await cli(
getMockArgv('--name', 'foo', '--description', 'bar', '--type', 'lib'),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar', 'library')),
);
});
it('should handle names already prefixed with "@metamask/"', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler');
jest.spyOn(utils, 'readMonorepoFiles').mockResolvedValue({
nodeVersions: '>=18.0.0',
});
jest.spyOn(utils, 'finalizeAndWriteData').mockResolvedValue();
expect(
await cli(
getMockArgv(
'--name',
'@metamask/foo',
'--description',
'bar',
'--type',
'snap',
),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar', 'snap')),
);
});
it('should normalize the "lib" shorthand to "library"', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
expect(
await cli(
getMockArgv('--name', 'foo', '--description', 'bar', '--type', 'lib'),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar', 'library')),
);
});
it('should create a new package', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
expect(
await cli(
getMockArgv(
'--name',
'foo',
'--description',
'bar',
'--type',
'library',
),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar', 'library')),
);
});
it('should error if the package name is missing', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
await expect(
cli(getMockArgv('--description', 'bar', '--type', 'snap'), commands),
).rejects.toThrow('exit: 1');
expect(console.error).toHaveBeenCalledWith(
'Missing required argument: "name"',
);
});
it('should error if the package description is missing', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
await expect(
cli(getMockArgv('--name', 'foo', '--type', 'snap'), commands),
).rejects.toThrow('exit: 1');
expect(console.error).toHaveBeenCalledWith(
'Missing required argument: "description"',
);
});
it('should default the package type to "snap"', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
expect(
await cli(
getMockArgv('--name', 'foo', '--description', 'bar'),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar', 'snap')),
);
});
it('should error if the package type is invalid', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler').mockImplementation();
await expect(
cli(
getMockArgv('--name', 'foo', '--description', 'bar', '--type', 'foo'),
commands,
),
).rejects.toThrow('exit: 1');
expect(console.error).toHaveBeenCalledWith(
'Invalid package type: "foo". Valid types are: "snap", "library", "lib".',
);
});
});
});