Skip to content

Commit 1bc1b94

Browse files
authored
Consistently use initMockLogs (#1174)
1 parent 817a0a3 commit 1bc1b94

File tree

7 files changed

+64
-106
lines changed

7 files changed

+64
-106
lines changed

src/__functional__/changelog/prepareChangelogPaths.test.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
import { afterEach, beforeAll, describe, expect, it, jest } from '@jest/globals';
1+
import { afterEach, describe, expect, it } from '@jest/globals';
22
import fs from 'fs';
33
import path from 'path';
44
import { removeTempDir } from '../../__fixtures__/tmpdir';
55
import { prepareChangelogPaths } from '../../changelog/prepareChangelogPaths';
66
import { createTestFileStructure } from '../../__fixtures__/createTestFileStructure';
7+
import { initMockLogs } from '../../__fixtures__/mockLogs';
78

89
describe('prepareChangelogPaths', () => {
9-
let consoleLogMock: jest.SpiedFunction<typeof console.log>;
10+
// there will be a bunch of ignorable warnings because /faketmpdir doesn't exist
11+
const logs = initMockLogs({ alsoLog: ['error'] });
1012
let tempDir: string | undefined;
1113

1214
const fakeDir = path.resolve('/faketmpdir');
1315
const packageName = 'test';
1416
/** This is the beginning of the md5 hash digest of "test" */
1517
const testHash = '098f6bcd';
1618

17-
beforeAll(() => {
18-
consoleLogMock = jest.spyOn(console, 'log').mockImplementation(() => {});
19-
// there will be a bunch of ignorable warnings because /faketmpdir doesn't exist
20-
jest.spyOn(console, 'warn').mockImplementation(() => {});
21-
});
22-
2319
afterEach(() => {
24-
consoleLogMock.mockClear();
2520
tempDir && removeTempDir(tempDir);
2621
tempDir = undefined;
2722
});
@@ -138,8 +133,8 @@ describe('prepareChangelogPaths', () => {
138133
expect(fs.existsSync(path.join(tempDir, 'CHANGELOG.json'))).toBe(false);
139134
expect(fs.readFileSync(paths.json!, 'utf8')).toBe('{}');
140135

141-
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringContaining('Renamed existing changelog file'));
142-
expect(consoleLogMock).toHaveBeenCalledTimes(2);
136+
expect(logs.mocks.log).toHaveBeenCalledWith(expect.stringContaining('Renamed existing changelog file'));
137+
expect(logs.mocks.log).toHaveBeenCalledTimes(2);
143138
});
144139

145140
it('migrates existing path with hash to non-hash path (uniqueFilenames true to false)', () => {
@@ -166,8 +161,8 @@ describe('prepareChangelogPaths', () => {
166161
expect(fs.existsSync(path.join(tempDir, `${oldName}.json`))).toBe(false);
167162
expect(fs.readFileSync(paths.json!, 'utf8')).toBe('{}');
168163

169-
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringContaining('Renamed existing changelog file'));
170-
expect(consoleLogMock).toHaveBeenCalledTimes(2);
164+
expect(logs.mocks.log).toHaveBeenCalledWith(expect.stringContaining('Renamed existing changelog file'));
165+
expect(logs.mocks.log).toHaveBeenCalledTimes(2);
171166
});
172167

173168
it('renames newest file if uniqueFilenames is true and there are multiple files with hashes', async () => {
@@ -198,8 +193,8 @@ describe('prepareChangelogPaths', () => {
198193
expect(fs.existsSync(path.join(tempDir, `CHANGELOG-${lastHash}.md`))).toBe(false);
199194
expect(fs.readFileSync(paths.md!, 'utf8')).toBe('last md');
200195

201-
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringContaining('Renamed existing changelog file'));
202-
expect(consoleLogMock).toHaveBeenCalledTimes(1);
196+
expect(logs.mocks.log).toHaveBeenCalledWith(expect.stringContaining('Renamed existing changelog file'));
197+
expect(logs.mocks.log).toHaveBeenCalledTimes(1);
203198

204199
// The other files are untouched
205200
expect(fs.readFileSync(path.join(tempDir, file1), 'utf8')).toBe('md 1');

src/__functional__/options/getOptions.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { describe, expect, it, beforeAll, afterAll, jest } from '@jest/globals';
1+
import { describe, expect, it, beforeAll, afterAll } from '@jest/globals';
22
import fs from 'fs';
33
import path from 'path';
44
import { RepositoryFactory } from '../../__fixtures__/repositoryFactory';
55
import { getOptions, getParsedOptions } from '../../options/getOptions';
66
import type { RepoOptions } from '../../types/BeachballOptions';
77
import { writeJson } from '../../object/writeJson';
8+
import { initMockLogs } from '../../__fixtures__/mockLogs';
89

910
describe('getOptions (deprecated)', () => {
11+
initMockLogs({ alsoLog: ['error', 'warn'] });
12+
1013
let repositoryFactory: RepositoryFactory;
1114
// Don't reuse a repo in these tests! If multiple tests load beachball.config.js from the same path,
1215
// it will use the version from the require cache, which will have outdated contents.
@@ -25,7 +28,6 @@ describe('getOptions (deprecated)', () => {
2528

2629
beforeAll(() => {
2730
repositoryFactory = new RepositoryFactory('single');
28-
jest.spyOn(console, 'log').mockImplementation(() => {});
2931
});
3032

3133
afterAll(() => {
@@ -109,7 +111,6 @@ describe('getParsedOptions', () => {
109111

110112
beforeAll(() => {
111113
repositoryFactory = new RepositoryFactory('single');
112-
jest.spyOn(console, 'log').mockImplementation(() => {});
113114
});
114115

115116
afterAll(() => {

src/__tests__/bump/setDependentVersions.test.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { describe, it, expect, afterEach, jest, beforeAll } from '@jest/globals';
1+
import { describe, it, expect } from '@jest/globals';
22
import { setDependentVersions } from '../../bump/setDependentVersions';
33
import { makePackageInfos, type PartialPackageInfos } from '../../__fixtures__/packageInfos';
44
import { consideredDependencies } from '../../types/PackageInfo';
5+
import { initMockLogs } from '../../__fixtures__/mockLogs';
56

67
type PartialBumpInfo = Parameters<typeof setDependentVersions>[0];
78

89
describe('setDependentVersions', () => {
9-
let consoleLogSpy: jest.SpiedFunction<typeof console.log>;
10+
const logs = initMockLogs({ alsoLog: ['warn', 'error'] });
1011

1112
/**
1213
* Make the bump info. Package versions should reflect any bumps applied.
@@ -26,15 +27,6 @@ describe('setDependentVersions', () => {
2627
};
2728
}
2829

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-
3830
it('returns empty object when no packages are in scope', () => {
3931
const bumpInfo = makeBumpInfo({
4032
packageInfos: { 'pkg-a': {} },
@@ -107,7 +99,7 @@ describe('setDependentVersions', () => {
10799
expect(result).toEqual({ 'pkg-b': new Set(['pkg-a']) });
108100

109101
// doesn't log by default
110-
expect(consoleLogSpy).not.toHaveBeenCalled();
102+
expect(logs.mocks.log).not.toHaveBeenCalled();
111103
});
112104

113105
it('handles (ignores) external dependencies', () => {
@@ -149,7 +141,7 @@ describe('setDependentVersions', () => {
149141

150142
setDependentVersions(bumpInfo, { verbose: true });
151143

152-
expect(consoleLogSpy).toHaveBeenCalledWith('pkg-b needs to be bumped because pkg-a ^1.0.0 -> ^2.0.0');
144+
expect(logs.mocks.log).toHaveBeenCalledWith('pkg-b needs to be bumped because pkg-a ^1.0.0 -> ^2.0.0');
153145
});
154146

155147
// Documenting this issue

src/__tests__/bump/updateLockFile.test.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { describe, it, expect, jest, afterEach, beforeAll, beforeEach } from '@jest/globals';
1+
import { describe, it, expect, jest, afterEach, beforeEach } from '@jest/globals';
22
import fs from 'fs';
33
import path from 'path';
44
import { updateLockFile } from '../../bump/updateLockFile';
55
import { packageManager, type PackageManagerResult } from '../../packageManager/packageManager';
6+
import { initMockLogs } from '../../__fixtures__/mockLogs';
67

78
jest.mock('fs');
89
jest.mock('../../packageManager/packageManager');
@@ -15,16 +16,10 @@ jest.mock('../../env', () => ({
1516
}));
1617

1718
describe('updateLockFile', () => {
19+
const logs = initMockLogs({ alsoLog: ['error'] });
1820
const mockRoot = path.resolve('/mock/root');
1921
const mockFs = fs as jest.Mocked<typeof fs>;
2022
const mockPackageManager = packageManager as jest.MockedFunction<typeof packageManager>;
21-
let consoleLogSpy: jest.SpiedFunction<typeof console.log>;
22-
let consoleWarnSpy: jest.SpiedFunction<typeof console.warn>;
23-
24-
beforeAll(() => {
25-
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined);
26-
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
27-
});
2823

2924
beforeEach(() => {
3025
mockPackageManager.mockResolvedValue({ success: true } as PackageManagerResult);
@@ -40,7 +35,7 @@ describe('updateLockFile', () => {
4035

4136
await updateLockFile({ path: mockRoot });
4237

43-
expect(consoleLogSpy).toHaveBeenCalledWith('Updating package-lock.json after bumping packages');
38+
expect(logs.mocks.log).toHaveBeenCalledWith('Updating package-lock.json after bumping packages');
4439
expect(mockPackageManager).toHaveBeenCalledWith('npm', ['install', '--package-lock-only', '--ignore-scripts'], {
4540
stdio: 'inherit',
4641
cwd: mockRoot,
@@ -52,7 +47,7 @@ describe('updateLockFile', () => {
5247

5348
await updateLockFile({ path: mockRoot });
5449

55-
expect(consoleLogSpy).toHaveBeenCalledWith('Updating pnpm-lock.yaml after bumping packages');
50+
expect(logs.mocks.log).toHaveBeenCalledWith('Updating pnpm-lock.yaml after bumping packages');
5651
expect(mockPackageManager).toHaveBeenCalledWith('pnpm', ['install', '--lockfile-only', '--ignore-scripts'], {
5752
stdio: 'inherit',
5853
cwd: mockRoot,
@@ -72,7 +67,7 @@ describe('updateLockFile', () => {
7267
stdio: 'inherit',
7368
cwd: mockRoot,
7469
});
75-
expect(consoleLogSpy).toHaveBeenCalledWith('Updating yarn.lock after bumping packages');
70+
expect(logs.mocks.log).toHaveBeenCalledWith('Updating yarn.lock after bumping packages');
7671
});
7772

7873
it('skips yarn.lock update for yarn v1', async () => {
@@ -83,8 +78,8 @@ describe('updateLockFile', () => {
8378

8479
expect(mockPackageManager).toHaveBeenCalledWith('yarn', ['--version'], { cwd: mockRoot });
8580
expect(mockPackageManager).toHaveBeenCalledTimes(1);
86-
expect(consoleLogSpy).not.toHaveBeenCalled();
87-
expect(consoleWarnSpy).not.toHaveBeenCalled();
81+
expect(logs.mocks.log).not.toHaveBeenCalled();
82+
expect(logs.mocks.warn).not.toHaveBeenCalled();
8883
});
8984

9085
it('warns when yarn version check fails', async () => {
@@ -93,7 +88,7 @@ describe('updateLockFile', () => {
9388

9489
await updateLockFile({ path: mockRoot });
9590

96-
expect(consoleWarnSpy).toHaveBeenCalledWith('Failed to get yarn version. Continuing...');
91+
expect(logs.mocks.warn).toHaveBeenCalledWith('Failed to get yarn version. Continuing...');
9792
});
9893

9994
it('warns when lock file update fails', async () => {
@@ -102,7 +97,7 @@ describe('updateLockFile', () => {
10297

10398
await updateLockFile({ path: mockRoot });
10499

105-
expect(consoleWarnSpy).toHaveBeenCalledWith('Updating package-lock.json failed. Continuing...');
100+
expect(logs.mocks.warn).toHaveBeenCalledWith('Updating package-lock.json failed. Continuing...');
106101
});
107102

108103
it('does nothing when no lock file exists', async () => {
@@ -111,6 +106,6 @@ describe('updateLockFile', () => {
111106
await updateLockFile({ path: mockRoot });
112107

113108
expect(mockPackageManager).not.toHaveBeenCalled();
114-
expect(consoleLogSpy).not.toHaveBeenCalled();
109+
expect(logs.mocks.log).not.toHaveBeenCalled();
115110
});
116111
});

src/__tests__/bump/updatePackageJsons.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { jest, describe, it, expect, beforeEach, afterEach, beforeAll } from '@jest/globals';
1+
import { jest, describe, it, expect, beforeEach, afterEach } from '@jest/globals';
22
import fs from 'fs';
33
import { updatePackageJsons } from '../../bump/updatePackageJsons';
44
import { makePackageInfos } from '../../__fixtures__/packageInfos';
55
import { consideredDependencies, type PackageInfo } from '../../types/PackageInfo';
66
import * as readJsonModule from '../../object/readJson';
77
import * as writeJsonModule from '../../object/writeJson';
8+
import { initMockLogs } from '../../__fixtures__/mockLogs';
89

910
jest.mock('fs');
1011
jest.mock('../../object/readJson');
@@ -14,7 +15,7 @@ describe('updatePackageJsons', () => {
1415
const mockFs = fs as jest.Mocked<typeof fs>;
1516
const mockReadJson = readJsonModule as jest.Mocked<typeof readJsonModule>;
1617
const mockWriteJson = writeJsonModule as jest.Mocked<typeof writeJsonModule>;
17-
let consoleWarnSpy: jest.SpiedFunction<typeof console.warn>;
18+
const logs = initMockLogs({ alsoLog: ['error'] });
1819

1920
/**
2021
* Get `writeJson` args for a package, with beachball-specific keys removed.
@@ -24,10 +25,6 @@ describe('updatePackageJsons', () => {
2425
return [packageJsonPath, json];
2526
}
2627

27-
beforeAll(() => {
28-
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
29-
});
30-
3128
beforeEach(() => {
3229
jest.clearAllMocks();
3330
mockFs.existsSync.mockReturnValue(true);
@@ -80,7 +77,7 @@ describe('updatePackageJsons', () => {
8077

8178
updatePackageJsons(modifiedPackages, packageInfos);
8279

83-
expect(consoleWarnSpy).toHaveBeenCalledWith('Skipping pkg-a since package.json does not exist');
80+
expect(logs.mocks.warn).toHaveBeenCalledWith('Skipping pkg-a since package.json does not exist');
8481
expect(mockReadJson.readJson).not.toHaveBeenCalled();
8582
expect(mockWriteJson.writeJson).not.toHaveBeenCalled();
8683
});

src/__tests__/validate/isValidChangelogOptions.test.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
import { afterEach, beforeAll, describe, expect, it, jest } from '@jest/globals';
1+
import { describe, expect, it } from '@jest/globals';
22
import { isValidChangelogOptions } from '../../validation/isValidChangelogOptions';
33
import type { ChangelogGroupOptions, ChangelogOptions } from '../../types/ChangelogOptions';
4+
import { initMockLogs } from '../../__fixtures__/mockLogs';
45

56
describe('isValidChangelogOptions', () => {
6-
let consoleErrorSpy: jest.SpiedFunction<typeof console.error>;
7-
8-
beforeAll(() => {
9-
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
10-
});
11-
12-
afterEach(() => {
13-
jest.clearAllMocks();
14-
});
7+
const logs = initMockLogs();
158

169
it('returns true when options have no groups', () => {
1710
const options: ChangelogOptions = {};
1811
expect(isValidChangelogOptions(options)).toBe(true);
19-
expect(consoleErrorSpy).not.toHaveBeenCalled();
12+
expect(logs.mocks.error).not.toHaveBeenCalled();
2013
});
2114

2215
it('returns true when groups are valid with masterPackageName', () => {
@@ -30,7 +23,7 @@ describe('isValidChangelogOptions', () => {
3023
],
3124
} as ChangelogOptions;
3225
expect(isValidChangelogOptions(options)).toBe(true);
33-
expect(consoleErrorSpy).not.toHaveBeenCalled();
26+
expect(logs.mocks.error).not.toHaveBeenCalled();
3427
});
3528

3629
it('returns true when groups are valid with mainPackageName', () => {
@@ -44,7 +37,7 @@ describe('isValidChangelogOptions', () => {
4437
],
4538
};
4639
expect(isValidChangelogOptions(options)).toBe(true);
47-
expect(consoleErrorSpy).not.toHaveBeenCalled();
40+
expect(logs.mocks.error).not.toHaveBeenCalled();
4841
});
4942

5043
it('returns false when group is missing changelogPath', () => {
@@ -57,7 +50,7 @@ describe('isValidChangelogOptions', () => {
5750
],
5851
} as ChangelogOptions;
5952
expect(isValidChangelogOptions(options)).toBe(false);
60-
expect(consoleErrorSpy).toHaveBeenCalled();
53+
expect(logs.mocks.error).toHaveBeenCalled();
6154
});
6255

6356
it('returns false when group is missing mainPackageName and masterPackageName', () => {
@@ -70,7 +63,7 @@ describe('isValidChangelogOptions', () => {
7063
],
7164
} as ChangelogOptions;
7265
expect(isValidChangelogOptions(options)).toBe(false);
73-
expect(consoleErrorSpy).toHaveBeenCalled();
66+
expect(logs.mocks.error).toHaveBeenCalled();
7467
});
7568

7669
it('returns false when group is missing include', () => {
@@ -83,7 +76,7 @@ describe('isValidChangelogOptions', () => {
8376
],
8477
} as ChangelogOptions;
8578
expect(isValidChangelogOptions(options)).toBe(false);
86-
expect(consoleErrorSpy).toHaveBeenCalled();
79+
expect(logs.mocks.error).toHaveBeenCalled();
8780
});
8881

8982
it('returns false when multiple groups are invalid', () => {
@@ -98,7 +91,7 @@ describe('isValidChangelogOptions', () => {
9891
],
9992
};
10093
expect(isValidChangelogOptions(options)).toBe(false);
101-
expect(consoleErrorSpy).toHaveBeenCalled();
94+
expect(logs.mocks.error).toHaveBeenCalled();
10295
});
10396

10497
it('returns false for a mix of valid and invalid groups', () => {
@@ -115,6 +108,6 @@ describe('isValidChangelogOptions', () => {
115108
],
116109
};
117110
expect(isValidChangelogOptions(options)).toBe(false);
118-
expect(consoleErrorSpy).toHaveBeenCalled();
111+
expect(logs.mocks.error).toHaveBeenCalled();
119112
});
120113
});

0 commit comments

Comments
 (0)