Skip to content

Commit cb7864a

Browse files
authored
fix(vitest): skip processing getter in auto-mocked constructor call (#4677)
1 parent 1e4aa8e commit cb7864a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/vitest/src/runtime/mocker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ export class VitestMocker {
334334
// so that mock states between prototype/instances don't affect each other
335335
// (jest reference https://github.com/jestjs/jest/blob/2c3d2409879952157433de215ae0eee5188a4384/packages/jest-mock/src/index.ts#L678-L691)
336336
if (this instanceof newContainer[property]) {
337-
for (const { key } of getAllMockableProperties(this, false, primitives)) {
337+
for (const { key, descriptor } of getAllMockableProperties(this, false, primitives)) {
338+
// skip getter since it's not mocked on prototype as well
339+
if (descriptor.get)
340+
continue
341+
338342
const value = this[key]
339343
const type = getType(value)
340344
const isFunction = type.includes('Function') && typeof value === 'function'

test/core/src/mockedC.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export class MockedC {
2020
}
2121

2222
set getSetProp(_val: number) {}
23+
24+
get getExpectNotCalled(): number {
25+
throw new Error('automocked constructor should not call this getter')
26+
}
2327
}
2428

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

0 commit comments

Comments
 (0)