[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(spy): do not mock overriden method, if parent was automocked (#9116)

authored by

Vladimir and committed by
GitHub
(Nov 27, 2025, 4:17 PM +0100) 1a24607b 120b3dac

+83
+6
packages/spy/src/index.ts
··· 433 433 // are only used by the automocker, so this doesn't matter 434 434 for (const prop of prototypeMembers) { 435 435 const prototypeMock = returnValue[prop] 436 + // the method was overidden because of inheritence, ignore it 437 + // eslint-disable-next-line ts/no-use-before-define 438 + if (prototypeMock !== mock.prototype[prop]) { 439 + continue 440 + } 441 + 436 442 const isMock = isMockFunction(prototypeMock) 437 443 const prototypeState = isMock ? prototypeMock.mock : undefined 438 444 const prototypeConfig = isMock ? MOCK_CONFIGS.get(prototypeMock) : undefined
+11
test/core/src/class-inheritence/bar.ts
··· 1 + import { Foo } from './foo.js' 2 + 3 + export class Bar extends Foo { 4 + override doSomething(): boolean { 5 + return true 6 + } 7 + 8 + doSomethingElse(): boolean { 9 + return true 10 + } 11 + }
+9
test/core/src/class-inheritence/foo.ts
··· 1 + export class Foo { 2 + doSomething(): boolean { 3 + return false 4 + } 5 + 6 + unused() { 7 + // 8 + } 9 + }
+57
test/core/test/mocking/automocking-class-inheritence.test.ts
··· 1 + // bar.spec.ts 2 + import { describe, expect, it, vi } from 'vitest' 3 + import { Bar } from '../../src/class-inheritence/bar' 4 + 5 + vi.mock(import('./../../src/class-inheritence/foo')) 6 + 7 + describe('not mocking class when parent is mocked', () => { 8 + describe('doSomething', () => { 9 + it('returns true', () => { 10 + const bar = new Bar() 11 + expect(bar.doSomething()).toBe(true) 12 + }) 13 + 14 + it('should match the prototype', () => { 15 + const bar = new Bar() 16 + expect(bar.doSomething).toBe(Bar.prototype.doSomething) 17 + }) 18 + 19 + it('should not be mocked', () => { 20 + const bar = new Bar() 21 + expect(bar.doSomething).not.toHaveProperty('mock') 22 + }) 23 + }) 24 + 25 + describe('doSomethingElse', () => { 26 + it('returns true', () => { 27 + const bar = new Bar() 28 + expect(bar.doSomethingElse()).toBe(true) 29 + }) 30 + 31 + it('should match the prototype', () => { 32 + const bar = new Bar() 33 + expect(bar.doSomethingElse).toBe(Bar.prototype.doSomethingElse) 34 + }) 35 + 36 + it('should not be mocked', () => { 37 + const bar = new Bar() 38 + expect(bar.doSomethingElse).not.toHaveProperty('mock') 39 + }) 40 + }) 41 + }) 42 + 43 + describe('mocking class when parent is not mocked', () => { 44 + it('mocks correctly', () => { 45 + class Bar { 46 + doSomething() {} 47 + } 48 + 49 + const Zoo = vi.mockObject(class Zoo extends Bar { 50 + ownMethod() {} 51 + }) 52 + 53 + const zoo = new Zoo() 54 + expect(vi.isMockFunction(zoo.doSomething)).toBe(true) 55 + expect(vi.isMockFunction(zoo.ownMethod)).toBe(true) 56 + }) 57 + })