Skip to content
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

fix: cleanup last mocked cache when call vi.doMock #2872

Merged
merged 5 commits into from Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions docs/api/vi.md
Expand Up @@ -221,13 +221,11 @@ increment(1) === 2
let mockedIncrement = 100

beforeEach(() => {
// simple doMock doesn't clear the previous cache, so we need to clear it manually here
vi.doUnmock('./increment.js')
// you can access variables inside a factory
vi.doMock('./increment.js', () => ({ increment: () => mockedIncrement++ }))
vi.doMock('./increment.js', () => ({ increment: () => ++mockedIncrement }))
})

test('importing the next module imports mocked one', () => {
test('importing the next module imports mocked one', async () => {
// original import WAS NOT MOCKED, because vi.doMock is evaluated AFTER imports
expect(increment(1)).toBe(2)
const { increment: mockedIncrement } = await import('./increment.js')
Expand Down
11 changes: 8 additions & 3 deletions packages/vitest/src/runtime/mocker.ts
Expand Up @@ -60,6 +60,12 @@ export class VitestMocker {
return this.executor.moduleCache
}

private deleteCachedItem(id: string) {
const mockId = this.getMockPath(id)
if (this.moduleCache.has(mockId))
this.moduleCache.delete(mockId)
}

public getSuiteFilepath(): string {
return getWorkerState().filepath || 'global'
}
Expand Down Expand Up @@ -292,9 +298,7 @@ export class VitestMocker {
if (mock && id in mock)
delete mock[id]

const mockId = this.getMockPath(id)
if (this.moduleCache.get(mockId))
this.moduleCache.delete(mockId)
this.deleteCachedItem(id)
}

public mockPath(originalId: string, path: string, external: string | null, factory?: MockFactory) {
Expand All @@ -309,6 +313,7 @@ export class VitestMocker {

this.mockMap.set(suitefile, mocks)
this.resolveCache.set(suitefile, resolves)
this.deleteCachedItem(id)
}

public async importActual<T>(rawId: string, importee: string): Promise<T> {
Expand Down
32 changes: 32 additions & 0 deletions test/core/test/do-mock.test.ts
@@ -0,0 +1,32 @@
import { expect, test, vi } from 'vitest'

test('doMock works', async () => {
const { increment: incrementWith1 } = await import('./fixtures/increment')
expect(incrementWith1(1)).toBe(2)

vi.doMock('./fixtures/increment', () => ({
increment: (num: number) => num + 10,
}))

const { increment: incrementWith10 } = await import('./fixtures/increment')

expect(incrementWith10(1)).toBe(11)
})

test('the second doMock can override the first doMock', async () => {
vi.doMock('./fixtures/increment', () => ({
increment: (num: number) => num + 10,
}))

const { increment: incrementWith1 } = await import('./fixtures/increment')

expect(incrementWith1(1)).toBe(11)

vi.doMock('./fixtures/increment', () => ({
increment: (num: number) => num + 20,
}))

const { increment: incrementWith20 } = await import('./fixtures/increment')

expect(incrementWith20(1)).toBe(21)
})
1 change: 1 addition & 0 deletions test/core/test/fixtures/increment.ts
@@ -0,0 +1 @@
export const increment = (num: number) => num + 1