-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track duplicates in MockMap, refactor setThrowOnModuleCollision -> as…
…sertValid (#1406) Summary: - Refactors mock duplicate handling from "throw the next time you see an error" to "throw now if there are errors" - The new structure is more generic and more plugin-friendly. We'll use plugins for two main purporses: - Remove non-Metro concerns from Metro. - Extensibility, for using metro-file-map outside Metro for other purposes - eg processing Buck file changes. - We'll make the same change for Haste duplicates, so that Haste and Mocks are each (eventually) plugins to metro-file-map. Changelog: Internal Pull Request resolved: #1406 Test Plan: - Not observable in Metro since we set `ignorePatterns: ''` - the mock map is always null. - New unit tests, existing coverage of mock behaviour. Reviewed By: huntie Differential Revision: D67277387 Pulled By: robhogan fbshipit-source-id: 8bb8384eb7ab7a425abf49e009c478b62d13f82c
- Loading branch information
1 parent
eb17b98
commit b63a043
Showing
7 changed files
with
202 additions
and
59 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
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import type MockMapType from '../MockMap'; | ||
|
||
let mockPathModule; | ||
jest.mock('path', () => mockPathModule); | ||
|
||
describe.each([['win32'], ['posix']])('MockMap on %s', platform => { | ||
const p: string => string = filePath => | ||
platform === 'win32' | ||
? filePath.replace(/\//g, '\\').replace(/^\\/, 'C:\\') | ||
: filePath; | ||
|
||
let MockMap: Class<MockMapType>; | ||
|
||
const opts = { | ||
console, | ||
mocksPattern: /__mocks__[\/\\].+\.(js|json)$/, | ||
rootDir: p('/root'), | ||
throwOnModuleCollision: true, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
mockPathModule = jest.requireActual<{}>('path')[platform]; | ||
MockMap = require('../MockMap').default; | ||
jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('set and get a mock module', () => { | ||
const mockMap = new MockMap(opts); | ||
mockMap.onNewOrModifiedFile(p('/root/__mocks__/foo.js')); | ||
expect(mockMap.getMockModule('foo')).toBe(p('/root/__mocks__/foo.js')); | ||
}); | ||
|
||
test('assertValid throws on duplicates', () => { | ||
const mockMap = new MockMap(opts); | ||
mockMap.onNewOrModifiedFile(p('/root/__mocks__/foo.js')); | ||
mockMap.onNewOrModifiedFile(p('/root/other/__mocks__/foo.js')); | ||
|
||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(() => mockMap.assertValid()).toThrowError( | ||
`Mock map has 1 error: | ||
Duplicate manual mock found for \`foo\`: | ||
* <rootDir>/../../__mocks__/foo.js | ||
* <rootDir>/../../other/__mocks__/foo.js | ||
`.replaceAll('/', mockPathModule.sep), | ||
); | ||
}); | ||
|
||
test('recovers from duplicates', () => { | ||
const mockMap = new MockMap(opts); | ||
mockMap.onNewOrModifiedFile(p('/root/__mocks__/foo.js')); | ||
mockMap.onNewOrModifiedFile(p('/root/other/__mocks__/foo.js')); | ||
|
||
expect(() => mockMap.assertValid()).toThrow(); | ||
|
||
// Latest mock wins | ||
expect(mockMap.getMockModule('foo')).toBe( | ||
p('/root/other/__mocks__/foo.js'), | ||
); | ||
|
||
expect(mockMap.getSerializableSnapshot()).toEqual({ | ||
mocks: new Map([['foo', p('other/__mocks__/foo.js')]]), | ||
duplicates: new Map([ | ||
['foo', new Set([p('other/__mocks__/foo.js'), p('__mocks__/foo.js')])], | ||
]), | ||
}); | ||
|
||
mockMap.onRemovedFile(p('/root/other/__mocks__/foo.js')); | ||
|
||
expect(() => mockMap.assertValid()).not.toThrow(); | ||
|
||
// Recovery after the latest mock is deleted | ||
expect(mockMap.getMockModule('foo')).toBe(p('/root/__mocks__/foo.js')); | ||
|
||
expect(mockMap.getSerializableSnapshot()).toEqual({ | ||
mocks: new Map([['foo', p('__mocks__/foo.js')]]), | ||
duplicates: new Map(), | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.