Skip to content

Commit

Permalink
fix: delete mock in moduleCache when unmock (vitest-dev#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChen928 authored and antfu committed Sep 4, 2022
1 parent 709c694 commit 4e4984d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/vitest/src/runtime/mocker.ts
Expand Up @@ -280,6 +280,10 @@ export class VitestMocker {
const mock = this.mockMap.get(suitefile)
if (mock?.[id])
delete mock[id]

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

public mockPath(path: string, external: string | null, factory?: () => any) {
Expand Down
26 changes: 26 additions & 0 deletions test/core/test/unmock-import.test.ts
@@ -0,0 +1,26 @@
import { afterEach, beforeEach, expect, test, vi } from 'vitest'

// https://github.com/vitest-dev/vitest/issues/1940
beforeEach(() => {
vi.doMock('/data', () => ({
data: {
state: 'STARTED',
},
}))
})
afterEach(() => {
vi.doUnmock('/data')
})

test('first import', async () => {
// @ts-expect-error I know this
const { data } = await import('/data')
data.state = 'STOPPED'
expect(data.state).toBe('STOPPED')
})

test('second import should had been re-mock', async () => {
// @ts-expect-error I know this
const { data } = await import('/data')
expect(data.state).toBe('STARTED')
})

0 comments on commit 4e4984d

Please sign in to comment.