Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/metro-file-map/src/plugins/MockPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ export default class MockPlugin
const duplicates = this.#raw.duplicates.get(mockName);
if (duplicates != null) {
const posixRelativePath = normalizePathSeparatorsToPosix(canonicalPath);
const wasActiveMock = this.#raw.mocks.get(mockName) === posixRelativePath;
duplicates.delete(posixRelativePath);
if (duplicates.size === 1) {
this.#raw.duplicates.delete(mockName);
}
// Set the mock to a remaining duplicate. Should never be empty.
const remaining = nullthrows(duplicates.values().next().value);
this.#raw.mocks.set(mockName, remaining);
// Only reassign the active mock if the file we removed *was* the active
// one; otherwise a non-active duplicate's removal would clobber it.
if (wasActiveMock) {
// Set the mock to a remaining duplicate. Should never be empty.
const remaining = nullthrows(duplicates.values().next().value);
this.#raw.mocks.set(mockName, remaining);
}
} else {
this.#raw.mocks.delete(mockName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ Duplicate manual mock found for \`foo\`:
});
});

test('removing a non-active duplicate keeps the active mock', () => {
onFileAdded(p('a/__mocks__/foo.js'));
onFileAdded(p('b/__mocks__/foo.js'));
onFileAdded(p('c/__mocks__/foo.js')); // latest wins -> active mock is c

expect(mockMap.getMockModule('foo')).toBe(p('/root/c/__mocks__/foo.js'));

// Remove a NON-active duplicate (b); the active mock (c) must be kept.
mockMap.onChanged({
addedFiles: new Map(),
modifiedFiles: new Map(),
removedFiles: new Map([[p('b/__mocks__/foo.js'), null]]),
addedDirectories: new Set(),
removedDirectories: new Set(),
});

expect(mockMap.getMockModule('foo')).toBe(p('/root/c/__mocks__/foo.js'));
});

test('loads from a snapshot', async () => {
await mockMap.initialize({
files: {
Expand Down