Skip to content

Commit

Permalink
fix(vitest): skip processing getter in auto-mocked constructor call (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 6, 2023
1 parent 1e4aa8e commit cb7864a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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

0 comments on commit cb7864a

Please sign in to comment.