Skip to content

Commit

Permalink
test(core): add test for scenario to reproduce #66
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed May 29, 2022
1 parent e26ab69 commit cbdc6cc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 30 deletions.
54 changes: 42 additions & 12 deletions src/bin-fix-mismatches/fix-mismatches.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'expect-more-jest';
import { scenarios } from '../../test/scenarios';
import type { TestScenario } from '../../test/scenarios/create-scenario';
import { getInput } from '../lib/get-input';
import { fixMismatches } from './fix-mismatches';

Expand All @@ -10,18 +11,47 @@ describe('fixMismatches', () => {

describe('when dependencies are installed with different versions', () => {
describe('when the dependency is a package maintained in this workspace', () => {
it('uses the workspace version', () => {
const scenario = scenarios.dependentDoesNotMatchWorkspaceVersion();
fixMismatches(getInput(scenario.disk, scenario.config), scenario.disk);
expect(scenario.disk.writeFileSync.mock.calls).toEqual([
scenario.files['packages/a/package.json'].diskWriteWhenChanged,
scenario.files['packages/b/package.json'].diskWriteWhenChanged,
]);
expect(scenario.log.mock.calls).toEqual([
scenario.files['packages/a/package.json'].logEntryWhenChanged,
scenario.files['packages/b/package.json'].logEntryWhenChanged,
scenario.files['packages/c/package.json'].logEntryWhenUnchanged,
]);
describe('when using a typical workspace', () => {
it('warns about the workspace version', () => {
const scenario = scenarios.dependentDoesNotMatchWorkspaceVersion();
fixMismatches(
getInput(scenario.disk, scenario.config),
scenario.disk,
);
expect(scenario.disk.writeFileSync.mock.calls).toEqual([
scenario.files['packages/a/package.json'].diskWriteWhenChanged,
scenario.files['packages/b/package.json'].diskWriteWhenChanged,
]);
expect(scenario.log.mock.calls).toEqual([
scenario.files['packages/a/package.json'].logEntryWhenChanged,
scenario.files['packages/b/package.json'].logEntryWhenChanged,
scenario.files['packages/c/package.json'].logEntryWhenUnchanged,
]);
});
});
describe('when using nested workspaces', () => {
it('warns about the workspace version', () => {
const scenario =
scenarios.dependentDoesNotMatchNestedWorkspaceVersion();
fixMismatches(
getInput(scenario.disk, scenario.config),
scenario.disk,
);
expect(scenario.disk.writeFileSync.mock.calls).toEqual([
scenario.files['workspaces/a/packages/a/package.json']
.diskWriteWhenChanged,
scenario.files['workspaces/b/packages/b/package.json']
.diskWriteWhenChanged,
]);
expect(scenario.log.mock.calls).toEqual([
scenario.files['workspaces/a/packages/a/package.json']
.logEntryWhenChanged,
scenario.files['workspaces/b/packages/b/package.json']
.logEntryWhenChanged,
scenario.files['workspaces/b/packages/c/package.json']
.logEntryWhenUnchanged,
]);
});
});
});

Expand Down
36 changes: 27 additions & 9 deletions src/bin-list-mismatches/list-mismatches.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'expect-more-jest';
import { scenarios } from '../../test/scenarios';
import type { TestScenario } from '../../test/scenarios/create-scenario';
import { getInput } from '../lib/get-input';
import { listMismatches } from './list-mismatches';

Expand All @@ -10,15 +11,32 @@ describe('listMismatches', () => {

describe('when dependencies are installed with different versions', () => {
describe('when the dependency is a package maintained in this workspace', () => {
it('warns ab the workspace version', () => {
const scenario = scenarios.dependentDoesNotMatchWorkspaceVersion();
listMismatches(getInput(scenario.disk, scenario.config), scenario.disk);
expect(scenario.log.mock.calls).toEqual([
['- c 0.0.1'],
[' 0.1.0 in dependencies of a'],
[' 0.2.0 in devDependencies of b'],
]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
const variants: [string, () => TestScenario][] = [
[
'when using a typical workspace',
scenarios.dependentDoesNotMatchWorkspaceVersion,
],
[
'when using nested workspaces',
scenarios.dependentDoesNotMatchNestedWorkspaceVersion,
],
];
variants.forEach(([context, getScenario]) => {
describe(context, () => {
it('warns about the workspace version', () => {
const scenario = getScenario();
listMismatches(
getInput(scenario.disk, scenario.config),
scenario.disk,
);
expect(scenario.log.mock.calls).toEqual([
['- c 0.0.1'],
[' 0.1.0 in dependencies of a'],
[' 0.2.0 in devDependencies of b'],
]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
});
});
});
});

Expand Down
25 changes: 20 additions & 5 deletions src/bin-list/list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'expect-more-jest';
import { scenarios } from '../../test/scenarios';
import type { TestScenario } from '../../test/scenarios/create-scenario';
import { getInput } from '../lib/get-input';
import { list } from './list';

Expand All @@ -10,11 +11,25 @@ describe('list', () => {

describe('when dependencies are installed with different versions', () => {
describe('when the dependency is a package maintained in this workspace', () => {
it('warns ab the workspace version', () => {
const scenario = scenarios.dependentDoesNotMatchWorkspaceVersion();
list(getInput(scenario.disk, scenario.config), scenario.disk);
expect(scenario.log.mock.calls).toEqual([['✕ c 0.1.0, 0.2.0']]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
const variants: [string, () => TestScenario][] = [
[
'when using a typical workspace',
scenarios.dependentDoesNotMatchWorkspaceVersion,
],
[
'when using nested workspaces',
scenarios.dependentDoesNotMatchNestedWorkspaceVersion,
],
];
variants.forEach(([context, getScenario]) => {
describe(context, () => {
it('warns about the workspace version', () => {
const scenario = getScenario();
list(getInput(scenario.disk, scenario.config), scenario.disk);
expect(scenario.log.mock.calls).toEqual([['✕ c 0.1.0, 0.2.0']]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
});
});
});
});

Expand Down
10 changes: 9 additions & 1 deletion test/scenarios/create-scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import minimatch from 'minimatch';
import path from 'path';
import type { SyncpackConfig } from '../../src/constants';
import type { SourceWrapper } from '../../src/lib/get-input/get-wrappers';
import type { MockDisk } from '../mock-disk';
import { mockDisk } from '../mock-disk';

interface MockedFile {
Expand All @@ -14,14 +15,21 @@ interface MockedFile {
relativePath: string;
}

export interface TestScenario {
config: Partial<SyncpackConfig & { configPath: string | undefined }>;
disk: MockDisk;
log: jest.SpyInstance;
files: Record<string, MockedFile>;
}

export function createScenario(
fileMocks: {
path: string;
before: SourceWrapper;
after: SourceWrapper;
}[],
config: Partial<SyncpackConfig & { configPath: string | undefined }>,
) {
): TestScenario {
const disk = mockDisk();
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
// resolve all paths
Expand Down
26 changes: 23 additions & 3 deletions test/scenarios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,16 @@ export const scenarios = {
);
},
/**
* Variation of the previous scenario in a nested workspace.
*
* Variation of `dependentDoesNotMatchWorkspaceVersion` in a nested workspace.
*
* C is developed in this monorepo, its version is `0.0.1`
* C's version is the single source of truth and should never be changed
* A and B depend on C incorrectly and should be fixed
* A, B, and C are in nested workspaces
*
* @see https://github.com/goldstack/goldstack/pull/170/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R19
* @see https://github.com/JamieMason/syncpack/pull/74
* @see https://github.com/JamieMason/syncpack/issues/66
*/
dependentDoesNotMatchNestedWorkspaceVersion() {
return createScenario(
Expand All @@ -129,7 +137,19 @@ export const scenarios = {
}),
},
],
{},
{
dev: true,
overrides: false,
peer: false,
prod: true,
resolutions: false,
workspace: true,
source: [
'package.json',
'workspaces/*/package.json',
'workspaces/*/packages/*/package.json',
],
},
);
},
/**
Expand Down

0 comments on commit cbdc6cc

Please sign in to comment.