-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathchange.test.ts
334 lines (275 loc) · 12.2 KB
/
change.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { describe, expect, it, afterEach, jest, beforeEach } from '@jest/globals';
import fs from 'fs-extra';
import type prompts from 'prompts';
import { getChangeFiles } from '../__fixtures__/changeFiles';
import { initMockLogs } from '../__fixtures__/mockLogs';
import { RepoFixture, RepositoryFactory } from '../__fixtures__/repositoryFactory';
import { change } from '../commands/change';
import { BeachballOptions } from '../types/BeachballOptions';
import { defaultBranchName } from '../__fixtures__/gitDefaults';
import { MockStdout } from '../__fixtures__/mockStdout';
import { MockStdin } from '../__fixtures__/mockStdin';
import { ChangeFileInfo } from '../types/ChangeInfo';
import { Repository } from '../__fixtures__/repository';
import { getDefaultOptions } from '../options/getDefaultOptions';
// prompts writes to stdout (not console) in a way that can't really be mocked with spies,
// so instead we inject a custom mock stdout stream, as well as stdin for entering answers
let stdin: MockStdin;
let stdout: MockStdout;
jest.mock(
'prompts',
(): typeof prompts =>
((questions, options) => {
questions = Array.isArray(questions) ? questions : [questions];
questions = questions.map(q => ({ ...q, stdin, stdout }));
return (jest.requireActual('prompts') as typeof prompts)(questions, options);
}) as typeof prompts
);
/**
* Inject these options into `PackageInfo.combinedOptions` for every package to simulate a
* repo-wide config. (Actual repo-wide configs aren't usually read in tests because the current
* implementation depends on the actual cwd, not the temp repo directory.)
*/
let mockBeachballOptions: Partial<BeachballOptions> | undefined;
jest.mock('../options/getDefaultOptions', () => ({
getDefaultOptions: () => ({
...(jest.requireActual('../options/getDefaultOptions') as any).getDefaultOptions(),
...mockBeachballOptions,
}),
}));
/** Wait for the prompt to finish rendering (simulates real user input) */
const waitForPrompt = () => new Promise(resolve => process.nextTick(resolve));
const monorepo: RepoFixture['folders'] = {
packages: { 'pkg-1': { version: '1.0.0' }, 'pkg-2': { version: '1.0.0' }, 'pkg-3': { version: '1.0.0' } },
};
function makeMonorepoChanges(repo: Repository) {
repo.checkout('-b', 'test');
repo.stageChange('packages/pkg-1/file.js');
repo.commitAll('commit 1');
repo.stageChange('packages/pkg-2/file.js');
repo.commitAll('commit 2');
}
describe('change command', () => {
let repositoryFactory: RepositoryFactory | undefined;
let repo: Repository | undefined;
const logs = initMockLogs();
function getOptions(options?: Partial<BeachballOptions>): BeachballOptions {
return {
...getDefaultOptions(),
// change to ?. if a future test uses a non-standard repo
path: repo!.rootPath,
branch: defaultBranchName,
...options,
};
}
beforeEach(() => {
stdin = new MockStdin();
stdout = new MockStdout({ replace: 'prompts' });
});
afterEach(() => {
stdin.destroy();
stdout.destroy();
repositoryFactory?.cleanUp();
repositoryFactory = undefined;
repo = undefined;
mockBeachballOptions = undefined;
});
it('does not create change files when there are no changes', async () => {
repositoryFactory = new RepositoryFactory('single');
repo = repositoryFactory.cloneRepository();
const options = getOptions();
await change(options);
expect(getChangeFiles(options)).toHaveLength(0);
});
it('creates and stages a change file', async () => {
repositoryFactory = new RepositoryFactory('single');
repo = repositoryFactory.cloneRepository();
repo.checkout('-b', 'test');
repo.commitChange('file.js');
const options = getOptions({ commit: false });
const changePromise = change(options);
await waitForPrompt();
// Use default change type and custom message
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
await stdin.sendByChar('\n');
// Also verify that the options shown are correct
expect(stdout.lastOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) »
> "file.js""
`);
await stdin.sendByChar('stage me please\n');
await changePromise;
expect(repo.status()).toMatch(/^A change/);
expect(logs.mocks.log).toHaveBeenLastCalledWith(expect.stringMatching(/^git staged these change files:/));
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
expect(fs.readJSONSync(changeFiles[0])).toMatchObject({
comment: 'stage me please',
packageName: 'foo',
type: 'patch',
});
});
it('creates and commits a change file', async () => {
repositoryFactory = new RepositoryFactory('single');
repo = repositoryFactory.cloneRepository();
repo.checkout('-b', 'test');
repo.commitChange('file.js');
const options = getOptions();
const changePromise = change(options);
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
await stdin.sendByChar('\n'); // default change type
await stdin.sendByChar('commit me please\n'); // custom message
await changePromise;
expect(logs.mocks.log).toHaveBeenLastCalledWith(expect.stringMatching(/^git committed these change files:/));
expect(repo.status()).toBe('');
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
expect(fs.readJSONSync(changeFiles[0])).toMatchObject({
comment: 'commit me please',
packageName: 'foo',
type: 'patch',
});
});
it('creates and commits a change file with changeDir set', async () => {
const testChangedir = 'changeDir';
repositoryFactory = new RepositoryFactory('single');
repo = repositoryFactory.cloneRepository();
repo.checkout('-b', 'test');
repo.commitChange('file.js');
const options = getOptions({
changeDir: testChangedir,
});
const changePromise = change(options);
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
await stdin.sendByChar('\n'); // default change type
await stdin.sendByChar('commit me please\n'); // custom message
await changePromise;
expect(logs.mocks.log).toHaveBeenLastCalledWith(expect.stringMatching(/^git committed these change files:/));
expect(repo.status()).toBe('');
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
expect(fs.readJSONSync(changeFiles[0])).toMatchObject({
comment: 'commit me please',
packageName: 'foo',
type: 'patch',
});
});
it('creates a change file when there are no changes but package name is provided', async () => {
repositoryFactory = new RepositoryFactory('single');
repo = repositoryFactory.cloneRepository();
const options = getOptions({
package: repositoryFactory.fixture.rootPackage!.name,
commit: false,
});
const changePromise = change(options);
await waitForPrompt();
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
await stdin.sendByChar('\n'); // default change type
await stdin.sendByChar('stage me please\n'); // custom message
await changePromise;
expect(repo.status()).toMatch(/^A change/);
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
});
it('creates and commits change files for multiple packages', async () => {
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
makeMonorepoChanges(repo);
const options = getOptions();
const changePromise = change(options);
// use custom values for first package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: pkg-1 (currently v1.0.0)');
stdin.emitKey({ name: 'down' });
await stdin.sendByChar('\n');
// also verify that the options shown are correct
expect(stdout.lastOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) »
> commit 2
commit 1"
`);
await stdin.sendByChar('custom\n');
// use defaults for second package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: pkg-2 (currently v1.0.0)');
await stdin.sendByChar('\n\n');
await changePromise;
expect(logs.mocks.log).toHaveBeenLastCalledWith(expect.stringMatching(/^git committed these change files:/));
expect(repo.status()).toBe('');
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(2);
const changeFileContents = changeFiles.map(changeFile => fs.readJSONSync(changeFile)) as ChangeFileInfo[];
expect(changeFileContents).toContainEqual(
expect.objectContaining({ comment: 'custom', packageName: 'pkg-1', type: 'minor' })
);
expect(changeFileContents).toContainEqual(
expect.objectContaining({ comment: 'commit 2', packageName: 'pkg-2', type: 'patch' })
);
});
it('creates and commits grouped change file for multiple packages', async () => {
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
makeMonorepoChanges(repo);
const options = getOptions({
groupChanges: true,
});
const changePromise = change(options);
// use custom values for first package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: pkg-1 (currently v1.0.0)');
stdin.emitKey({ name: 'down' });
await stdin.sendByChar('\n');
await stdin.sendByChar('custom\n');
// use defaults for second package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: pkg-2 (currently v1.0.0)');
await stdin.sendByChar('\n\n');
await changePromise;
expect(logs.mocks.log).toHaveBeenLastCalledWith(expect.stringMatching(/^git committed these change files:/));
expect(repo.status()).toBe('');
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
const contents = fs.readJSONSync(changeFiles[0]);
expect(contents.changes).toEqual([
expect.objectContaining({ comment: 'custom', packageName: 'pkg-1', type: 'minor' }),
expect.objectContaining({ comment: 'commit 2', packageName: 'pkg-2', type: 'patch' }),
]);
});
it('uses custom per-package prompt', async () => {
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
makeMonorepoChanges(repo);
mockBeachballOptions = {
changeFilePrompt: {
changePrompt: (defaultPrompt, pkg) => {
const questions = [defaultPrompt.changeType!, defaultPrompt.description!];
return pkg === 'pkg-1'
? questions
: [{ type: 'text', name: 'custom', message: 'custom question' }, ...questions];
},
},
};
const options = getOptions({
groupChanges: true,
});
const changePromise = change(options);
await waitForPrompt();
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: pkg-1 (currently v1.0.0)');
expect(stdout.lastOutput()).toMatch(/Change type/);
await stdin.sendByChar('\n');
expect(stdout.lastOutput()).toMatch(/Describe changes/);
await stdin.sendByChar('\n');
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: pkg-2 (currently v1.0.0)');
expect(stdout.lastOutput()).toMatch(/custom question/);
await stdin.sendByChar('stuff\n');
expect(stdout.lastOutput()).toMatch(/Change type/);
await stdin.sendByChar('\n');
expect(stdout.lastOutput()).toMatch(/Describe changes/);
await stdin.sendByChar('\n');
await changePromise;
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
const contents = fs.readJSONSync(changeFiles[0]);
expect(contents.changes).toEqual([
expect.objectContaining({ packageName: 'pkg-1', type: 'patch', comment: 'commit 2' }),
expect.objectContaining({ packageName: 'pkg-2', type: 'patch', comment: 'commit 2', custom: 'stuff' }),
]);
});
// custom prompt for different packages (only truly doable here because elsewhere it uses combinedOptions)
});