|
| 1 | +import { describe, it, expect, afterEach, jest, beforeAll, afterAll } from '@jest/globals'; |
| 2 | +import { setDependentVersions } from '../../bump/setDependentVersions'; |
| 3 | +import { makePackageInfos, type PartialPackageInfos } from '../../__fixtures__/packageInfos'; |
| 4 | +import { consideredDependencies } from '../../types/PackageInfo'; |
| 5 | + |
| 6 | +type PartialBumpInfo = Parameters<typeof setDependentVersions>[0]; |
| 7 | + |
| 8 | +describe('setDependentVersions', () => { |
| 9 | + let consoleLogSpy: jest.SpiedFunction<typeof console.log>; |
| 10 | + |
| 11 | + /** |
| 12 | + * Make the bump info. Package versions should reflect any bumps applied. |
| 13 | + * |
| 14 | + * Unless otherwise specified, assumes all packages are in scope and have been modified. |
| 15 | + * Realistically this would have been determined based on change types and `dependentChangeTypes`. |
| 16 | + */ |
| 17 | + function makeBumpInfo( |
| 18 | + params: { packageInfos: PartialPackageInfos } & Partial<Omit<PartialBumpInfo, 'packageInfos'>> |
| 19 | + ): PartialBumpInfo { |
| 20 | + const { packageInfos, ...rest } = params; |
| 21 | + return { |
| 22 | + packageInfos: makePackageInfos(packageInfos), |
| 23 | + scopedPackages: new Set(Object.keys(packageInfos)), |
| 24 | + modifiedPackages: new Set(Object.keys(packageInfos)), |
| 25 | + ...rest, |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + beforeAll(() => { |
| 30 | + consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined); |
| 31 | + jest.spyOn(console, 'info').mockImplementation(() => undefined); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + afterAll(() => { |
| 39 | + jest.restoreAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns empty object when no packages are in scope', () => { |
| 43 | + const bumpInfo = makeBumpInfo({ |
| 44 | + packageInfos: { 'pkg-a': {} }, |
| 45 | + scopedPackages: new Set(), |
| 46 | + // not sure if this would be included if out of scope, but check in case |
| 47 | + modifiedPackages: new Set(['pkg-a']), |
| 48 | + }); |
| 49 | + |
| 50 | + const result = setDependentVersions(bumpInfo, {}); |
| 51 | + |
| 52 | + expect(result).toEqual({}); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns empty object when no packages are modified', () => { |
| 56 | + const bumpInfo = makeBumpInfo({ |
| 57 | + packageInfos: { 'pkg-a': {} }, |
| 58 | + modifiedPackages: new Set(), |
| 59 | + }); |
| 60 | + |
| 61 | + const result = setDependentVersions(bumpInfo, {}); |
| 62 | + |
| 63 | + expect(result).toEqual({}); |
| 64 | + }); |
| 65 | + |
| 66 | + it('bumps dependency version range when dependency is bumped to unsatisfied range', () => { |
| 67 | + const bumpInfo = makeBumpInfo({ |
| 68 | + packageInfos: { |
| 69 | + // assume this had change type major |
| 70 | + 'pkg-a': { version: '2.0.0' }, |
| 71 | + // and this would be bumped for dependentChangeType patch |
| 72 | + 'pkg-b': { version: '1.0.1', dependencies: { 'pkg-a': '^1.0.0' } }, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + const result = setDependentVersions(bumpInfo, {}); |
| 77 | + |
| 78 | + expect(bumpInfo.packageInfos['pkg-b'].dependencies!['pkg-a']).toBe('^2.0.0'); |
| 79 | + expect(result).toEqual({ 'pkg-b': new Set(['pkg-a']) }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('bumps dependency range even if already satisfied', () => { |
| 83 | + const bumpInfo = makeBumpInfo({ |
| 84 | + packageInfos: { |
| 85 | + // assume this had change type minor |
| 86 | + 'pkg-a': { version: '1.1.0' }, |
| 87 | + // and this would be bumped for dependentChangeType patch |
| 88 | + 'pkg-b': { version: '1.0.1', dependencies: { 'pkg-a': '^1.0.0' } }, |
| 89 | + 'pkg-c': { version: '1.0.1', dependencies: { 'pkg-a': '~1.0.0' } }, |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + const result = setDependentVersions(bumpInfo, {}); |
| 94 | + |
| 95 | + expect(bumpInfo.packageInfos['pkg-b'].dependencies!['pkg-a']).toBe('^1.1.0'); |
| 96 | + expect(bumpInfo.packageInfos['pkg-c'].dependencies!['pkg-a']).toBe('~1.1.0'); |
| 97 | + expect(result).toEqual({ 'pkg-b': new Set(['pkg-a']), 'pkg-c': new Set(['pkg-a']) }); |
| 98 | + }); |
| 99 | + |
| 100 | + it.each(consideredDependencies)('handles %s', depType => { |
| 101 | + const bumpInfo = makeBumpInfo({ |
| 102 | + packageInfos: { |
| 103 | + 'pkg-a': { version: '1.1.0' }, |
| 104 | + 'pkg-b': { version: '1.0.1', [depType]: { 'pkg-a': '^1.0.0' } }, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + const result = setDependentVersions(bumpInfo, {}); |
| 109 | + |
| 110 | + expect(bumpInfo.packageInfos['pkg-b'][depType]!['pkg-a']).toBe('^1.1.0'); |
| 111 | + expect(result).toEqual({ 'pkg-b': new Set(['pkg-a']) }); |
| 112 | + |
| 113 | + // doesn't log by default |
| 114 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('handles (ignores) external dependencies', () => { |
| 118 | + const bumpInfo = makeBumpInfo({ |
| 119 | + packageInfos: { |
| 120 | + 'pkg-a': { version: '1.0.0', dependencies: { external: '^1.0.0' } }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + const result = setDependentVersions(bumpInfo, {}); |
| 125 | + |
| 126 | + expect(bumpInfo.packageInfos['pkg-a'].dependencies!['external']).toBe('^1.0.0'); |
| 127 | + expect(result).toEqual({}); |
| 128 | + }); |
| 129 | + |
| 130 | + it('handles multiple dependencies being bumped', () => { |
| 131 | + const bumpInfo = makeBumpInfo({ |
| 132 | + packageInfos: { |
| 133 | + 'pkg-a': { version: '1.0.1' }, |
| 134 | + 'pkg-b': { version: '1.1.0' }, |
| 135 | + 'pkg-c': { version: '1.0.1', dependencies: { 'pkg-a': '^1.0.0' }, peerDependencies: { 'pkg-b': '^1.0.0' } }, |
| 136 | + }, |
| 137 | + }); |
| 138 | + |
| 139 | + const result = setDependentVersions(bumpInfo, {}); |
| 140 | + |
| 141 | + expect(bumpInfo.packageInfos['pkg-c'].dependencies!['pkg-a']).toBe('^1.0.1'); |
| 142 | + expect(bumpInfo.packageInfos['pkg-c'].peerDependencies!['pkg-b']).toBe('^1.1.0'); |
| 143 | + expect(result).toEqual({ 'pkg-c': new Set(['pkg-a', 'pkg-b']) }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('logs when verbose is true', () => { |
| 147 | + const bumpInfo = makeBumpInfo({ |
| 148 | + packageInfos: { |
| 149 | + 'pkg-a': { version: '2.0.0' }, |
| 150 | + 'pkg-b': { version: '1.0.1', dependencies: { 'pkg-a': '^1.0.0' } }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + setDependentVersions(bumpInfo, { verbose: true }); |
| 155 | + |
| 156 | + expect(consoleLogSpy).toHaveBeenCalledWith('pkg-b needs to be bumped because pkg-a ^1.0.0 -> ^2.0.0'); |
| 157 | + }); |
| 158 | + |
| 159 | + // Documenting this issue |
| 160 | + // https://github.com/microsoft/beachball/issues/981 |
| 161 | + it('currently misses bumps of workspace: ranges', () => { |
| 162 | + const bumpInfo = makeBumpInfo({ |
| 163 | + packageInfos: { |
| 164 | + 'pkg-a': { version: '1.1.0' }, |
| 165 | + // * means an exact dependency and should definitely cause a bump |
| 166 | + 'pkg-b': { version: '1.0.1', dependencies: { 'pkg-a': 'workspace:*' } }, |
| 167 | + }, |
| 168 | + }); |
| 169 | + |
| 170 | + const result = setDependentVersions(bumpInfo, {}); |
| 171 | + |
| 172 | + expect(bumpInfo.packageInfos['pkg-b'].dependencies!['pkg-a']).toBe('workspace:*'); |
| 173 | + expect(result).toEqual({}); // should have pkg-b depending on pkg-a |
| 174 | + }); |
| 175 | +}); |
0 commit comments