Skip to content

Commit a3edce5

Browse files
authored
Merge pull request #63 from trurl-master/feature/cssnumberish-support
feat: add CSSNumberish support with type-safe conversion helpers
2 parents 4ae6e7b + 7d82ee5 commit a3edce5

File tree

10 files changed

+367
-68
lines changed

10 files changed

+367
-68
lines changed

global.d.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
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+
125
export {};
226

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+
338
declare global {
439
// 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;
1241
}

jest-setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ function fn() {
1414
return jest.fn();
1515
}
1616

17+
function spyOn(...args: Parameters<typeof jest.spyOn>) {
18+
return jest.spyOn(...args);
19+
}
20+
1721
globalThis.runner = {
1822
name: 'jest',
1923
useFakeTimers,
2024
useRealTimers,
2125
advanceTimersByTime,
2226
fn,
27+
spyOn,
2328
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsdom-testing-mocks",
3-
"version": "1.13.1",
3+
"version": "1.14.0",
44
"author": "Ivan Galiatin",
55
"license": "MIT",
66
"description": "A set of tools for emulating browser behavior in jsdom environment",

0 commit comments

Comments
 (0)