Skip to content

Commit 9e731bd

Browse files
authored
Add more tests and improve publish logging (#1201)
1 parent 99f75b1 commit 9e731bd

21 files changed

Lines changed: 1294 additions & 87 deletions

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"runtimeExecutable": "npm",
1212
"cwd": "${workspaceFolder}",
1313
"runtimeArgs": ["run-script", "test"],
14-
"args": ["--", "--runInBand", "--watch", "--testTimeout=1000000", "${fileBasenameNoExtension}"],
14+
"args": ["--", "--runInBand", "--watch", "--testTimeout=1000000", "${file}"],
1515
"sourceMaps": true,
1616
"outputCapture": "std",
1717
"console": "integratedTerminal",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Refine publish logging",
4+
"packageName": "beachball",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

src/__e2e__/publishE2E.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { validate } from '../validation/validate';
2020
// These tests are slow, so they should only cover E2E publishing scenarios that can't be fully
2121
// covered by lower-level tests (such as publishToRegistry or bumping functional tests), and a
2222
// few all-up scenarios as sanity checks. Tests specific to git or npm scenarios should
23-
// potentially go in publishGit.test.ts or publishRegistry.test.ts instead.
23+
// potentially go in publishGit.test.ts or publishNpm.test.ts instead.
2424
//
2525
// Spawning actual npm to run commands against a fake registry is extremely slow, so mock it for
2626
// this test (packagePublish covers the more complete npm registry scenario).
@@ -88,9 +88,9 @@ describe('publish command (e2e)', () => {
8888
generateChangeFiles(['foo'], options);
8989
repo.push();
9090

91-
let fetchCount = 0;
9291
addGitObserver(args => {
93-
args[0] === 'fetch' && fetchCount++;
92+
// no fetch when flag set to false
93+
expect(args[0]).not.toBe('fetch');
9494
});
9595

9696
await publishWrapper(parsedOptions);
@@ -101,9 +101,6 @@ describe('publish command (e2e)', () => {
101101
'dist-tags': { latest: '1.1.0' },
102102
});
103103

104-
// no fetch when flag set to false
105-
expect(fetchCount).toBe(0);
106-
107104
repo.checkout(defaultBranchName);
108105
repo.pull();
109106
expect(repo.getCurrentTags()).toEqual(['foo_v1.1.0']);

src/__e2e__/publishGit.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { getParsedOptions } from '../options/getOptions';
1616
import { readJson } from '../object/readJson';
1717
import { createCommandContext } from '../monorepo/createCommandContext';
1818
import type { RepoOptions } from '../types/BeachballOptions';
19-
import { addGitObserver, clearGitObservers } from 'workspace-tools';
2019

2120
describe('publish command (git)', () => {
2221
let repositoryFactory: RepositoryFactory | undefined;
@@ -44,7 +43,6 @@ describe('publish command (git)', () => {
4443
}
4544

4645
afterEach(() => {
47-
clearGitObservers();
4846
repositoryFactory?.cleanUp();
4947
repositoryFactory = undefined;
5048
repo = undefined;
@@ -129,26 +127,4 @@ describe('publish command (git)', () => {
129127
// changes from publish process were committed
130128
expect(fs.existsSync(txtPath)).toBe(true);
131129
});
132-
133-
it('specifies fetch depth when depth param is defined', async () => {
134-
repositoryFactory = new RepositoryFactory('single');
135-
repo = repositoryFactory.cloneRepository();
136-
137-
const { options, parsedOptions } = getOptions({
138-
depth: 10,
139-
});
140-
141-
generateChangeFiles(['foo'], options);
142-
repo.push();
143-
144-
const gitObserver = jest.fn((args: string[]) => {
145-
if (args[0] === 'fetch') {
146-
expect(args).toContain('--depth=10');
147-
}
148-
});
149-
addGitObserver(gitObserver);
150-
151-
await publish(options, createCommandContext(parsedOptions));
152-
expect(gitObserver).toHaveBeenCalled();
153-
});
154130
});
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { validate } from '../validation/validate';
2525
jest.mock('../packageManager/npm');
2626
// jest.mock('npm-registry-fetch');
2727

28-
describe('publish command (registry)', () => {
28+
describe('publish command (npm)', () => {
2929
const npmMock = initNpmMock();
3030

3131
let repositoryFactory: RepositoryFactory | undefined;
@@ -110,7 +110,7 @@ describe('publish command (registry)', () => {
110110

111111
// If there's only the private package with a change file, nothing happens
112112
await publishWrapper(parsedOptions);
113-
expect(logs.mocks.log).toHaveBeenCalledWith('Nothing to bump, skipping publish!');
113+
expect(logs.mocks.log).toHaveBeenCalledWith(expect.stringContaining('Nothing to bump - skipping publish!'));
114114
expect(logs.mocks.warn).toHaveBeenCalledWith(expect.stringContaining('Change detected for private package foopkg'));
115115
expect(logs.mocks.error).not.toHaveBeenCalled();
116116
expect(npmMock.getPublishedVersions('foopkg')).toBeUndefined();
@@ -132,10 +132,9 @@ describe('publish command (registry)', () => {
132132
[log] OK!
133133
134134
[log]
135-
[log]
136-
Preparing to publish
137-
[log]
138-
Publishing with the following configuration:
135+
[log] Preparing to publish
136+
137+
[log] Publishing with the following configuration:
139138
140139
registry: fake
141140
@@ -150,8 +149,8 @@ describe('publish command (registry)', () => {
150149
151150
152151
[log] Creating temporary publish branch publish_<timestamp>
153-
[log]
154-
Bumping versions and publishing packages to npm registry
152+
153+
[log] Bumping versions and publishing packages to npm registry
155154
156155
[log] Removing change files:
157156
[log] - publicpkg-<guid>.json
@@ -171,8 +170,9 @@ describe('publish command (registry)', () => {
171170
172171
[log]
173172
[log] Skipping git push and tagging
174-
[log]
175-
Cleaning up
173+
174+
[log] Cleaning up
175+
176176
[log] git checkout master
177177
[log] deleting temporary publish branch publish_<timestamp>"
178178
`);
@@ -210,7 +210,7 @@ describe('publish command (registry)', () => {
210210
// initial validate() isn't relevant here
211211
await publish(options, createCommandContext(parsedOptions));
212212

213-
expect(logs.mocks.log).toHaveBeenCalledWith('Nothing to bump, skipping publish!');
213+
expect(logs.mocks.log).toHaveBeenCalledWith(expect.stringContaining('Nothing to bump - skipping publish!'));
214214
expect(logs.mocks.warn).toHaveBeenCalledWith(expect.stringContaining('Change detected for private package bar'));
215215
expect(logs.mocks.warn).toHaveBeenCalledWith(
216216
expect.stringContaining('Change detected for nonexistent package fake')

src/__fixtures__/mockLogs.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { jest, afterEach, beforeAll, afterAll } from '@jest/globals';
1+
import { jest, afterEach, beforeAll, afterAll, beforeEach } from '@jest/globals';
22

33
/** Methods that will be mocked. More could be added later if needed. */
44
type MockLogMethod = 'log' | 'warn' | 'error';
@@ -10,6 +10,12 @@ type MockLogsOptions = {
1010
* All logging can be enabled by setting the VERBOSE env var.
1111
*/
1212
alsoLog?: boolean | MockLogMethod[];
13+
14+
/**
15+
* Instead of setting up mocks in `beforeAll`, do it in `beforeEach`.
16+
* This allows calling `jest.resetAllMocks()` in tests without breaking the logging mocks.
17+
*/
18+
mockBeforeEach?: boolean;
1319
};
1420

1521
export type MockLogs = {
@@ -49,7 +55,7 @@ export type MockLogs = {
4955
* of any lifecycle hooks or tests because it calls lifecycle hooks internally for setup and teardown.
5056
*/
5157
export function initMockLogs(options: MockLogsOptions = {}): MockLogs {
52-
const { alsoLog } = options;
58+
const { alsoLog, mockBeforeEach } = options;
5359
let allLines: unknown[][] = [];
5460
let overrideOptions: MockLogsOptions | undefined;
5561
const jestConsole = { ...console };
@@ -102,7 +108,7 @@ export function initMockLogs(options: MockLogsOptions = {}): MockLogs {
102108
},
103109
};
104110

105-
beforeAll(() => {
111+
(mockBeforeEach ? beforeEach : beforeAll)(() => {
106112
for (const method of mockedMethods) {
107113
const mainShouldLog = shouldLog(method, alsoLog);
108114

@@ -121,7 +127,7 @@ export function initMockLogs(options: MockLogsOptions = {}): MockLogs {
121127
logs.clear();
122128
});
123129

124-
afterAll(() => {
130+
(mockBeforeEach ? afterEach : afterAll)(() => {
125131
Object.values(logs.mocks).forEach(mock => mock.mockRestore());
126132
});
127133

src/__fixtures__/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const portRange = 1000;
1717
* Lists of tests known to use `Registry`. This is used to make each test try a different
1818
* port range to avoid collisions caused by race conditions with grabbing free ports.
1919
*/
20-
const knownTests = ['packagePublish', 'publishE2E', 'publishRegistry', 'syncE2E'];
20+
const knownTests = ['packagePublish', 'publishE2E', 'publishNpm', 'syncE2E'];
2121

2222
// NOTE: If you are getting timeouts and port collisions, set jest.setTimeout to a higher value.
2323
// The default value of 5 seconds may not be enough in situations with port collisions.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`publish command logs expected output for a standard publish flow 1`] = `
4+
"[log]
5+
Validating options and change files...
6+
[log]
7+
Validating package dependencies...
8+
[log] Validating no private package among package dependencies
9+
[log] OK!
10+
11+
[log]
12+
[log] Preparing to publish
13+
14+
[log] Publishing with the following configuration:
15+
16+
registry: fake
17+
18+
current branch: master
19+
current hash: <commit>
20+
target branch: origin/master
21+
npm dist-tag: latest
22+
23+
bumps versions before publishing: yes
24+
publishes to npm registry: yes
25+
pushes bumps and changelogs to remote git repo: no
26+
27+
28+
[log] Creating temporary publish branch publish_<timestamp>
29+
30+
[log] Bumping versions and publishing packages to npm registry
31+
32+
[log]
33+
[log] Skipping git push and tagging
34+
35+
[log] Cleaning up
36+
37+
[log] git checkout master
38+
[log] deleting temporary publish branch publish_<timestamp>"
39+
`;

0 commit comments

Comments
 (0)