Skip to content

Navigation Menu

Sign in
Sign up

Fix MockPlugin clobbering the active mock when a non-active duplicate is removed - #1756

Open
durvesh1992 wants to merge 1 commit into
react:main from
durvesh1992:fix/mockplugin-remove-nonactive-duplicate
Open

Fix MockPlugin clobbering the active mock when a non-active duplicate is removed #1756
durvesh1992 wants to merge 1 commit into
react:main from
durvesh1992:fix/mockplugin-remove-nonactive-duplicate

Conversation

@durvesh1992

@durvesh1992 durvesh1992 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

When a manual-mock file with duplicates is removed, MockPlugin#onFileRemoved unconditionally reassigns the active mock to an arbitrary remaining duplicate:

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); // always overwrites the active mock

It never checks whether the removed file was the active mock (this.#raw.mocks.get(mockName)). When a non-active duplicate is removed and 3+ duplicates exist, values().next().value returns the first-inserted path, silently changing which mock resolves — even though the previously-active mock still exists on disk.

The existing 2-duplicate "recovers from duplicates" test masks this because the single remaining element is coincidentally always the right answer.

Fix

Capture whether the removed file was the active mock before deleting, and only reassign in that case.

Test plan

jest packages/metro-file-map/src/plugins/mocks/__tests__/

Added a regression test: 3 duplicates (a, b, c → active c), remove the non-active b, assert the active mock stays c. Fails before (becomes a), passes after. Full MockPlugin + getMockName suites: 13 pass, no regressions.

... is removed
When a manual mock with duplicates was removed, #onFileRemoved
unconditionally reassigned the active mock to an arbitrary remaining
duplicate (the first in the Set). If the removed file was NOT the active
mock and 3+ duplicates existed, this silently changed which __mocks__
file resolves for that name — even though the active mock still existed.
Only reassign when the removed file was the active mock; otherwise leave
it untouched.
Adds a regression test (3 duplicates, remove a non-active one; fails
before / passes after).
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 30, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /