Skip to content

Commit

Permalink
fix(mocker): clear automocked modules on unmock (#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed Nov 21, 2022
1 parent a2e9daf commit 609185b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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)
})

0 comments on commit 609185b

Please sign in to comment.