Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mocking value proxy filter Symbol static properties #3036

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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',
})
})