|
| 1 | +import type { Mock, MockResult, MockSettledResult } from 'vitest' |
| 2 | +import { expectTypeOf, test, vi } from 'vitest' |
| 3 | + |
| 4 | +type Procedure = (...args: any[]) => any |
| 5 | + |
| 6 | +test('spy.mock when implementation is a class', () => { |
| 7 | + class Klass { |
| 8 | + constructor(_a: string, _b?: number) { |
| 9 | + // ... |
| 10 | + } |
| 11 | + |
| 12 | + static getType() { |
| 13 | + return 'Klass' |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + const Mock = vi.fn(Klass) |
| 18 | + |
| 19 | + expectTypeOf(Mock.mock.calls).toEqualTypeOf<[a: string, b?: number][]>() |
| 20 | + expectTypeOf(Mock.mock.results).toEqualTypeOf<MockResult<void>[]>() |
| 21 | + expectTypeOf(Mock.mock.contexts).toEqualTypeOf<Klass[]>() |
| 22 | + expectTypeOf(Mock.mock.instances).toEqualTypeOf<Klass[]>() |
| 23 | + expectTypeOf(Mock.mock.invocationCallOrder).toEqualTypeOf<number[]>() |
| 24 | + expectTypeOf(Mock.mock.settledResults).toEqualTypeOf<MockSettledResult<void>[]>() |
| 25 | + expectTypeOf(Mock.mock.lastCall).toEqualTypeOf<[a: string, b?: number] | undefined>() |
| 26 | + |
| 27 | + // static properties are defined |
| 28 | + expectTypeOf(Mock.getType).toBeFunction() |
| 29 | + expectTypeOf(Mock.getType).returns.toBeString() |
| 30 | + |
| 31 | + expectTypeOf(Mock).constructorParameters.toEqualTypeOf<[a: string, b?: number]>() |
| 32 | + expectTypeOf(Mock).instance.toEqualTypeOf<Klass>() |
| 33 | +}) |
| 34 | + |
| 35 | +test('spy.mock when implementation is a class-like function', () => { |
| 36 | + function Klass(this: typeof Klass, _a: string, _b?: number) { |
| 37 | + // ... |
| 38 | + } |
| 39 | + |
| 40 | + const Mock = vi.fn(Klass) |
| 41 | + |
| 42 | + expectTypeOf(Mock.mock.calls).toEqualTypeOf<[a: string, b?: number][]>() |
| 43 | + expectTypeOf(Mock.mock.results).toEqualTypeOf<MockResult<void>[]>() |
| 44 | + expectTypeOf(Mock.mock.contexts).toEqualTypeOf<typeof Klass[]>() |
| 45 | + expectTypeOf(Mock.mock.instances).toEqualTypeOf<typeof Klass[]>() |
| 46 | + expectTypeOf(Mock.mock.invocationCallOrder).toEqualTypeOf<number[]>() |
| 47 | + expectTypeOf(Mock.mock.settledResults).toEqualTypeOf<MockSettledResult<void>[]>() |
| 48 | + expectTypeOf(Mock.mock.lastCall).toEqualTypeOf<[a: string, b?: number] | undefined>() |
| 49 | + |
| 50 | + expectTypeOf(Mock).constructorParameters.toEqualTypeOf<[a: string, b?: number]>() |
| 51 | +}) |
| 52 | + |
| 53 | +test('spy.mock when implementation is a normal function', () => { |
| 54 | + function FN(_a: string, _b?: number) { |
| 55 | + return 42 |
| 56 | + } |
| 57 | + |
| 58 | + const Mock = vi.fn(FN) |
| 59 | + |
| 60 | + expectTypeOf(Mock.mock.calls).toEqualTypeOf<[a: string, b?: number][]>() |
| 61 | + expectTypeOf(Mock.mock.results).toEqualTypeOf<MockResult<number>[]>() |
| 62 | + expectTypeOf(Mock.mock.contexts).toEqualTypeOf<unknown[]>() |
| 63 | + expectTypeOf(Mock.mock.instances).toEqualTypeOf<unknown[]>() |
| 64 | + expectTypeOf(Mock.mock.invocationCallOrder).toEqualTypeOf<number[]>() |
| 65 | + expectTypeOf(Mock.mock.settledResults).toEqualTypeOf<MockSettledResult<number>[]>() |
| 66 | + expectTypeOf(Mock.mock.lastCall).toEqualTypeOf<[a: string, b?: number] | undefined>() |
| 67 | + |
| 68 | + expectTypeOf(Mock).constructorParameters.toEqualTypeOf<[a: string, b?: number]>() |
| 69 | +}) |
| 70 | + |
| 71 | +test('cann call a function mock with and without new', () => { |
| 72 | + const Mock = vi.fn(function fn(this: any) { |
| 73 | + this.test = true |
| 74 | + }) |
| 75 | + |
| 76 | + const _mockClass = new Mock() |
| 77 | + const _mockFn = Mock() |
| 78 | +}) |
| 79 | + |
| 80 | +test('cannot call class mock without new', () => { |
| 81 | + const Mock = vi.fn(class {}) |
| 82 | + |
| 83 | + const _mockClass = new Mock() |
| 84 | + // @ts-expect-error value is not callable |
| 85 | + const _mockFn = Mock() |
| 86 | +}) |
| 87 | + |
| 88 | +test('spying on a function that supports new', () => { |
| 89 | + interface ReturnClass {} |
| 90 | + interface BothFnAndClass { |
| 91 | + new (): ReturnClass |
| 92 | + (): ReturnClass |
| 93 | + } |
| 94 | + |
| 95 | + const Mock = vi.fn(function R() {} as BothFnAndClass) |
| 96 | + |
| 97 | + // supports new |
| 98 | + const _mockClass = new Mock() |
| 99 | + // supports T() |
| 100 | + const _mockFn = Mock() |
| 101 | +}) |
| 102 | + |
| 103 | +test('withImplementation returns correct type', () => { |
| 104 | + const spy = vi.fn() |
| 105 | + |
| 106 | + const result42 = spy.withImplementation(() => {}, () => { |
| 107 | + return 42 |
| 108 | + }) |
| 109 | + expectTypeOf(result42).toEqualTypeOf<Mock<Procedure>>() |
| 110 | + |
| 111 | + const resultObject = spy.withImplementation(() => {}, () => { |
| 112 | + return { then: () => 42 } |
| 113 | + }) |
| 114 | + expectTypeOf(resultObject).toEqualTypeOf<Mock<Procedure>>() |
| 115 | + |
| 116 | + const resultVoid = spy.withImplementation(() => {}, () => {}) |
| 117 | + expectTypeOf(resultVoid).toEqualTypeOf<Mock<Procedure>>() |
| 118 | + |
| 119 | + const promise42 = spy.withImplementation(() => {}, async () => { |
| 120 | + return 42 |
| 121 | + }) |
| 122 | + expectTypeOf(promise42).toEqualTypeOf<Promise<Mock<Procedure>>>() |
| 123 | + |
| 124 | + const promiseObject = spy.withImplementation(() => {}, async () => { |
| 125 | + return { hello: () => 42 } |
| 126 | + }) |
| 127 | + expectTypeOf(promiseObject).toEqualTypeOf<Promise<Mock<Procedure>>>() |
| 128 | + |
| 129 | + const promiseVoid = spy.withImplementation(() => {}, async () => {}) |
| 130 | + expectTypeOf(promiseVoid).toEqualTypeOf<Promise<Mock<Procedure>>>() |
| 131 | + |
| 132 | + const promisePromise = spy.withImplementation(() => {}, () => { |
| 133 | + return Promise.resolve() |
| 134 | + }) |
| 135 | + expectTypeOf(promisePromise).toEqualTypeOf<Promise<Mock<Procedure>>>() |
| 136 | +}) |
0 commit comments