-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathcli.test.ts
167 lines (141 loc) · 5.05 KB
/
cli.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
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[]) {
return ['/mock/path', '/mock/entry/path', ...args];
}
/**
* Returns the parsed `yargs.Arguments` object for a given package name and
* description.
*
* @param name - The package name.
* @param description - The package description.
* @returns The parsed argv object.
*/
function getParsedArgv(name: string, description: string) {
return {
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
_: [],
$0: 'create-package',
name: `@metamask/${name}`,
description,
};
}
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) => {
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', ' '), 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({
tsConfig: {},
tsConfigBuild: {},
nodeVersions: '>=18.0.0',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
jest.spyOn(utils, 'finalizeAndWriteData').mockResolvedValue();
expect(
await cli(
getMockArgv('--name', 'foo', '--description', 'bar'),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar')),
);
});
it('should handle names already prefixed with "@metamask/"', async () => {
const defaultCommand = commandMap.$0;
jest.spyOn(defaultCommand, 'handler');
jest.spyOn(utils, 'readMonorepoFiles').mockResolvedValue({
tsConfig: {},
tsConfigBuild: {},
nodeVersions: '>=18.0.0',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
jest.spyOn(utils, 'finalizeAndWriteData').mockResolvedValue();
expect(
await cli(
getMockArgv('--name', '@metamask/foo', '--description', 'bar'),
commands,
),
).toBeUndefined();
expect(defaultCommand.handler).toHaveBeenCalledTimes(1);
expect(defaultCommand.handler).toHaveBeenCalledWith(
expect.objectContaining(getParsedArgv('foo', 'bar')),
);
});
it('should create a new package', 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')),
);
});
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'), 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'), commands)).rejects.toThrow(
'exit: 1',
);
expect(console.error).toHaveBeenCalledWith(
'Missing required argument: "description"',
);
});
});
});