-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathbump.test.ts
More file actions
580 lines (488 loc) · 25.6 KB
/
bump.test.ts
File metadata and controls
580 lines (488 loc) · 25.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import { describe, expect, it, afterEach, jest } from '@jest/globals';
import path from 'path';
import { generateChangeFiles, getChangeFiles } from '../__fixtures__/changeFiles';
import { readChangelogJson, readChangelogMd } from '../__fixtures__/changelog';
import { initMockLogs } from '../__fixtures__/mockLogs';
import { type RepoFixture, RepositoryFactory } from '../__fixtures__/repositoryFactory';
import { bump } from '../commands/bump';
import { getPackageInfos } from '../monorepo/getPackageInfos';
import type { HooksOptions, ParsedOptions, RepoOptions } from '../types/BeachballOptions';
import type { Repository } from '../__fixtures__/repository';
import type { PackageJson } from '../types/PackageInfo';
import { getParsedOptions } from '../options/getOptions';
import { defaultRemoteBranchName } from '../__fixtures__/gitDefaults';
import { readJson } from '../object/readJson';
import { validate } from '../validation/validate';
import { createCommandContext } from '../monorepo/createCommandContext';
import type { CommandContext } from '../types/CommandContext';
import type { BumpInfo } from '../types/BumpInfo';
import { deepFreeze } from '../__fixtures__/object';
import { catalogsToYaml, type Catalogs } from 'workspace-tools';
//
// These tests use git repos and are slow, so besides a few basic scenarios, this file should
// only contain cases that can't be realistically tested in memory only.
// Most scenarios for version bumps should be tested in bumpInMemory.test.ts instead.
//
describe('bump command', () => {
let repositoryFactory: RepositoryFactory | undefined;
let repo: Repository | undefined;
initMockLogs();
/**
* Get options. Defaults to the repository root as cwd.
* Defaults to `fetch: false` since fetching is rarely relevant for these tests and is slow.
*/
function getOptions(repoOptions?: Partial<RepoOptions>, cwd?: string) {
const parsedOptions = getParsedOptions({
cwd: cwd || repo?.rootPath || '',
argv: ['node', 'beachball', 'bump'],
testRepoOptions: { branch: defaultRemoteBranchName, fetch: false, ...repoOptions },
});
return { options: parsedOptions.options, parsedOptions };
}
/**
* For more realistic testing, call `validate()` like the CLI command does, then call `bump()`.
* This helps catch any new issues with double bumps or context mutation.
* @returns the context containing the bump info
*/
async function bumpWrapper(parsedOptions: ParsedOptions) {
// This does an initial bump
const { context } = validate(parsedOptions, { checkDependencies: true });
// Ensure the later bump process does not modify the context
deepFreeze(context);
await bump(parsedOptions.options, context);
return context as CommandContext & { bumpInfo: BumpInfo };
}
afterEach(() => {
if (repositoryFactory) {
repositoryFactory.cleanUp();
repositoryFactory = undefined;
}
repo = undefined;
});
it('bumps only packages with change files with bumpDeps: false', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
'pkg-1': { version: '1.0.0' },
'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': '1.0.0' } },
'pkg-3': { version: '1.0.0', dependencies: { 'pkg-2': '1.0.0' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: false,
});
const comment = 'test comment for pkg-1';
generateChangeFiles([{ packageName: 'pkg-1', comment, type: 'minor' }], options);
repo.push();
const { bumpInfo } = await bumpWrapper(parsedOptions);
// Only pkg-1 actually gets bumped
expect(bumpInfo?.calculatedChangeTypes).toEqual({ 'pkg-1': 'minor' });
// Currently, pkg-2 ends up included due to https://github.com/microsoft/beachball/issues/1123
expect(bumpInfo?.modifiedPackages).toEqual(new Set(['pkg-1', 'pkg-2']));
expect(bumpInfo?.dependentChangedBy).toEqual({ 'pkg-2': new Set(['pkg-1']) });
const packageInfos = getPackageInfos(parsedOptions);
const pkg1NewVersion = '1.1.0';
expect(packageInfos['pkg-1'].version).toBe(pkg1NewVersion);
expect(packageInfos['pkg-2'].version).toBe('1.0.0');
expect(packageInfos['pkg-3'].version).toBe('1.0.0');
expect(packageInfos['pkg-2'].dependencies!['pkg-1']).toBe(pkg1NewVersion);
expect(packageInfos['pkg-3'].dependencies!['pkg-2']).toBe(monorepo['packages']['pkg-2'].version);
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(0);
const pkg1Changelog = readChangelogJson(repo.pathTo('packages/pkg-1'));
expect(pkg1Changelog!.entries[0].comments.minor![0].comment).toBe(comment);
// There's a check in writeChangeFiles to prevent writing changelogs for packages in
// dependentChangedBy with no changeType
const pkg2Changelog = readChangelogJson(repo.pathTo('packages/pkg-2'));
expect(pkg2Changelog).toBeNull();
});
it('for multi-root monorepo, only bumps packages in the current root', async () => {
repositoryFactory = new RepositoryFactory('multi-project');
expect(Object.keys(repositoryFactory.fixtures)).toEqual(['project-a', 'project-b']);
repo = repositoryFactory.cloneRepository();
const projectARoot = repo.pathTo('project-a');
const projectBRoot = repo.pathTo('project-b');
const infoA = getOptions({ bumpDeps: true }, projectARoot);
const optionsA = infoA.options;
const infoB = getOptions({ bumpDeps: true }, projectBRoot);
const optionsB = infoB.options;
generateChangeFiles([{ packageName: '@project-a/foo' }], optionsA);
generateChangeFiles([{ packageName: '@project-a/foo', type: 'major' }], optionsB);
repo.push();
await bumpWrapper(infoA.parsedOptions);
const packageInfosA = getPackageInfos(infoA.parsedOptions);
const packageInfosB = getPackageInfos(infoB.parsedOptions);
expect(packageInfosA['@project-a/foo'].version).toBe('1.1.0');
expect(packageInfosB['@project-b/foo'].version).toBe('1.0.0');
const changeFilesA = getChangeFiles(optionsA);
const changeFilesB = getChangeFiles(optionsB);
expect(changeFilesA).toHaveLength(0);
expect(changeFilesB).toHaveLength(1);
});
// This is mostly covered by readChangeFiles and unlinkChangeFiles, but it might be good to
// ensure it works all-up.
it('bumps only packages with change files committed between specified ref and head using since/fromRef flag', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
'pkg-1': { version: '1.0.0' },
'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': '1.0.0' } },
'pkg-3': { version: '1.0.0' },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: false,
// Incidentally use this to verify generateChangelog: false is respected
generateChangelog: false,
});
// generate an initial set of change files
generateChangeFiles(['pkg-1'], options);
// set the initial change files commit as fromRef
options.fromRef = repo.getCurrentHash();
// generate a new set of change files
generateChangeFiles(['pkg-3'], options);
repo.push();
const { bumpInfo, originalPackageInfos } = await bumpWrapper(parsedOptions);
expect(bumpInfo.calculatedChangeTypes).toEqual({ 'pkg-3': 'minor' });
expect(bumpInfo.modifiedPackages).toEqual(new Set(['pkg-3']));
const packageInfos = getPackageInfos(parsedOptions);
expect(packageInfos['pkg-1']).toEqual(originalPackageInfos['pkg-1']);
expect(packageInfos['pkg-2']).toEqual(originalPackageInfos['pkg-2']);
expect(packageInfos['pkg-3'].version).toBe('1.1.0');
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
// Verify generateChangelog: false
expect(readChangelogJson(repo.pathTo('packages/pkg-3'))).toBeNull();
expect(readChangelogMd(repo.pathTo('packages/pkg-3'))).toBeNull();
});
it('bumps all dependent packages with bumpDeps: true (default)', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
'pkg-1': { version: '1.0.0' },
// check all dependency types
'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': '1.0.0' } },
'pkg-3': { version: '1.0.0', devDependencies: { 'pkg-2': '1.0.0' } },
'pkg-4': { version: '1.0.0', peerDependencies: { 'pkg-3': '1.0.0' } },
'pkg-5': { version: '1.0.0', optionalDependencies: { 'pkg-4': '1.0.0' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
generateChangelog: true,
});
const comment = 'test comment for pkg-1';
generateChangeFiles([{ packageName: 'pkg-1', type: 'minor', comment }], options);
repo.push();
const { bumpInfo } = await bumpWrapper(parsedOptions);
expect(bumpInfo.modifiedPackages).toEqual(new Set(['pkg-1', 'pkg-2', 'pkg-3', 'pkg-4', 'pkg-5']));
const packageInfos = getPackageInfos(parsedOptions);
const pkg1NewVersion = '1.1.0';
const dependentNewVersion = '1.0.1';
expect(packageInfos['pkg-1'].version).toBe(pkg1NewVersion);
expect(packageInfos['pkg-2'].version).toBe(dependentNewVersion);
expect(packageInfos['pkg-3'].version).toBe(dependentNewVersion);
expect(packageInfos['pkg-2'].dependencies!['pkg-1']).toBe(pkg1NewVersion);
expect(packageInfos['pkg-3'].devDependencies!['pkg-2']).toBe(dependentNewVersion);
expect(packageInfos['pkg-4'].peerDependencies!['pkg-3']).toBe(dependentNewVersion);
expect(packageInfos['pkg-5'].optionalDependencies!['pkg-4']).toBe(dependentNewVersion);
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(0);
const pkg1Changelog = readChangelogJson(repo.pathTo('packages/pkg-1'));
expect(pkg1Changelog!.entries[0].comments.minor![0].comment).toBe(comment);
const pkg2Changelog = readChangelogJson(repo.pathTo('packages/pkg-2'));
expect(pkg2Changelog!.entries[0].comments.patch![0].comment).toBe(`Bump pkg-1 to v${pkg1NewVersion}`);
const pkg3Changelog = readChangelogJson(repo.pathTo('packages/pkg-3'));
expect(pkg3Changelog!.entries[0].comments.patch![0].comment).toBe(`Bump pkg-2 to v${dependentNewVersion}`);
});
// Most grouped package scenarios are covered in bumpInMemory.test.ts.
// Test this complicated scenario E2E too to verify all the pieces work together.
it('bumps all grouped AND dependent packages', async () => {
const monorepo: RepoFixture['folders'] = {
'packages/grp': {
// the test helper only handles one nesting level
'pkg-1': { version: '1.0.0' },
'pkg-2': { version: '1.0.0' },
'pkg-3': { version: '1.0.0', dependencies: { commonlib: '1.0.0' } },
},
packages: {
app: { version: '1.0.0', dependencies: { 'pkg-1': '1.0.0' } },
commonlib: { version: '1.0.0' },
unrelated: { version: '1.0.0' },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
groups: [{ include: 'packages/grp/*', name: 'grp', disallowedChangeTypes: [] }],
bumpDeps: true,
});
// Bump commonlib, which is not in the group, but triggers a dependent bump of pkg-3,
// which triggers bump of the whole group and then the app.
// Also verify the non-default dependentChangeType passes through.
generateChangeFiles([{ packageName: 'commonlib', dependentChangeType: 'minor' }], options);
repo.push();
const { originalPackageInfos } = await bumpWrapper(parsedOptions);
// This scenario is also covered in bumpInMemory, so focus on the filesystem parts
const packageInfos = getPackageInfos(parsedOptions);
const groupNewVersion = '1.1.0';
expect(packageInfos['pkg-1'].version).toBe(groupNewVersion);
expect(packageInfos['pkg-2'].version).toBe(groupNewVersion);
expect(packageInfos['pkg-3'].version).toBe(groupNewVersion);
expect(packageInfos['commonlib'].version).toBe('1.1.0');
expect(packageInfos['app'].version).toBe('1.1.0');
expect(packageInfos['unrelated']).toEqual(originalPackageInfos['unrelated']);
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(0);
// Current behavior: group bumps don't generate changelog entries
// (not sure if this is good or bad)
expect(readChangelogJson(repo.pathTo('packages/grp/pkg-1'))).toBeNull();
expect(readChangelogJson(repo.pathTo('packages/grp/pkg-2'))).toBeNull();
// The original dependent bump gets an entry
const pkg3Changelog = readChangelogJson(repo.pathTo('packages/grp/pkg-3'));
expect(pkg3Changelog!.entries[0].comments.minor![0].comment).toBe('Bump commonlib to v1.1.0');
// As does bumping pkg-1 in app
const appChangelog = readChangelogJson(repo.pathTo('packages/app'));
expect(appChangelog!.entries[0].comments.minor![0].comment).toBe('Bump pkg-1 to v1.1.0');
});
// Scope filtering of changes happens in readChangesFiles, not the actual bump logic,
// so we need an E2E test to make sure it all works together.
// (Scope filtering of dependents happens in the bump step.)
it('should not bump out-of-scope package even if package has change', async () => {
repositoryFactory = new RepositoryFactory('monorepo');
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
scope: ['!packages/bar'],
});
// bar depends on baz, so that gives bar an extra chance to get a dependent bump
generateChangeFiles(['bar', 'baz'], options);
repo.push();
const { bumpInfo, originalPackageInfos } = await bumpWrapper(parsedOptions);
// Verify the in-memory part of the bump
expect(bumpInfo.calculatedChangeTypes).toEqual({ baz: 'minor' });
expect(bumpInfo.modifiedPackages).toEqual(new Set(['baz']));
expect(bumpInfo.dependentChangedBy).toEqual({});
expect(bumpInfo.scopedPackages).toEqual(new Set(['baz', 'foo', 'a', 'b']));
expect(bumpInfo.changeFileChangeInfos).toHaveLength(1);
expect(bumpInfo.changeFileChangeInfos[0].change.packageName).toBe('baz');
const packageInfos = getPackageInfos(parsedOptions);
expect(packageInfos['bar']).toEqual(originalPackageInfos['bar']);
expect(packageInfos['foo']).toEqual(originalPackageInfos['foo']);
expect(packageInfos['baz'].version).toBe('1.4.0');
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(1);
expect(readChangelogJson(repo.pathTo('packages/bar'))).toBeNull();
expect(readChangelogJson(repo.pathTo('packages/baz'))).not.toBeNull();
});
// Scope filtering of dependents currently happens in the bump step and can be tested in bumpInMemory,
// but probably also good to have E2E coverage of this scenario in case that changes in the future.
it('should not bump out-of-scope package and its dependencies even if dependency of the package has change', async () => {
repositoryFactory = new RepositoryFactory('monorepo');
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
scope: ['!packages/foo'],
});
generateChangeFiles([{ packageName: 'bar', type: 'patch' }], options);
repo.push();
// bumpInMemory already tests the in-memory part of this scenario
const { originalPackageInfos } = await bumpWrapper(parsedOptions);
const packageInfos = getPackageInfos(parsedOptions);
expect(packageInfos['bar'].version).toBe('1.3.5');
// Since foo is out of scope, currently its dep on bar is not bumped.
// This is usually fine, but could be an issue if bar is bumped to an incompatible version.
// Somewhat related: https://github.com/microsoft/beachball/issues/620#issuecomment-3609264966
expect(packageInfos['foo']).toEqual(originalPackageInfos['foo']);
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(0);
expect(readChangelogJson(repo.pathTo('packages/bar'))).not.toBeNull();
expect(readChangelogJson(repo.pathTo('packages/foo'))).toBeNull();
});
// This is mostly covered by bumpInMemory.test.ts, but the current changelog behavior is probably
// worth documenting here.
it('bumps dependents with file: deps', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
'pkg-1': { version: '1.0.0' },
'pkg-2': { version: '0.0.0', dependencies: { 'pkg-1': 'file:../pkg-1' } },
'pkg-3': { version: '0.0.0', devDependencies: { 'pkg-2': 'file:../pkg-2' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
generateChangelog: true,
});
generateChangeFiles([{ packageName: 'pkg-1', type: 'minor' }], options);
repo.push();
const { originalPackageInfos } = await bumpWrapper(parsedOptions);
const packageInfos = getPackageInfos(parsedOptions);
// All the packages are bumped despite the file: dep specs.
// The dep specs are not modified, but the dependent versions are bumped.
expect(packageInfos['pkg-1']).toEqual({ ...originalPackageInfos['pkg-1'], version: '1.1.0' });
expect(packageInfos['pkg-2']).toEqual({ ...originalPackageInfos['pkg-2'], version: '0.0.1' });
expect(packageInfos['pkg-3']).toEqual({ ...originalPackageInfos['pkg-3'], version: '0.0.1' });
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(0);
// Current behavior: dependentChangedBy misses file: deps, so pkg-2 and pkg-3 don't have
// changelog entries for the pkg-1 bump.
// https://github.com/microsoft/beachball/issues/981
const changelogJson2 = readChangelogJson(repo.pathTo('packages/pkg-2'));
expect(changelogJson2).toBeNull();
const changelogJson3 = readChangelogJson(repo.pathTo('packages/pkg-3'));
expect(changelogJson3).toBeNull();
});
// Prerelease scenarios are covered in more detail in bumpInMemory.test.ts
it('bumps to prerelease and uses prerelease version for dependents', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
'pkg-1': { version: '1.0.0' },
'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': '1.0.0' } },
'pkg-3': { version: '1.0.0', devDependencies: { 'pkg-2': '1.0.0' } },
'pkg-4': { version: '1.0.0', peerDependencies: { 'pkg-3': '1.0.0' } },
'pkg-5': { version: '1.0.0', optionalDependencies: { 'pkg-4': '1.0.0' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
prereleasePrefix: 'beta',
});
generateChangeFiles([{ packageName: 'pkg-1', type: 'prerelease', dependentChangeType: 'prerelease' }], options);
repo.push();
await bumpWrapper(parsedOptions);
const packageInfos = getPackageInfos(parsedOptions);
const newVersion = '1.0.1-beta.0';
expect(packageInfos['pkg-1'].version).toBe(newVersion);
expect(packageInfos['pkg-2'].version).toBe(newVersion);
expect(packageInfos['pkg-3'].version).toBe(newVersion);
expect(packageInfos['pkg-4'].version).toBe(newVersion);
expect(packageInfos['pkg-2'].dependencies!['pkg-1']).toBe(newVersion);
expect(packageInfos['pkg-3'].devDependencies!['pkg-2']).toBe(newVersion);
expect(packageInfos['pkg-4'].peerDependencies!['pkg-3']).toBe(newVersion);
expect(packageInfos['pkg-5'].optionalDependencies!['pkg-4']).toBe(newVersion);
const changeFiles = getChangeFiles(options);
expect(changeFiles).toHaveLength(0);
});
it('bumps dependents with workspace: deps', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
// Include some external deps to make sure nothing weird happens there
'pkg-1': { version: '1.0.0', dependencies: { extra: '~1.2.3' } },
'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': 'workspace:~' } },
// this workspace version will be updated
'pkg-3': { version: '1.0.0', dependencies: { 'pkg-2': 'workspace:^1.0.0', other: 'npm:lodash' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
});
generateChangeFiles([{ packageName: 'pkg-1', type: 'minor' }], options);
repo.push();
// The bumpInfo object is covered by the similar test in bumpInMemory.test.ts
const { originalPackageInfos } = await bumpWrapper(parsedOptions);
const packageInfos = getPackageInfos(parsedOptions);
// All the dependent packages are bumped despite the workspace: dep specs
expect(packageInfos['pkg-1']).toEqual({ ...originalPackageInfos['pkg-1'], version: '1.1.0' });
// workspace:~ range isn't changed
expect(packageInfos['pkg-2']).toEqual({ ...originalPackageInfos['pkg-2'], version: '1.0.1' });
// workspace: range with number is updated
expect(packageInfos['pkg-3']).toEqual({
...originalPackageInfos['pkg-3'],
version: '1.0.1',
dependencies: { 'pkg-2': 'workspace:^1.0.1', other: 'npm:lodash' },
});
expect(readChangelogJson(repo.pathTo('packages/pkg-1'))).not.toBeNull();
const pkg3Changelog = readChangelogJson(repo.pathTo('packages/pkg-3'));
// this gets a changelog entry since the workspace:^1.0.0 dep was updated
// (a little debatable whether the current text is correct)
expect(pkg3Changelog!.entries[0].comments.patch![0].comment).toBe('Bump pkg-2 to v1.0.1');
// Current behavior: dependentChangedBy misses deps like workspace:~ that don't change,
// so the bump of pkg-1 will be missing from pkg-2's changelog.
// https://github.com/microsoft/beachball/issues/981
expect(readChangelogJson(repo.pathTo('packages/pkg-2'))).toBeNull();
});
it('bumps dependents with catalog: deps', async () => {
const monorepo: RepoFixture['folders'] = {
packages: {
'pkg-1': { version: '1.0.0' },
'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': 'catalog:' } },
'pkg-3': { version: '1.0.0', dependencies: { 'pkg-2': 'catalog:' } },
},
};
const catalogs: Catalogs = {
default: { 'pkg-1': 'workspace:~', 'pkg-2': 'workspace:^1.0.0' },
};
repositoryFactory = new RepositoryFactory({
folders: monorepo,
// This isn't currently read by bump() but should be present for completeness
extraFiles: { '.yarnrc.yml': catalogsToYaml(catalogs) },
});
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: true,
});
generateChangeFiles([{ packageName: 'pkg-1', type: 'minor' }], options);
repo.push();
// The bumpInfo object is covered by the similar test in bumpInMemory.test.ts
const { originalPackageInfos } = await bumpWrapper(parsedOptions);
const packageInfos = getPackageInfos(parsedOptions);
// All the dependent packages are bumped despite the catalog: dep specs
expect(packageInfos['pkg-1'].version).toBe('1.1.0');
// catalog: ranges aren't changed
expect(packageInfos['pkg-2']).toEqual({ ...originalPackageInfos['pkg-2'], version: '1.0.1' });
expect(packageInfos['pkg-3']).toEqual({ ...originalPackageInfos['pkg-3'], version: '1.0.1' });
expect(readChangelogJson(repo.pathTo('packages/pkg-1'))).not.toBeNull();
// Current behavior: dependentChangedBy misses catalog: deps, so there are no changelog entries
// https://github.com/microsoft/beachball/issues/981
expect(readChangelogJson(repo.pathTo('packages/pkg-2'))).toBeNull();
expect(readChangelogJson(repo.pathTo('packages/pkg-3'))).toBeNull();
});
// Explicit tests for sync/async hooks aren't necessary, especially since these are slow tests.
// Async is slightly trickier, so test that.
it('calls prebump/postbump hooks', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
repo = repositoryFactory.cloneRepository();
const { options, parsedOptions } = getOptions({
bumpDeps: false,
hooks: {
prebump: jest.fn<NonNullable<HooksOptions['prebump']>>(async (packagePath, name, version) => {
expect(packagePath.endsWith('pkg-1')).toBeTruthy();
expect(name).toBe('pkg-1');
// This is currently wrong--it should still be the old version
// https://github.com/microsoft/beachball/issues/1116
expect(version).toBe('1.1.0');
await new Promise(resolve => setTimeout(resolve, 0)); // simulate async work
const jsonPath = path.join(packagePath, 'package.json');
expect(readJson<PackageJson>(jsonPath).version).toBe('1.0.0');
}),
postbump: jest.fn<NonNullable<HooksOptions['postbump']>>(async (packagePath, name, version) => {
expect(packagePath.endsWith('pkg-1')).toBeTruthy();
expect(name).toBe('pkg-1');
expect(version).toBe('1.1.0');
await new Promise(resolve => setTimeout(resolve, 0)); // simulate async work
const jsonPath = path.join(packagePath, 'package.json');
expect(readJson<PackageJson>(jsonPath).version).toBe('1.1.0');
}),
},
});
generateChangeFiles(['pkg-1'], options);
repo.push();
// Skip validate for this test
await bump(options, createCommandContext(parsedOptions));
expect(options.hooks?.prebump).toHaveBeenCalled();
expect(options.hooks?.postbump).toHaveBeenCalled();
});
});