Skip to content

Commit 65d71b9

Browse files
authoredFeb 22, 2023
fix: cleanup last mocked cache when call vi.doMock (#2872)
fix #2870
1 parent 0fc0803 commit 65d71b9

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed
 

‎docs/api/vi.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,11 @@ increment(1) === 2
221221
let mockedIncrement = 100
222222

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

230-
test('importing the next module imports mocked one', () => {
228+
test('importing the next module imports mocked one', async () => {
231229
// original import WAS NOT MOCKED, because vi.doMock is evaluated AFTER imports
232230
expect(increment(1)).toBe(2)
233231
const { increment: mockedIncrement } = await import('./increment.js')

‎packages/vitest/src/runtime/mocker.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export class VitestMocker {
6060
return this.executor.moduleCache
6161
}
6262

63+
private deleteCachedItem(id: string) {
64+
const mockId = this.getMockPath(id)
65+
if (this.moduleCache.has(mockId))
66+
this.moduleCache.delete(mockId)
67+
}
68+
6369
public getSuiteFilepath(): string {
6470
return getWorkerState().filepath || 'global'
6571
}
@@ -292,9 +298,7 @@ export class VitestMocker {
292298
if (mock && id in mock)
293299
delete mock[id]
294300

295-
const mockId = this.getMockPath(id)
296-
if (this.moduleCache.get(mockId))
297-
this.moduleCache.delete(mockId)
301+
this.deleteCachedItem(id)
298302
}
299303

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

310314
this.mockMap.set(suitefile, mocks)
311315
this.resolveCache.set(suitefile, resolves)
316+
this.deleteCachedItem(id)
312317
}
313318

314319
public async importActual<T>(rawId: string, importee: string): Promise<T> {

‎test/core/test/do-mock.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect, test, vi } from 'vitest'
2+
3+
test('doMock works', async () => {
4+
const { increment: incrementWith1 } = await import('./fixtures/increment')
5+
expect(incrementWith1(1)).toBe(2)
6+
7+
vi.doMock('./fixtures/increment', () => ({
8+
increment: (num: number) => num + 10,
9+
}))
10+
11+
const { increment: incrementWith10 } = await import('./fixtures/increment')
12+
13+
expect(incrementWith10(1)).toBe(11)
14+
})
15+
16+
test('the second doMock can override the first doMock', async () => {
17+
vi.doMock('./fixtures/increment', () => ({
18+
increment: (num: number) => num + 10,
19+
}))
20+
21+
const { increment: incrementWith1 } = await import('./fixtures/increment')
22+
23+
expect(incrementWith1(1)).toBe(11)
24+
25+
vi.doMock('./fixtures/increment', () => ({
26+
increment: (num: number) => num + 20,
27+
}))
28+
29+
const { increment: incrementWith20 } = await import('./fixtures/increment')
30+
31+
expect(incrementWith20(1)).toBe(21)
32+
})

‎test/core/test/fixtures/increment.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const increment = (num: number) => num + 1

0 commit comments

Comments
 (0)
Please sign in to comment.