Wraps a function and attaches a mock version. The returned function calls the original, and .mocked() calls the mock.
.mocked()
Argument types of the function.
Return type of the function.
Original function.
Mock implementation.
Function with attached .mocked() method.
const realFn = (x: number) => x * 2;const mockFn = (x: number) => 42;const wrapped = withMock(realFn, mockFn);wrapped(3); // 6wrapped.mocked(3); // 42 Copy
const realFn = (x: number) => x * 2;const mockFn = (x: number) => 42;const wrapped = withMock(realFn, mockFn);wrapped(3); // 6wrapped.mocked(3); // 42
export function withMock<TArgs extends unknown[], TReturn>( fn: (...args: TArgs) => TReturn, mockFn: (...args: TArgs) => TReturn): ((...args: TArgs) => TReturn) & { mocked: (...args: TArgs) => TReturn } { return Object.assign(fn, { mocked: mockFn });} Copy
export function withMock<TArgs extends unknown[], TReturn>( fn: (...args: TArgs) => TReturn, mockFn: (...args: TArgs) => TReturn): ((...args: TArgs) => TReturn) & { mocked: (...args: TArgs) => TReturn } { return Object.assign(fn, { mocked: mockFn });}
Wraps a function and attaches a mock version. The returned function calls the original, and
.mocked()calls the mock.