Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Mock as ViMock } from 'vitest';

// By re-declaring the vitest module, we can augment its types.
declare module 'vitest' {
/**
* Augment vitest's Mock type to be compatible with jest.Mock.
* This allows us to use a single, consistent type for mocks across both test runners.
*/
export interface Mock<A extends unknown[] = unknown[], R = unknown>
extends jest.Mock<A, R> {}

/**
* Augment vitest's SpyInstance to be compatible with jest.SpyInstance.
* Note the swapped generic arguments:
* - Vitest: SpyInstance<[Args], ReturnValue>
* - Jest: SpyInstance<ReturnValue, [Args]>
* This declaration makes them interoperable.
*/
export interface SpyInstance<A extends unknown[] = unknown[], R = unknown>
extends jest.SpyInstance<R, A> {}
}

export {};

interface Runner {
name: 'vi' | 'jest';
useFakeTimers: () => void;
useRealTimers: () => void;
advanceTimersByTime: (time: number) => Promise<void> | void;
/** A generic function to create a mock function, compatible with both runners. */
fn: () => jest.Mock<unknown[], unknown>;
/** A generic function to spy on a method, compatible with both runners. */
spyOn: typeof jest.spyOn;
}

declare global {
// eslint-disable-next-line no-var
var runner: {
name: 'vi' | 'jest';
useFakeTimers: () => void;
useRealTimers: () => void;
advanceTimersByTime: (time: number) => Promise<void>;
fn: () => jest.Mock<any, any>;
};
var runner: Runner;
}
5 changes: 5 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ function fn() {
return jest.fn();
}

function spyOn(...args: Parameters<typeof jest.spyOn>) {
return jest.spyOn(...args);
}

globalThis.runner = {
name: 'jest',
useFakeTimers,
useRealTimers,
advanceTimersByTime,
fn,
spyOn,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsdom-testing-mocks",
"version": "1.13.1",
"version": "1.14.0",
"author": "Ivan Galiatin",
"license": "MIT",
"description": "A set of tools for emulating browser behavior in jsdom environment",
Expand Down
Loading
Loading