|
| 1 | +/* eslint-disable @typescript-eslint/no-empty-interface */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | +import { Mock as ViMock } from 'vitest'; |
| 4 | + |
| 5 | +// By re-declaring the vitest module, we can augment its types. |
| 6 | +declare module 'vitest' { |
| 7 | + /** |
| 8 | + * Augment vitest's Mock type to be compatible with jest.Mock. |
| 9 | + * This allows us to use a single, consistent type for mocks across both test runners. |
| 10 | + */ |
| 11 | + export interface Mock<A extends unknown[] = unknown[], R = unknown> |
| 12 | + extends jest.Mock<A, R> {} |
| 13 | + |
| 14 | + /** |
| 15 | + * Augment vitest's SpyInstance to be compatible with jest.SpyInstance. |
| 16 | + * Note the swapped generic arguments: |
| 17 | + * - Vitest: SpyInstance<[Args], ReturnValue> |
| 18 | + * - Jest: SpyInstance<ReturnValue, [Args]> |
| 19 | + * This declaration makes them interoperable. |
| 20 | + */ |
| 21 | + export interface SpyInstance<A extends unknown[] = unknown[], R = unknown> |
| 22 | + extends jest.SpyInstance<R, A> {} |
| 23 | +} |
| 24 | + |
1 | 25 | export {}; |
2 | 26 |
|
| 27 | +interface Runner { |
| 28 | + name: 'vi' | 'jest'; |
| 29 | + useFakeTimers: () => void; |
| 30 | + useRealTimers: () => void; |
| 31 | + advanceTimersByTime: (time: number) => Promise<void> | void; |
| 32 | + /** A generic function to create a mock function, compatible with both runners. */ |
| 33 | + fn: () => jest.Mock<unknown[], unknown>; |
| 34 | + /** A generic function to spy on a method, compatible with both runners. */ |
| 35 | + spyOn: typeof jest.spyOn; |
| 36 | +} |
| 37 | + |
3 | 38 | declare global { |
4 | 39 | // eslint-disable-next-line no-var |
5 | | - var runner: { |
6 | | - name: 'vi' | 'jest'; |
7 | | - useFakeTimers: () => void; |
8 | | - useRealTimers: () => void; |
9 | | - advanceTimersByTime: (time: number) => Promise<void>; |
10 | | - fn: () => jest.Mock<any, any>; |
11 | | - }; |
| 40 | + var runner: Runner; |
12 | 41 | } |
0 commit comments