UNPKG

3.23 kBTypeScriptView Raw
1/// <reference types="jest" />
2declare type JMPFunctionPropertyNames<T> = {
3 [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
4}[keyof T] & string;
5declare type JMPArgsType<T> = T extends (...args: infer A) => any ? A : never;
6declare type JMPReturnType<T> = T extends (...args: any[]) => infer A ? A : never;
7/**
8 * Helper function for manually creating new spy mocks of functions not supported by this module.
9 *
10 * @param target Object containing the function that will be mocked.
11 * @param property Name of the function that will be mocked.
12 * @param impl Mock implementation of the target's function. The return type must match the target function's.
13 */
14export declare function spyOnImplementing<T extends {}, M extends JMPFunctionPropertyNames<T>, F extends T[M], I extends (...args: any[]) => JMPReturnType<F>>(target: T, property: M, impl: I): jest.SpyInstance<Required<JMPReturnType<F>>, JMPArgsType<F>>;
15/**
16 * Helper function to create a mock of the Node.js method
17 * `process.exit(code: number)`.
18 *
19 * @param {Object} err Optional error to raise. If unspecified or falsy, calling `process.exit` will resume code
20 * execution instead of raising an error.
21 */
22export declare const mockProcessExit: (err?: any) => jest.SpyInstance<never, [code?: number]>;
23/**
24 * Helper function to create a mock of the Node.js method
25 * `process.stdout.write(text: string, callback?: function): boolean`.
26 */
27export declare const mockProcessStdout: () => jest.SpyInstance<Required<boolean>, [str: string, encoding?: string, cb?: Function]>;
28/**
29 * Helper function to create a mock of the Node.js method
30 * `process.stderr.write(text: string, callback?: function): boolean`.
31 */
32export declare const mockProcessStderr: () => jest.SpyInstance<Required<boolean>, [str: string, encoding?: string, cb?: Function]>;
33/**
34 * Helper function to create a mock of the Node.js method
35 * `process.uptime()`.
36 */
37export declare const mockProcessUptime: (value?: number) => jest.SpyInstance<number, []>;
38/**
39 * Helper function to create a mock of the Node.js method
40 * `console.log(message: any)`.
41 */
42export declare const mockConsoleLog: () => jest.SpyInstance<void, [message?: any, ...optionalParams: any[]]>;
43declare type JestCallableMocksObject = {
44 [_: string]: () => jest.SpyInstance;
45};
46declare type JestMocksObject<T extends JestCallableMocksObject> = {
47 [K in keyof T]: T[K] extends () => infer J ? J : never;
48};
49export interface MockedRunResult<R, M> {
50 error?: any;
51 result?: R;
52 mocks: M;
53}
54/**
55 * Helper function to run a synchronous function with provided mocks in place, as a virtual environment.
56 *
57 * Every provided mock will be automatically restored when this function returns.
58 */
59export declare function mockedRun<T extends JestCallableMocksObject, R>(callers: T): (f: () => R) => MockedRunResult<R, JestMocksObject<T>>;
60/**
61 * Helper function to run an asynchronous function with provided mocks in place, as a virtual environment.
62 *
63 * Every provided mock will be automatically restored when this function returns.
64 */
65export declare function asyncMockedRun<T extends JestCallableMocksObject, R>(callers: T): (f: () => Promise<R>) => Promise<MockedRunResult<R, JestMocksObject<T>>>;
66export {};