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

mcous / vitest-when / 17318606235

29 Aug 2025 08:18AM UTC coverage: 99.01% (-1.0%) from 100.0%
17318606235

Pull #28

github

web-flow
Merge b3a096be9 into e76e73273
Pull Request #28: fix: workaround bug in vitest where getMockImplementation() does not …

70 of 71 branches covered (98.59%)

Branch coverage included in aggregate %.

6 of 7 new or added lines in 2 files covered. (85.71%)

130 of 131 relevant lines covered (99.24%)

188.52 hits per line

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

85.71
/src/fallback-implementation.ts
1
import type { AnyCallable, MockInstance } from './types.ts'
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)
306✔
10
}
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)) {
279✔
20
    const maybeTinyspyInternals = (
21
      maybeTinyspy as unknown as { [sym]: unknown }
279✔
22
    )[sym]
23
    if (
279!
24
      maybeTinyspyInternals &&
1,116✔
25
      typeof maybeTinyspyInternals === 'object' &&
26
      'getOriginal' in maybeTinyspyInternals &&
27
      typeof maybeTinyspyInternals.getOriginal === 'function'
28
    ) {
29
      // eslint-disable-next-line @typescript-eslint/no-unsafe-call
30
      return maybeTinyspyInternals.getOriginal() as TFunc
279✔
31
    }
32
  }
NEW
33
  return undefined
×
34
}
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

© 2026 Coveralls, Inc