Fix MockPlugin clobbering the active mock when a non-active duplicate is removed#1756
Open
durvesh1992 wants to merge 1 commit into
Open
Fix MockPlugin clobbering the active mock when a non-active duplicate is removed#1756durvesh1992 wants to merge 1 commit into
durvesh1992 wants to merge 1 commit into
Conversation
… 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).
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a manual-mock file with duplicates is removed,
MockPlugin#onFileRemovedunconditionally reassigns the active mock to an arbitrary remaining duplicate: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().valuereturns 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
Added a regression test: 3 duplicates (a, b, c → active c), remove the non-active
b, assert the active mock staysc. Fails before (becomesa), passes after. Full MockPlugin + getMockName suites: 13 pass, no regressions.