|
1 | 1 | import type { AnyCallable, MockInstance } from './types.ts' |
2 | 2 |
|
3 | | -export function getFallbackImplementation<TFunc extends AnyCallable>( |
4 | | - spy: MockInstance<TFunc>, |
5 | | -) { |
6 | | - // In vitest@<4, getMockImplementation() returns undefined after calling mockReset(), even |
7 | | - // if the mock was initialized with vi.fn(impl) and impl is still active. |
8 | | - // In this case, case the actual implementation from the wrapped tinyspy object. |
9 | | - return spy.getMockImplementation() ?? getTinyspyOriginalImplementation(spy) |
| 3 | +/** Get the fallback implementation of a mock if no matching stub is found. */ |
| 4 | +export const getFallbackImplementation = <TFunc extends AnyCallable>( |
| 5 | + mock: MockInstance<TFunc>, |
| 6 | +): TFunc | undefined => { |
| 7 | + return ( |
| 8 | + mock.getMockImplementation() ?? getTinyspyInternals(mock)?.getOriginal() |
| 9 | + ) |
10 | 10 | } |
11 | 11 |
|
12 | | -function getTinyspyOriginalImplementation<TFunc extends AnyCallable>( |
13 | | - maybeTinyspy: MockInstance<TFunc>, |
14 | | -): TFunc | undefined { |
15 | | - // tinyspy stores its internal state in a symbol property of the spy instance. |
16 | | - // This state "survives" vitest's mockReset and is the basis for returning the original |
17 | | - // function's behavior (the one it was instantiated with). |
18 | | - // Note that the state's impl field is not the original implementation after a reset, but getOriginal() is. |
19 | | - for (const sym of Object.getOwnPropertySymbols(maybeTinyspy)) { |
20 | | - const maybeTinyspyInternals = ( |
21 | | - maybeTinyspy as unknown as { [sym]: unknown } |
22 | | - )[sym] |
| 12 | +/** Internal state from Tinyspy, where a mock's default implementation is stored. */ |
| 13 | +interface TinyspyInternals<TFunc extends AnyCallable> { |
| 14 | + getOriginal: () => TFunc | undefined |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Get the fallback implementation out of tinyspy internals. |
| 19 | + * |
| 20 | + * This slight hack works around a bug in Vitest <= 3 |
| 21 | + * where `getMockImplementation` will return `undefined` after `mockReset`, |
| 22 | + * even if a default implementation is still active. |
| 23 | + * The implementation remains present in tinyspy internal state, |
| 24 | + * which is stored on a Symbol key in the mock object. |
| 25 | + */ |
| 26 | +const getTinyspyInternals = <TFunc extends AnyCallable>( |
| 27 | + mock: MockInstance<TFunc>, |
| 28 | +): TinyspyInternals<TFunc> | undefined => { |
| 29 | + const maybeTinyspy = mock as unknown as Record<PropertyKey, unknown> |
| 30 | + |
| 31 | + for (const key of Object.getOwnPropertySymbols(maybeTinyspy)) { |
| 32 | + const maybeTinyspyInternals = maybeTinyspy[key] |
| 33 | + |
23 | 34 | if ( |
24 | 35 | maybeTinyspyInternals && |
25 | 36 | typeof maybeTinyspyInternals === 'object' && |
26 | 37 | 'getOriginal' in maybeTinyspyInternals && |
27 | 38 | typeof maybeTinyspyInternals.getOriginal === 'function' |
28 | 39 | ) { |
29 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-call |
30 | | - return maybeTinyspyInternals.getOriginal() as TFunc |
| 40 | + return maybeTinyspyInternals as TinyspyInternals<TFunc> |
31 | 41 | } |
32 | 42 | } |
| 43 | + |
33 | 44 | return undefined |
34 | 45 | } |
0 commit comments