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(mocker): clear automocked modules on unmock #2353

Merged
merged 1 commit into from Nov 21, 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
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/mocker.ts
Expand Up @@ -291,7 +291,7 @@ export class VitestMocker {
const id = this.normalizePath(path)

const mock = this.mockMap.get(suitefile)
if (mock?.[id])
if (mock && id in mock)
delete mock[id]

const mockId = this.getMockPath(id)
Expand Down
3 changes: 3 additions & 0 deletions test/core/test/fixtures/mocked-dependency.ts
@@ -0,0 +1,3 @@
export function helloWorld(): void {
throw new Error('not implemented')
}
11 changes: 11 additions & 0 deletions test/core/test/unmock-import.test.ts
Expand Up @@ -8,6 +8,7 @@ beforeEach(() => {
},
}))
})

afterEach(() => {
vi.doUnmock('/data')
})
Expand All @@ -24,3 +25,13 @@ test('second import should had been re-mock', async () => {
const { data } = await import('/data')
expect(data.state).toBe('STARTED')
})

test('unmock should clear modules replaced with imitation', async () => {
vi.doMock('./fixtures/mocked-dependency')
const { helloWorld } = await import('./fixtures/mocked-dependency')
expect(vi.isMockFunction(helloWorld)).toBe(true)

vi.doUnmock('./fixtures/mocked-dependency')
const { helloWorld: unmocked } = await import('./fixtures/mocked-dependency')
expect(vi.isMockFunction(unmocked)).toBe(false)
})