Skip to content

Commit 0cf4409

Browse files
authoredMar 20, 2023
fix: mocking value proxy filter Symbol static properties (#3036)
1 parent 29c4952 commit 0cf4409

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { spyOn } from '../integrations/spy'
88
import type { MockFactory, PendingSuiteMock } from '../types/mocker'
99
import type { VitestExecutor } from './execute'
1010

11+
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]
12+
1113
class RefTracker {
1214
private idMap = new Map<any, number>()
1315
private mockedValueMap = new Map<number, any>()
@@ -139,7 +141,7 @@ export class VitestMocker {
139141
return target.then.bind(target)
140142
}
141143
else if (!(prop in target)) {
142-
if (prop === '__esModule')
144+
if (filterPublicKeys.includes(prop))
143145
return undefined
144146
const c = getColors()
145147
throw new Error(
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test, vi } from 'vitest'
2+
import { dynamicImport } from '../src/dynamic-import'
3+
4+
vi.mock('test', () => {
5+
return {
6+
foo: 'foo',
7+
}
8+
})
9+
10+
test('testing toMatchObject for mocking module', async () => {
11+
const result = await dynamicImport('test')
12+
expect(result).toMatchObject({
13+
foo: 'foo',
14+
})
15+
})

0 commit comments

Comments
 (0)
Please sign in to comment.