Skip to content

Commit

Permalink
fix: cleanup last mocked cache when call vi.doMock (#2872)
Browse files Browse the repository at this point in the history
fix #2870
  • Loading branch information
mysteryven committed Feb 22, 2023
1 parent 0fc0803 commit 65d71b9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
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

0 comments on commit 65d71b9

Please sign in to comment.