Skip to content

Commit 1a24607

Browse files
authored
fix(spy): do not mock overriden method, if parent was automocked (#9116)
1 parent 120b3da commit 1a24607

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

packages/spy/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ function createMock(
433433
// are only used by the automocker, so this doesn't matter
434434
for (const prop of prototypeMembers) {
435435
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+
436442
const isMock = isMockFunction(prototypeMock)
437443
const prototypeState = isMock ? prototypeMock.mock : undefined
438444
const prototypeConfig = isMock ? MOCK_CONFIGS.get(prototypeMock) : undefined
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class Foo {
2+
doSomething(): boolean {
3+
return false
4+
}
5+
6+
unused() {
7+
//
8+
}
9+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
})

0 commit comments

Comments
 (0)