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: delete mock in moduleCache when unmock #1947

Merged
merged 3 commits into from Sep 2, 2022
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
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')
})