-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): refactor tests and add more scenarios
- Loading branch information
1 parent
e571775
commit d37ad27
Showing
20 changed files
with
379 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,207 +1,57 @@ | ||
import 'expect-more-jest'; | ||
import { wrapper } from '../../test/mock'; | ||
import { mockDisk } from '../../test/mock-disk'; | ||
import { scenarios } from '../../test/scenarios'; | ||
import { getInput } from '../lib/get-input'; | ||
import { fixMismatches } from './fix-mismatches'; | ||
|
||
describe('fixMismatches', () => { | ||
describe('when dependencies are installed with different versions', () => { | ||
describe('when the dependency is not a package maintained in this workspace', () => { | ||
it('uses the highest version', () => { | ||
const disk = mockDisk(); | ||
const aBefore = wrapper('a', ['foo@0.1.0']); | ||
const bBefore = wrapper('b', [], ['foo@0.2.0']); | ||
const aAfter = wrapper('a', ['foo@0.2.0']); | ||
const log = jest | ||
.spyOn(console, 'log') | ||
.mockImplementation(() => undefined); | ||
disk.globSync.mockImplementation((glob) => { | ||
if (glob.endsWith('packages/*/package.json')) { | ||
return ['packages/a/package.json', 'packages/b/package.json']; | ||
} | ||
}); | ||
disk.readFileSync.mockImplementation((filePath) => { | ||
if (filePath.endsWith('packages/a/package.json')) { | ||
return aBefore.json; | ||
} | ||
if (filePath.endsWith('packages/b/package.json')) { | ||
return bBefore.json; | ||
} | ||
}); | ||
fixMismatches(getInput(disk, {}), disk); | ||
expect(disk.writeFileSync.mock.calls).toEqual([ | ||
[expect.stringContaining('packages/a/package.json'), aAfter.json], | ||
]); | ||
expect(log.mock.calls).toEqual([ | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/a/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/-/), | ||
expect.stringMatching('packages/b/package.json'), | ||
], | ||
]); | ||
log.mockRestore(); | ||
}); | ||
}); | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
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 disk = mockDisk(); | ||
const aBefore = wrapper('a', ['foo@0.1.0']); | ||
const bBefore = wrapper('b', [], ['foo@0.2.0']); | ||
const fooBefore = wrapper('foo', [], [], [], { | ||
name: 'foo', | ||
version: '0.0.1', | ||
}); | ||
const aAfter = wrapper('a', ['foo@0.0.1']); | ||
const bAfter = wrapper('b', [], ['foo@0.0.1']); | ||
const log = jest | ||
.spyOn(console, 'log') | ||
.mockImplementation(() => undefined); | ||
disk.globSync.mockImplementation((glob) => { | ||
if (glob.endsWith('packages/*/package.json')) { | ||
return [ | ||
'packages/a/package.json', | ||
'packages/b/package.json', | ||
'packages/foo/package.json', | ||
]; | ||
} | ||
}); | ||
disk.readFileSync.mockImplementation((filePath) => { | ||
if (filePath.endsWith('packages/a/package.json')) return aBefore.json; | ||
if (filePath.endsWith('packages/b/package.json')) return bBefore.json; | ||
if (filePath.endsWith('packages/foo/package.json')) | ||
return fooBefore.json; | ||
}); | ||
fixMismatches(getInput(disk, {}), disk); | ||
expect(disk.writeFileSync.mock.calls).toEqual([ | ||
[expect.stringContaining('packages/a/package.json'), aAfter.json], | ||
[expect.stringContaining('packages/b/package.json'), bAfter.json], | ||
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(log.mock.calls).toEqual([ | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/a/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/b/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/-/), | ||
expect.stringMatching('packages/foo/package.json'), | ||
], | ||
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, | ||
]); | ||
log.mockRestore(); | ||
}); | ||
}); | ||
|
||
it('replaces non-semver dependencies with valid semver dependencies', () => { | ||
const disk = mockDisk(); | ||
const aBefore = wrapper('a', ['foo@link:vendor/foo-0.1.0']); | ||
const bBefore = wrapper('b', ['foo@link:vendor/foo-0.2.0']); | ||
const cBefore = wrapper('c', ['foo@0.3.0']); | ||
const dBefore = wrapper('d', ['foo@0.2.0']); | ||
const aAfter = wrapper('a', ['foo@0.3.0']); | ||
const bAfter = wrapper('b', ['foo@0.3.0']); | ||
const dAfter = wrapper('d', ['foo@0.3.0']); | ||
const log = jest | ||
.spyOn(console, 'log') | ||
.mockImplementation(() => undefined); | ||
disk.globSync.mockImplementation((glob) => { | ||
if (glob.endsWith('packages/*/package.json')) { | ||
return [ | ||
'packages/a/package.json', | ||
'packages/b/package.json', | ||
'packages/c/package.json', | ||
'packages/d/package.json', | ||
]; | ||
} | ||
}); | ||
disk.readFileSync.mockImplementation((filePath) => { | ||
if (filePath.endsWith('packages/a/package.json')) return aBefore.json; | ||
if (filePath.endsWith('packages/b/package.json')) return bBefore.json; | ||
if (filePath.endsWith('packages/c/package.json')) return cBefore.json; | ||
if (filePath.endsWith('packages/d/package.json')) return dBefore.json; | ||
}); | ||
fixMismatches(getInput(disk, {}), disk); | ||
expect(disk.writeFileSync.mock.calls).toEqual([ | ||
[expect.stringContaining('packages/a/package.json'), aAfter.json], | ||
[expect.stringContaining('packages/b/package.json'), bAfter.json], | ||
[expect.stringContaining('packages/d/package.json'), dAfter.json], | ||
const scenario = scenarios.mismatchesIncludeNonSemverVersions(); | ||
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, | ||
scenario.files['packages/d/package.json'].diskWriteWhenChanged, | ||
]); | ||
expect(log.mock.calls).toEqual([ | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/a/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/b/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/-/), | ||
expect.stringMatching('packages/c/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/d/package.json'), | ||
], | ||
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, | ||
scenario.files['packages/d/package.json'].logEntryWhenChanged, | ||
]); | ||
log.mockRestore(); | ||
}); | ||
|
||
it('removes banned/disallowed dependencues', () => { | ||
const disk = mockDisk(); | ||
const aBefore = wrapper('a', ['foo@0.1.0']); | ||
const bBefore = wrapper('b', ['bar@0.2.0']); | ||
const bAfter = wrapper('b'); | ||
const log = jest | ||
.spyOn(console, 'log') | ||
.mockImplementation(() => undefined); | ||
disk.globSync.mockImplementation((glob) => { | ||
if (glob.endsWith('packages/*/package.json')) { | ||
return ['packages/a/package.json', 'packages/b/package.json']; | ||
} | ||
}); | ||
disk.readFileSync.mockImplementation((filePath) => { | ||
if (filePath.endsWith('packages/a/package.json')) { | ||
return aBefore.json; | ||
} | ||
if (filePath.endsWith('packages/b/package.json')) { | ||
return bBefore.json; | ||
} | ||
}); | ||
fixMismatches( | ||
getInput(disk, { | ||
versionGroups: [ | ||
{ | ||
dependencies: ['bar'], | ||
packages: ['**'], | ||
isBanned: true, | ||
}, | ||
], | ||
}), | ||
disk, | ||
); | ||
expect(disk.writeFileSync.mock.calls).toEqual([ | ||
[expect.stringContaining('packages/b/package.json'), bAfter.json], | ||
it('removes banned/disallowed dependencies', () => { | ||
const scenario = scenarios.dependencyIsBanned(); | ||
fixMismatches(getInput(scenario.disk, scenario.config), scenario.disk); | ||
expect(scenario.disk.writeFileSync.mock.calls).toEqual([ | ||
scenario.files['packages/b/package.json'].diskWriteWhenChanged, | ||
]); | ||
expect(log.mock.calls).toEqual([ | ||
expect(scenario.log.mock.calls).toEqual([ | ||
[expect.stringMatching(/Version Group 1/)], | ||
[ | ||
expect.stringMatching(/-/), | ||
expect.stringMatching('packages/a/package.json'), | ||
], | ||
[ | ||
expect.stringMatching(/✓/), | ||
expect.stringMatching('packages/b/package.json'), | ||
], | ||
scenario.files['packages/a/package.json'].logEntryWhenUnchanged, | ||
scenario.files['packages/b/package.json'].logEntryWhenChanged, | ||
]); | ||
|
||
log.mockRestore(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,4 +85,5 @@ lintSemverRanges( | |
source: program.opts().source, | ||
workspace: program.opts().workspace, | ||
}), | ||
disk, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'expect-more-jest'; | ||
import { scenarios } from '../../test/scenarios'; | ||
import { getInput } from '../lib/get-input'; | ||
import { lintSemverRanges } from './lint-semver-ranges'; | ||
|
||
describe('lintSemverRanges', () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('lists versions with ranges which do not match the project config', () => { | ||
const scenario = scenarios.semverRangesDoNotMatchConfig(); | ||
lintSemverRanges(getInput(scenario.disk, scenario.config), scenario.disk); | ||
expect(scenario.log.mock.calls).toEqual([ | ||
['✕ bar 2.0.0 in dependencies of a should be ~2.0.0'], | ||
['✕ foo 0.1.0 in dependencies of a should be ~0.1.0'], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,4 +68,5 @@ listMismatches( | |
source: program.opts().source, | ||
workspace: program.opts().workspace, | ||
}), | ||
disk, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'expect-more-jest'; | ||
import { scenarios } from '../../test/scenarios'; | ||
import { getInput } from '../lib/get-input'; | ||
import { listMismatches } from './list-mismatches'; | ||
|
||
describe('listMismatches', () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
|
||
it('replaces non-semver dependencies with valid semver dependencies', () => { | ||
const scenario = scenarios.mismatchesIncludeNonSemverVersions(); | ||
listMismatches(getInput(scenario.disk, scenario.config), scenario.disk); | ||
expect(scenario.log.mock.calls).toEqual([ | ||
['- foo 0.3.0'], | ||
[' link:vendor/foo-0.1.0 in dependencies of a'], | ||
[' link:vendor/foo-0.2.0 in dependencies of b'], | ||
[' 0.3.0 in dependencies of c'], | ||
[' 0.2.0 in dependencies of d'], | ||
]); | ||
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1); | ||
}); | ||
|
||
it('removes banned/disallowed dependencies', () => { | ||
const scenario = scenarios.dependencyIsBanned(); | ||
listMismatches(getInput(scenario.disk, scenario.config), scenario.disk); | ||
expect(scenario.log.mock.calls).toEqual([ | ||
[expect.stringMatching(/Version Group 1/)], | ||
['✕ bar remove this dependency'], | ||
[' 0.2.0 in dependencies of b'], | ||
]); | ||
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,4 +64,5 @@ list( | |
source: program.opts().source, | ||
workspace: program.opts().workspace, | ||
}), | ||
disk, | ||
); |
Oops, something went wrong.