-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: fix re-mocking virtual module #9748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sheremet-va
merged 5 commits into
vitest-dev:main
from
hi-ogawa:02-26-test_add_reproduction_for_https__github.com_vitest-dev_vitest_issues_9742
Feb 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af348eb
test: add reproduction for https://github.com/vitest-dev/vitest/issue…
hi-ogawa c185f2d
test: indirect mock without reset
hi-ogawa 52d07fe
fix: fix re-mocking virtual module
hi-ogawa c587cb0
test: cleanup
hi-ogawa fe40527
chore: remove debug
hi-ogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { expect, test, vi } from 'vitest' | ||
|
|
||
| test('multiple resetModules and doMock for indirect actual module', async () => { | ||
| vi.doUnmock('./fixtures/increment') | ||
|
|
||
| const { incrementIndirect: originalIncrement } = await import('./fixtures/increment-indirect') | ||
| expect(originalIncrement(1)).toBe(2) | ||
|
|
||
| vi.doMock('./fixtures/increment', () => ({ | ||
| increment: (num: number) => num + 10, | ||
| })) | ||
| vi.resetModules() | ||
|
|
||
| const { incrementIndirect: incrementWith10 } = await import('./fixtures/increment-indirect') | ||
| expect(incrementWith10(1)).toBe(11) | ||
|
|
||
| vi.doMock('./fixtures/increment', () => ({ | ||
| increment: (num: number) => num + 20, | ||
| })) | ||
| vi.resetModules() | ||
|
|
||
| const { incrementIndirect: incrementWith20 } = await import('./fixtures/increment-indirect') | ||
| expect(incrementWith20(1)).toBe(21) | ||
|
|
||
| vi.doMock('./fixtures/increment', () => ({ | ||
| increment: (num: number) => num + 30, | ||
| })) | ||
|
|
||
| const { incrementIndirect: incrementWith20Still } = await import('./fixtures/increment-indirect') | ||
| expect(incrementWith20Still(1)).toBe(21) | ||
| }) | ||
|
|
||
| test('multiple doMock for direct virtual module', async () => { | ||
| // @ts-expect-error virtual module | ||
| const { value: originalValue } = await import('virtual-module-direct') | ||
| expect(originalValue).toBe('original-direct') | ||
|
|
||
| vi.doMock('virtual-module-direct', () => ({ | ||
| value: 'direct-1', | ||
| })) | ||
|
|
||
| // @ts-expect-error virtual module | ||
| const { value: mockedValue1 } = await import('virtual-module-direct') | ||
| expect(mockedValue1).toBe('direct-1') | ||
|
|
||
| vi.doMock('virtual-module-direct', () => ({ | ||
| value: 'direct-2', | ||
| })) | ||
|
|
||
| // @ts-expect-error virtual module | ||
| const { value: mockedValue2 } = await import('virtual-module-direct') | ||
| expect(mockedValue2).toBe('direct-2') | ||
| }) | ||
|
|
||
| test('multiple resetModules and doMock for indirect virtual module', async () => { | ||
| const { getVirtualValue: originalGetVirtualValue } = await import('./fixtures/virtual-module-indirect') | ||
| expect(originalGetVirtualValue()).toBe('original-indirect') | ||
|
|
||
| vi.doMock('virtual-module-indirect', () => ({ value: 'indirect-1' })) | ||
| vi.resetModules() | ||
|
|
||
| const { getVirtualValue: mockedGetVirtualValue1 } = await import('./fixtures/virtual-module-indirect') | ||
| expect(mockedGetVirtualValue1()).toBe('indirect-1') | ||
|
|
||
| vi.resetModules() | ||
| vi.doMock('virtual-module-indirect', () => ({ value: 'indirect-2' })) | ||
|
|
||
| const { getVirtualValue: mockedGetVirtualValue2 } = await import('./fixtures/virtual-module-indirect') | ||
| expect(mockedGetVirtualValue2()).toBe('indirect-2') | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { increment } from './increment' | ||
|
|
||
| export function incrementIndirect(num: number) { | ||
| return increment(num) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // @ts-expect-error virtual module | ||
| import { value } from 'virtual-module-indirect' | ||
|
|
||
| export function getVirtualValue() { | ||
| return value | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the case of virtual module, here we previously had
id = /@id/virtual-module(aka "url") as module runnerfetchModuleresult, which then leaded toevaluatedModuleNode.id = mock:/@id/virtual-moduleduring later mock interception.However, when invalidating mock for next
doMockcalls, we useresolveIdofvirtual-moduleand it tries to invalidatemock:virtual-module. This inconsistency makes invalidation to miss.What this
resolvedMockcase should do instead is to returnidasresolvedIdof mocked module, which is already available asresolvedMock.id, so that is the solution here.