Skip to content

Commit

Permalink
fix: delete mock in moduleCache when unmock (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChen928 authored Sep 2, 2022
1 parent 55049f4 commit 03f7450
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 03f7450

Please sign in to comment.