export type AllArgsMatcher = ((args: Y, equals: jest.MatcherUtils['equals']) => boolean) & { _isAllArgsFunctionMatcher?: true; _isFunctionMatcher?: true; }; type IsAny = 0 extends (1 & T) ? true : false; type IsUnknown = IsAny extends true ? false : [unknown] extends [T] ? ([T] extends [unknown] ? true : false) : false; type NormalizeReturn = IsAny extends true ? any : IsUnknown extends true ? any : T; type NormalizeArgs = number extends TArgs['length'] ? IsAny extends true ? any[] : IsUnknown extends true ? any[] : TArgs : { [K in keyof TArgs]: IsAny extends true ? any : IsUnknown extends true ? any : TArgs[K]; }; type ResolvedValue = IsAny extends true ? any : IsUnknown extends true ? unknown : T extends PromiseLike ? U | T : T; type RejectedValue = IsAny extends true ? any : IsUnknown extends true ? unknown : T extends PromiseLike ? any : never; export type ArgumentOrMatcher = { [Index in keyof ArgTypes]: ArgTypes[Index] | jest.AsymmetricMatcher | WhenMock; }; export interface WhenMockWithMatchers { mockReturnValue(value: T): WhenMockWithMatchers & WhenMock; mockReturnValueOnce(value: T): WhenMockWithMatchers & WhenMock; mockResolvedValue(value: ResolvedValue): WhenMockWithMatchers & WhenMock; mockResolvedValueOnce(value: ResolvedValue): WhenMockWithMatchers & WhenMock; mockRejectedValue(value: RejectedValue): WhenMockWithMatchers & WhenMock; mockRejectedValueOnce(value: RejectedValue): WhenMockWithMatchers & WhenMock; mockImplementation(fn: (...args: Y) => T): WhenMockWithMatchers & WhenMock; mockImplementationOnce(fn?: (...args: Y) => T): WhenMockWithMatchers & WhenMock; defaultImplementation(fn: (...args: Y) => T): WhenMockWithMatchers & WhenMock; defaultReturnValue(value: T): WhenMockWithMatchers & WhenMock; defaultResolvedValue(value: ResolvedValue): WhenMockWithMatchers & WhenMock; defaultRejectedValue(value: RejectedValue): WhenMockWithMatchers & WhenMock; mockReset(): WhenMock; } type WhenMockChain = WhenMockWithMatchers & WhenMock; /** * The main WhenMock class that provides jest-when functionality. Not used directly by users. * Users should use the `when()` function to create WhenMock instances. * * @example * ```typescript * const mock = jest.fn(); * const whenMock = when(mock); // This line creates a WhenMock instance * * whenMock * .calledWith('hello') * .mockReturnValue('world') * .calledWith('goodbye') * .mockReturnValue('cruel world'); * * mock('hello'); // Returns: "world" * mock('goodbye'); // Returns: "cruel world" * ``` * * @template TReturn The return type of the mocked function */ export declare class WhenMock { /** * Specify the arguments that should trigger this mock behavior * @param matchers The argument matchers (literals, objects, Jest matchers, or function matchers) * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo', 42).mockReturnValue('matched'); * * fn('foo', 42); // Returns: "matched" * fn('bar', 42); // Returns: undefined * ``` */ calledWith: { (allArgsMatcher: AllArgsMatcher): WhenMockWithMatchers; (...matchers: ArgumentOrMatcher): WhenMockWithMatchers; }; /** * Specify the arguments that should trigger this mock behavior and assert they are called * @param matchers The argument matchers (literals, objects, Jest matchers, or function matchers) * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).expectCalledWith('foo').mockReturnValue('success'); * * fn('foo'); // Returns: "success" ✅ * fn('bar'); // Throws assertion error ❌ * ``` */ expectCalledWith: { (allArgsMatcher: AllArgsMatcher): WhenMockWithMatchers; (...matchers: ArgumentOrMatcher): WhenMockWithMatchers; }; /** * Set a default implementation for the mock (fallback when no specific matchers match) * @param implementation The function to call * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockReturnValue('special').defaultImplementation(() => 'default'); * * fn('foo'); // Returns: "special" * fn('bar'); // Returns: "default" * ``` */ defaultImplementation: (mockImplementation: (...args: TArgs) => TReturn) => WhenMockChain; /** * Set a default return value for the mock (fallback when no specific matchers match) * @param returnValue The value to return * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockReturnValue('special').defaultReturnValue('default'); * * fn('foo'); // Returns: "special" * fn('bar'); // Returns: "default" * ``` */ defaultReturnValue: (returnValue: TReturn) => WhenMockChain; /** * Set a default resolved Promise value for the mock (fallback when no specific matchers match) * @param returnValue The value to resolve with * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockResolvedValue('special').defaultResolvedValue('default'); * * await fn('foo'); // Returns: Promise that resolves to "special" * await fn('bar'); // Returns: Promise that resolves to "default" * ``` */ defaultResolvedValue: (returnValue: ResolvedValue) => WhenMockChain; /** * Set a default rejected Promise value for the mock (fallback when no specific matchers match) * @param err The error to reject with * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockResolvedValue('special').defaultRejectedValue(new Error('default error')); * * await fn('foo'); // Returns: Promise that resolves to "special" * await fn('bar'); // Returns: Promise that rejects with Error('default error') * ``` */ defaultRejectedValue: (err: RejectedValue) => WhenMockChain; /** * Set a return value for the mock when called with the specified arguments * @param returnValue The value to return * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockReturnValue('success'); * * fn('foo'); // Returns: "success" * ``` */ mockReturnValue: (returnValue: TReturn) => WhenMockChain; /** * Set a return value for the mock when called with the specified arguments (one-time only) * @param returnValue The value to return * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockReturnValueOnce('first').mockReturnValue('default'); * * fn('foo'); // Returns: "first" * fn('foo'); // Returns: "default" * ``` */ mockReturnValueOnce: (returnValue: TReturn) => WhenMockChain; /** * Set a resolved Promise value for the mock when called with the specified arguments * @param returnValue The value to resolve with * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockResolvedValue('async success'); * * await fn('foo'); // Returns: Promise that resolves to "async success" * ``` */ mockResolvedValue: (returnValue: ResolvedValue) => WhenMockChain; /** * Set a resolved Promise value for the mock when called with the specified arguments (one-time only) * @param returnValue The value to resolve with * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockResolvedValueOnce('first async'); * * await fn('foo'); // Returns: Promise that resolves to "first async" * await fn('foo'); // Returns: Promise that resolves to undefined * ``` */ mockResolvedValueOnce: (returnValue: ResolvedValue) => WhenMockChain; /** * Set a rejected Promise value for the mock when called with the specified arguments * @param err The error to reject with * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockRejectedValue(new Error('async error')); * * await fn('foo'); // Returns: Promise that rejects with Error('async error') * ``` */ mockRejectedValue: (err: RejectedValue) => WhenMockChain; /** * Set a rejected Promise value for the mock when called with the specified arguments (one-time only) * @param err The error to reject with * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockRejectedValueOnce(new Error('first error')); * * await fn('foo'); // Returns: Promise that rejects with Error('first error') * await fn('foo'); // Returns: Promise that resolves to undefined * ``` */ mockRejectedValueOnce: (err: RejectedValue) => WhenMockChain; /** * Set a custom implementation for the mock when called with the specified arguments * @param implementation The function to call * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockImplementation((arg) => `processed: ${arg}`); * * fn('foo'); // Returns: "processed: foo" * ``` */ mockImplementation: (implementation: (...args: TArgs) => TReturn) => WhenMockChain; /** * Set a custom implementation for the mock when called with the specified arguments (one-time only) * @param implementation The function to call * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockImplementationOnce((arg) => `first: ${arg}`); * * fn('foo'); // Returns: "first: foo" * fn('foo'); // Returns: undefined * ``` */ mockImplementationOnce: (implementation?: (...args: TArgs) => TReturn) => WhenMockChain; /** * Reset all when mocks for this function * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockReturnValue('bar'); * when(fn).calledWith('baz').mockReturnValue('qux'); * * fn.resetWhenMocks(); * fn('foo'); // Returns: undefined * fn('baz'); // Returns: undefined * ``` */ resetWhenMocks: () => void; /** * Reset the mock for the specified arguments * @returns The WhenMock instance for chaining * @example * ```typescript * const fn = jest.fn(); * when(fn).calledWith('foo').mockReturnValue('bar'); * * fn('foo'); // Returns: "bar" * * when(fn).calledWith('foo').mockReset(); * fn('foo'); // Returns: undefined * ``` */ mockReset: () => WhenMock; /** * Create a new WhenMock instance * @param fn The Jest mock or spy function to wrap */ constructor(fn: jest.Mock | jest.SpyInstance); } type ExtractFunction = Exclude extends (...args: any[]) => any ? Exclude : never; type FunctionFromMockLike = T extends { mockImplementation(fn?: infer TImplementation): any; } ? ExtractFunction : never; type SafeReturnType = T extends (...args: any) => any ? ReturnType : never; type Whenified = [FunctionFromMockLike] extends [never] ? T extends (...args: infer TArgs) => any ? WhenMock>, NormalizeArgs> : never : FunctionFromMockLike extends (...args: infer TArgs) => any ? WhenMock>>, NormalizeArgs> : never; /** * The main jest-when function for creating mocks * * Can also be used to create function matchers (see below) * * @example * ```typescript * // Create a mock * const mock = jest.fn(); * when(mock).calledWith('hello').mockReturnValue('world'); * mock('hello'); // Returns: "world" * ``` * * @example * ```typescript * // Create a function matcher * const isEven = when((n: number) => n % 2 === 0); * when(mock).calledWith(isEven).mockReturnValue('even number'); * mock(4); // Returns: "even number" * ``` */ export declare function when(fn: T): Whenified; export declare namespace when { var allArgs: (fn: (args: Y, equals: jest.MatcherUtils["equals"]) => boolean) => AllArgsMatcher; } /** * Reset all when mocks across all functions, restoring their original implementations * * @example * ```typescript * const mock1 = jest.fn(); * const mock2 = jest.fn(); * * when(mock1).calledWith('hello').mockReturnValue('world'); * when(mock2).calledWith('goodbye').mockReturnValue('cruel world'); * * resetAllWhenMocks(); * mock1('hello'); // Returns: undefined (back to original behavior) * mock2('goodbye'); // Returns: undefined (back to original behavior) * ``` */ export declare const resetAllWhenMocks: () => void; /** * Verify that all when mocks have been called, throwing an error if any were not called * * @example * ```typescript * const mock = jest.fn(); * when(mock).expectCalledWith('hello').mockReturnValue('world'); * * mock('hello'); // Returns: "world" (this mock was called) * * verifyAllWhenMocksCalled(); // ✅ Passes - all mocks were called * ``` * * @throws Error if any when mocks were not called, with details about which mocks were missed */ export declare const verifyAllWhenMocksCalled: () => void; declare const _default: { when: typeof when; resetAllWhenMocks: () => void; verifyAllWhenMocksCalled: () => void; WhenMock: typeof WhenMock; }; export default _default; //# sourceMappingURL=when.d.ts.map