diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index 5aa8c343c2e8..26ce9ff2f2c5 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/packages/vitest/src/runtime/mocker.ts @@ -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' diff --git a/test/core/src/mockedC.ts b/test/core/src/mockedC.ts index 84477be0862f..fb9e822fab4a 100644 --- a/test/core/src/mockedC.ts +++ b/test/core/src/mockedC.ts @@ -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 {