Skip to content

Commit 609185b

Browse files
authoredNov 21, 2022
fix(mocker): clear automocked modules on unmock (#2353)
1 parent a2e9daf commit 609185b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class VitestMocker {
291291
const id = this.normalizePath(path)
292292

293293
const mock = this.mockMap.get(suitefile)
294-
if (mock?.[id])
294+
if (mock && id in mock)
295295
delete mock[id]
296296

297297
const mockId = this.getMockPath(id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function helloWorld(): void {
2+
throw new Error('not implemented')
3+
}

‎test/core/test/unmock-import.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ beforeEach(() => {
88
},
99
}))
1010
})
11+
1112
afterEach(() => {
1213
vi.doUnmock('/data')
1314
})
@@ -24,3 +25,13 @@ test('second import should had been re-mock', async () => {
2425
const { data } = await import('/data')
2526
expect(data.state).toBe('STARTED')
2627
})
28+
29+
test('unmock should clear modules replaced with imitation', async () => {
30+
vi.doMock('./fixtures/mocked-dependency')
31+
const { helloWorld } = await import('./fixtures/mocked-dependency')
32+
expect(vi.isMockFunction(helloWorld)).toBe(true)
33+
34+
vi.doUnmock('./fixtures/mocked-dependency')
35+
const { helloWorld: unmocked } = await import('./fixtures/mocked-dependency')
36+
expect(vi.isMockFunction(unmocked)).toBe(false)
37+
})

0 commit comments

Comments
 (0)
Please sign in to comment.