Skip to content

Commit 78dd795

Browse files
fix: use wrapping function instead of Vitest's spyOn (#1060)
<!-- 👋 Hi, thanks for sending a PR to console-fail-test! 📢 Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #1059 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/console-fail-test/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/console-fail-test/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Because `vi.spyOn` returns the original mock now, this assigns an intermediate function to `container[methodName]`. 📢
1 parent e6cd603 commit 78dd795

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/spies/vitest.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SpyCallArgs, SpyFactory, SpyFactoryGetter } from "./spyTypes.js";
22

33
declare interface ViSpy {
4+
(...args: unknown[]): unknown;
45
mock: ViSpyMock;
56
mockRestore(): void;
67
}
@@ -11,6 +12,7 @@ declare interface ViSpyMock {
1112

1213
declare interface Vitest {
1314
vi: {
15+
fn(): ViSpy;
1416
spyOn(container: unknown, methodName: string): ViSpy;
1517
};
1618
}
@@ -27,11 +29,15 @@ const isVitestModule = (spyLibrary: unknown): spyLibrary is Vitest => {
2729

2830
const createVitestSpyFactory = (spyLibrary: Vitest): SpyFactory => {
2931
return (container: any, methodName: string) => {
30-
const spy = spyLibrary.vi.spyOn(container, methodName);
32+
const originalMethod = container[methodName];
33+
const spy = spyLibrary.vi.fn();
34+
container[methodName] = (...args: unknown[]) => spy(...args);
3135

3236
return {
3337
getCalls: () => spy.mock.calls,
34-
restore: spy.mockRestore,
38+
restore: () => {
39+
container[methodName] = originalMethod;
40+
},
3541
};
3642
};
3743
};

0 commit comments

Comments
 (0)