Skip to content

Commit

Permalink
fix: mocking value proxy filter Symbol static properties (#3036)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChen928 committed Mar 20, 2023
1 parent 29c4952 commit 0cf4409
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vitest/src/runtime/mocker.ts
Expand Up @@ -8,6 +8,8 @@ import { spyOn } from '../integrations/spy'
import type { MockFactory, PendingSuiteMock } from '../types/mocker'
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]

class RefTracker {
private idMap = new Map<any, number>()
private mockedValueMap = new Map<number, any>()
Expand Down Expand Up @@ -139,7 +141,7 @@ export class VitestMocker {
return target.then.bind(target)
}
else if (!(prop in target)) {
if (prop === '__esModule')
if (filterPublicKeys.includes(prop))
return undefined
const c = getColors()
throw new Error(
Expand Down
15 changes: 15 additions & 0 deletions test/core/test/mocked-public-key.test.ts
@@ -0,0 +1,15 @@
import { expect, test, vi } from 'vitest'
import { dynamicImport } from '../src/dynamic-import'

vi.mock('test', () => {
return {
foo: 'foo',
}
})

test('testing toMatchObject for mocking module', async () => {
const result = await dynamicImport('test')
expect(result).toMatchObject({
foo: 'foo',
})
})

0 comments on commit 0cf4409

Please sign in to comment.