Skip to content

Commit

Permalink
Reuse repository fixtures to speed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed May 4, 2023
1 parent 830b3b2 commit 839a542
Show file tree
Hide file tree
Showing 26 changed files with 553 additions and 417 deletions.
7 changes: 7 additions & 0 deletions change/beachball-e19233fc-2764-4e87-8f58-d6315c281d2d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add an internal mechanism to disable caching for tests",
"packageName": "beachball",
"email": "[email protected]",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/** @type {import('@jest/types').Config.InitialProjectOptions} */
const commonOptions = {
roots: ['<rootDir>/src'],
setupFilesAfterEnv: ['<rootDir>/scripts/jestSetup.js'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
Expand Down
7 changes: 7 additions & 0 deletions scripts/jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-check
// Disable caching in workspace-tools to prevent interference with tests that reuse directories
// with potentially different contents.
require('workspace-tools').setCachingEnabled(false);

// Disable caching in beachball
process.env.BEACHBALL_DISABLE_CACHE = '1';
192 changes: 81 additions & 111 deletions src/__e2e__/bump.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, afterEach } from '@jest/globals';
import { describe, expect, it, afterEach, beforeAll, afterAll } from '@jest/globals';
import fs from 'fs-extra';
import path from 'path';
import { generateChangeFiles, getChangeFiles } from '../__fixtures__/changeFiles';
Expand All @@ -10,28 +10,48 @@ import { getPackageInfos } from '../monorepo/getPackageInfos';
import { BeachballOptions } from '../types/BeachballOptions';

describe('version bumping', () => {
let repositoryFactory: RepositoryFactory | undefined;
/** Factories used in multiple tests */
const factories = {
singlePackage: new RepositoryFactory('single'),
monorepo: new RepositoryFactory('monorepo'),
multiWorkspace: new RepositoryFactory('multi-workspace'),
monorepoSinglePackage: new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
}),
monorepoMultiDepTypes: new RepositoryFactory({
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' } },
},
},
}),
};
let factory: RepositoryFactory | undefined;

initMockLogs();

beforeAll(() => {
RepositoryFactory.initAll(factories);
});

afterEach(() => {
if (repositoryFactory) {
repositoryFactory.cleanUp();
repositoryFactory = undefined;
}
RepositoryFactory.resetOrCleanUp(factory, factories);
factory = undefined;
});

afterAll(() => {
RepositoryFactory.cleanUpAll(factories);
});

it('bumps only packages with change files', 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' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoMultiDepTypes;
const monorepo = factory.fixture.folders!;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand All @@ -55,9 +75,9 @@ describe('version bumping', () => {
});

it('for multi-workspace (multi-monorepo), only bumps packages in the current workspace', async () => {
repositoryFactory = new RepositoryFactory('multi-workspace');
expect(Object.keys(repositoryFactory.fixtures)).toEqual(['workspace-a', 'workspace-b']);
const repo = repositoryFactory.cloneRepository();
factory = factories.multiWorkspace;
expect(Object.keys(factory.fixtures)).toEqual(['workspace-a', 'workspace-b']);
const repo = factory.defaultRepo;

const workspaceARoot = repo.pathTo('workspace-a');
const workspaceBRoot = repo.pathTo('workspace-b');
Expand Down Expand Up @@ -88,8 +108,9 @@ describe('version bumping', () => {
'pkg-3': { version: '1.0.0' },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = new RepositoryFactory({ folders: monorepo });
factory.init();
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -117,16 +138,8 @@ describe('version bumping', () => {
});

it('bumps all dependent packages with `bumpDeps` 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', devDependencies: { 'pkg-2': '1.0.0' } },
'pkg-4': { version: '1.0.0', peerDependencies: { 'pkg-3': '1.0.0' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoMultiDepTypes;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -161,8 +174,9 @@ describe('version bumping', () => {
'pkg-4': { version: '1.0.0' },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = new RepositoryFactory({ folders: monorepo });
factory.init();
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -199,8 +213,9 @@ describe('version bumping', () => {
unrelated: { version: '1.0.0' },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = new RepositoryFactory({ folders: monorepo });
factory.init();
const repo = factory.defaultRepo;

generateChangeFiles([{ packageName: 'commonlib', dependentChangeType: 'minor' }], repo.rootPath);

Expand All @@ -227,9 +242,9 @@ describe('version bumping', () => {
});

it('should not bump out-of-scope package even if package has change', async () => {
repositoryFactory = new RepositoryFactory('monorepo');
const monorepo = repositoryFactory.fixture.folders!;
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepo;
const monorepo = factory.fixture.folders!;
const repo = factory.defaultRepo;

generateChangeFiles(['foo'], repo.rootPath);

Expand All @@ -250,9 +265,9 @@ describe('version bumping', () => {
});

it('should not bump out-of-scope package and its dependencies even if dependency of the package has change', async () => {
repositoryFactory = new RepositoryFactory('monorepo');
const monorepo = repositoryFactory.fixture.folders!;
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepo;
const monorepo = factory.fixture.folders!;
const repo = factory.defaultRepo;

generateChangeFiles([{ packageName: 'bar', type: 'patch' }], repo.rootPath);

Expand All @@ -274,16 +289,9 @@ describe('version bumping', () => {
});

it('bumps all packages and keeps change files with `keep-change-files` 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', devDependencies: { 'pkg-2': '1.0.0' } },
'pkg-4': { version: '1.0.0', peerDependencies: { 'pkg-3': '1.0.0' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoMultiDepTypes;
const monorepo = factory.fixture.folders!;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -311,16 +319,8 @@ describe('version bumping', () => {
});

it('bumps all packages and uses prefix in the version', 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' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoMultiDepTypes;
const repo = factory.defaultRepo;

generateChangeFiles([{ packageName: 'pkg-1', type: 'prerelease' }], repo.rootPath);

Expand Down Expand Up @@ -349,16 +349,8 @@ describe('version bumping', () => {
});

it('bumps all packages and uses prefixed versions in 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' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoMultiDepTypes;
const repo = factory.defaultRepo;

generateChangeFiles(
[{ packageName: 'pkg-1', type: 'prerelease', dependentChangeType: 'prerelease' }],
Expand Down Expand Up @@ -398,8 +390,9 @@ describe('version bumping', () => {
'pkg-4': { version: '1.0.0', peerDependencies: { 'pkg-3': '1.0.0' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = new RepositoryFactory({ folders: monorepo });
factory.init();
const repo = factory.defaultRepo;

generateChangeFiles(
[{ packageName: 'pkg-1', type: 'prerelease', dependentChangeType: 'prerelease' }],
Expand Down Expand Up @@ -438,8 +431,9 @@ describe('version bumping', () => {
package2: { version: '0.0.1', dependencies: { package1: '^0.0.1' } },
},
};
repositoryFactory = new RepositoryFactory({ folders: monorepo });
const repo = repositoryFactory.cloneRepository();
factory = new RepositoryFactory({ folders: monorepo });
factory.init();
const repo = factory.defaultRepo;

generateChangeFiles(
[
Expand Down Expand Up @@ -470,12 +464,8 @@ describe('version bumping', () => {
});

it('calls sync prebump hook before packages are bumped', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoSinglePackage;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -503,12 +493,8 @@ describe('version bumping', () => {
});

it('calls async prebump hook before packages are bumped', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoSinglePackage;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -536,12 +522,8 @@ describe('version bumping', () => {
});

it('propagates prebump hook exceptions', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoSinglePackage;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand All @@ -561,12 +543,8 @@ describe('version bumping', () => {
});

it('calls sync postbump hook before packages are bumped', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoSinglePackage;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -594,12 +572,8 @@ describe('version bumping', () => {
});

it('calls async postbump hook before packages are bumped', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoSinglePackage;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down Expand Up @@ -627,12 +601,8 @@ describe('version bumping', () => {
});

it('propagates postbump hook exceptions', async () => {
repositoryFactory = new RepositoryFactory({
folders: {
packages: { 'pkg-1': { version: '1.0.0' } },
},
});
const repo = repositoryFactory.cloneRepository();
factory = factories.monorepoSinglePackage;
const repo = factory.defaultRepo;

generateChangeFiles(['pkg-1'], repo.rootPath);

Expand Down
Loading

0 comments on commit 839a542

Please sign in to comment.