Skip to content

Commit

Permalink
fix(workspace): find missed mismatches against workspace versions
Browse files Browse the repository at this point in the history
Closes #66
  • Loading branch information
JamieMason committed Jun 3, 2022
1 parent 89a5a56 commit 25c1836
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/bin-list-mismatches/list-mismatches.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ describe('listMismatches', () => {

describe('when dependencies are installed with different versions', () => {
describe('when the dependency is a package maintained in this workspace', () => {
const variants: [string, () => TestScenario][] = [
const variants: [string, () => TestScenario, string][] = [
[
'when using a typical workspace',
scenarios.dependentDoesNotMatchWorkspaceVersion,
'packages/c/package.json',
],
[
'when using nested workspaces',
scenarios.dependentDoesNotMatchNestedWorkspaceVersion,
'workspaces/b/packages/c/package.json',
],
];
variants.forEach(([context, getScenario]) => {
variants.forEach(([context, getScenario, originPath]) => {
describe(context, () => {
it('warns about the workspace version', () => {
const scenario = getScenario();
Expand All @@ -33,6 +35,7 @@ describe('listMismatches', () => {
['- c 0.0.1'],
[' 0.1.0 in dependencies of a'],
[' 0.2.0 in devDependencies of b'],
[` 0.0.1 at ${originPath}`],
]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
});
Expand Down
4 changes: 3 additions & 1 deletion src/bin-list/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('list', () => {
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.log.mock.calls).toEqual([
['✕ c 0.0.1, 0.1.0, 0.2.0'],
]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
});
});
Expand Down
15 changes: 8 additions & 7 deletions src/lib/get-input/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,16 @@ export const getConfig = (

const hasTypeOverride =
isBoolean(program.dev) ||
isBoolean(program.workspace) ||
isBoolean(program.overrides) ||
isBoolean(program.peer) ||
isBoolean(program.pnpmOverrides) ||
isBoolean(program.prod) ||
isBoolean(program.resolutions);
isBoolean(program.resolutions) ||
isBoolean(program.workspace);

const dev = hasTypeOverride
? Boolean(program.dev)
: getOption<boolean>('dev', isBoolean);
const workspace = hasTypeOverride
? Boolean(program.workspace)
: getOption<boolean>('workspace', isBoolean);
const overrides = hasTypeOverride
? Boolean(program.overrides)
: getOption<boolean>('overrides', isBoolean);
Expand All @@ -65,6 +62,9 @@ export const getConfig = (
const resolutions = hasTypeOverride
? Boolean(program.resolutions)
: getOption<boolean>('resolutions', isBoolean);
const workspace = hasTypeOverride
? Boolean(program.workspace)
: getOption<boolean>('workspace', isBoolean);

const dependencyTypes =
dev ||
Expand All @@ -76,12 +76,13 @@ export const getConfig = (
workspace
? DEPENDENCY_TYPES.filter(
(type) =>
(type === 'dependencies' && prod) ||
(type === 'devDependencies' && dev) ||
(type === 'overrides' && overrides) ||
(type === 'peerDependencies' && peer) ||
(type === 'pnpmOverrides' && pnpmOverrides) ||
(type === 'resolutions' && resolutions),
(type === 'dependencies' && prod) ||
(type === 'resolutions' && resolutions) ||
(type === 'workspace' && workspace),
)
: DEPENDENCY_TYPES;

Expand Down
61 changes: 32 additions & 29 deletions src/lib/get-input/get-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,47 @@ describe('getInput', () => {
const prod = 'dependencies';
const resolutions = 'resolutions';
const workspace = 'workspace';
const allTypes = [
dev,
overrides,
peer,
pnpmOverrides,
prod,
resolutions,
workspace,
];
const ix = {
dev: 'devDependencies',
overrides: 'overrides',
peer: 'peerDependencies',
pnpmOverrides: 'pnpmOverrides',
prod: 'dependencies',
resolutions: 'resolutions',
workspace: 'workspace',
};

it('includes all except workspace (which is a not a property of package.json) if none are set', () => {
it('includes all if none are set', () => {
expect(getInput(disk, {})).toHaveProperty(
'dependencyTypes',
expect.arrayContaining([prod, dev, peer, overrides, resolutions]),
expect.toBeArrayIncludingOnly(allTypes),
);
});
it('enables one if it is the only one set', () => {
expect(getInput(disk, { prod: true })).toHaveProperty('dependencyTypes', [
prod,
]);
expect(getInput(disk, { dev: true })).toHaveProperty('dependencyTypes', [
dev,
]);
expect(getInput(disk, { peer: true })).toHaveProperty('dependencyTypes', [
peer,
]);
expect(getInput(disk, { workspace: true })).toHaveProperty(
'dependencyTypes',
[],
);
expect(getInput(disk, { overrides: true })).toHaveProperty(
'dependencyTypes',
[overrides],
);
expect(getInput(disk, { pnpmOverrides: true })).toHaveProperty(
'dependencyTypes',
[pnpmOverrides],
);
expect(getInput(disk, { resolutions: true })).toHaveProperty(
'dependencyTypes',
[resolutions],
);
expect.assertions(allTypes.length);
Object.entries(ix).forEach(([optionName, typeName]) => {
expect(getInput(disk, { [optionName]: true })).toHaveProperty(
'dependencyTypes',
expect.toBeArrayIncludingOnly([typeName]),
);
});
});
it('enables some if only those are set', () => {
expect(
getInput(disk, { dev: true, workspace: true, prod: true }),
).toHaveProperty('dependencyTypes', expect.arrayContaining([prod, dev]));
getInput(disk, { dev: true, prod: true, workspace: true }),
).toHaveProperty(
'dependencyTypes',
expect.toBeArrayIncludingOnly([dev, prod, workspace]),
);
});
});
describe('source', () => {
Expand Down
3 changes: 2 additions & 1 deletion test/scenarios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export const scenarios = {
/**
* 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 depends on C incorrectly and should be fixed
* B depends on C incorrectly and should be fixed
*/
dependentDoesNotMatchWorkspaceVersion() {
return createScenario(
Expand Down

0 comments on commit 25c1836

Please sign in to comment.