|
| 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