Skip to content

Commit

Permalink
fix(mocker): don't restore mock to the original if module is mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 5, 2023
1 parent 1525389 commit a1ed9fb
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isNodeBuiltin } from 'vite-node/utils'
import { getWorkerState } from '../utils/global'
import { getAllMockableProperties } from '../utils/base'
import type { MockFactory, PendingSuiteMock } from '../types/mocker'
import { spyOn } from '../integrations/spy'
import { fn as spy } from '../integrations/spy'
import type { VitestExecutor } from './execute'

const filterPublicKeys = ['__esModule', Symbol.asyncIterator, Symbol.hasInstance, Symbol.isConcatSpreadable, Symbol.iterator, Symbol.match, Symbol.matchAll, Symbol.replace, Symbol.search, Symbol.split, Symbol.species, Symbol.toPrimitive, Symbol.toStringTag, Symbol.unscopables]
Expand Down Expand Up @@ -271,20 +271,9 @@ export class VitestMocker {

// Sometimes this assignment fails for some unknown reason. If it does,
// just move along.
if (!define(newContainer, property, isFunction ? value : {}))
if (!define(newContainer, property, isFunction ? spy() : {}))
continue

if (isFunction) {
const mock = spyOn(newContainer, property).mockImplementation(() => undefined)
mock.mockRestore = () => {
mock.mockReset()
mock.mockImplementation(undefined!)
return mock
}
// tinyspy retains length, but jest doesn't.
Object.defineProperty(newContainer[property], 'length', { value: 0 })
}

refs.track(value, newContainer[property])
mockPropertiesOf(value, newContainer[property])
}
Expand Down

0 comments on commit a1ed9fb

Please sign in to comment.