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(vitest): skip processing getter in auto-mocked constructor call #4677

Merged
merged 1 commit into from
Dec 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,11 @@ export class VitestMocker {
// so that mock states between prototype/instances don't affect each other
// (jest reference https://github.com/jestjs/jest/blob/2c3d2409879952157433de215ae0eee5188a4384/packages/jest-mock/src/index.ts#L678-L691)
if (this instanceof newContainer[property]) {
for (const { key } of getAllMockableProperties(this, false, primitives)) {
for (const { key, descriptor } of getAllMockableProperties(this, false, primitives)) {
// skip getter since it's not mocked on prototype as well
if (descriptor.get)
continue

const value = this[key]
const type = getType(value)
const isFunction = type.includes('Function') && typeof value === 'function'
Expand Down
4 changes: 4 additions & 0 deletions test/core/src/mockedC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class MockedC {
}

set getSetProp(_val: number) {}

get getExpectNotCalled(): number {
throw new Error('automocked constructor should not call this getter')
}
}

export async function asyncFunc(): Promise<string> {
Expand Down