• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcous / vitest-when / 18828710038

27 Oct 2025 03:19AM UTC coverage: 98.086% (-0.9%) from 99.029%
18828710038

push

github

web-flow
feat: update to Vitest v4 and refine types (#34)

64 of 67 branches covered (95.52%)

Branch coverage included in aggregate %.

20 of 21 new or added lines in 5 files covered. (95.24%)

141 of 142 relevant lines covered (99.3%)

183.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.47
/src/fallback-implementation.ts
1
import type { AnyFunction, MockInstance } from './types.ts'
2

3
/** Get the fallback implementation of a mock if no matching stub is found. */
4
export const getFallbackImplementation = (
27✔
5
  mock: MockInstance,
6
): AnyFunction | undefined => {
7
  return (
306✔
8
    (mock.getMockImplementation() as AnyFunction | undefined) ??
594✔
9
    getTinyspyInternals(mock)?.getOriginal()
10
  )
11
}
12

13
/** Internal state from Tinyspy, where a mock's default implementation is stored. */
14
interface TinyspyInternals {
15
  getOriginal: () => AnyFunction | undefined
16
}
17

18
/**
19
 * Get the fallback implementation out of tinyspy internals.
20
 *
21
 * This slight hack works around a bug in Vitest <= 3
22
 * where `getMockImplementation` will return `undefined` after `mockReset`,
23
 * even if a default implementation is still active.
24
 * The implementation remains present in tinyspy internal state,
25
 * which is stored on a Symbol key in the mock object.
26
 */
27
const getTinyspyInternals = (
27✔
28
  mock: MockInstance,
29
): TinyspyInternals | undefined => {
30
  const maybeTinyspy = mock as unknown as Record<PropertyKey, unknown>
288✔
31

32
  for (const key of Object.getOwnPropertySymbols(maybeTinyspy)) {
288✔
33
    const maybeTinyspyInternals = maybeTinyspy[key]
306✔
34

35
    if (
306!
36
      maybeTinyspyInternals &&
612!
37
      typeof maybeTinyspyInternals === 'object' &&
38
      'getOriginal' in maybeTinyspyInternals &&
39
      typeof maybeTinyspyInternals.getOriginal === 'function'
40
    ) {
NEW
41
      return maybeTinyspyInternals as TinyspyInternals
×
42
    }
43
  }
44

45
  return undefined
288✔
46
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc