/** This module is browser compatible. */ /** An error related to spying on a function or instance method. */ export declare class MockError extends Error { constructor(message: string); } /** Call information recorded by a spy. */ export interface SpyCall { /** Arguments passed to a function when called. */ args: Args; /** The value that was returned by a function. */ returned?: Return; /** The error value that was thrown by a function. */ error?: Error; /** The instance that a method was called on. */ self?: Self; } /** A function or instance method wrapper that records all calls made to it. */ export interface Spy { (this: Self, ...args: Args): Return; /** The function that is being spied on. */ original: (this: Self, ...args: Args) => Return; /** Information about calls made to the function or instance method. */ calls: SpyCall[]; /** Whether or not the original instance method has been restored. */ restored: boolean; /** If spying on an instance method, this restores the original instance method. */ restore(): void; } /** * Creates a session that tracks all mocks created before it's restored. * If a callback is provided, it restores all mocks created within it. */ export declare function mockSession(): number; export declare function mockSession(func: (this: Self, ...args: Args) => Return): (this: Self, ...args: Args) => Return; /** Creates an async session that tracks all mocks created before the promise resolves. */ export declare function mockSessionAsync(func: (this: Self, ...args: Args) => Promise): (this: Self, ...args: Args) => Promise; /** * Restores all mocks registered in the current session that have not already been restored. * If an id is provided, it will restore all mocks registered in the session associed with that id that have not already been restored. */ export declare function restore(id?: number): void; /** Wraps a function or instance method with a Spy. */ export declare function spy(): Spy; export declare function spy(func: (this: Self, ...args: Args) => Return): Spy; export declare function spy(self: Self, property: keyof Self): Spy; /** An instance method replacement that records all calls made to it. */ export interface Stub extends Spy { /** The function that is used instead of the original. */ fake: (this: Self, ...args: Args) => Return; } /** Replaces an instance method with a Stub. */ export declare function stub(self: Self, property: keyof Self): Stub; export declare function stub(self: Self, property: keyof Self, func: (this: Self, ...args: Args) => Return): Stub; /** * Asserts that a spy is called as much as expected and no more. */ export declare function assertSpyCalls(spy: Spy, expectedCalls: number): void; /** Call information recorded by a spy. */ export interface ExpectedSpyCall { /** Arguments passed to a function when called. */ args?: [...Args, ...unknown[]]; /** The instance that a method was called on. */ self?: Self; /** * The value that was returned by a function. * If you expect a promise to reject, expect error instead. */ returned?: Return; error?: { /** The class for the error that was thrown by a function. */ Class?: new (...args: any[]) => Error; /** Part of the message for the error that was thrown by a function. */ msgIncludes?: string; }; } /** * Asserts that a spy is called as expected. */ export declare function assertSpyCall(spy: Spy, callIndex: number, expected?: ExpectedSpyCall): void; /** * Asserts that an async spy is called as expected. */ export declare function assertSpyCallAsync(spy: Spy>, callIndex: number, expected?: ExpectedSpyCall | Return>): Promise; /** * Asserts that a spy is called with a specific arg as expected. */ export declare function assertSpyCallArg(spy: Spy, callIndex: number, argIndex: number, expected: ExpectedArg): ExpectedArg; /** * Asserts that an spy is called with a specific range of args as expected. * If a start and end index is not provided, the expected will be compared against all args. * If a start is provided without an end index, the expected will be compared against all args from the start index to the end. * The end index is not included in the range of args that are compared. */ export declare function assertSpyCallArgs(spy: Spy, callIndex: number, expected: ExpectedArgs): ExpectedArgs; export declare function assertSpyCallArgs(spy: Spy, callIndex: number, argsStart: number, expected: ExpectedArgs): ExpectedArgs; export declare function assertSpyCallArgs(spy: Spy, callIndex: number, argStart: number, argEnd: number, expected: ExpectedArgs): ExpectedArgs; /** Creates a function that returns the instance the method was called on. */ export declare function returnsThis(): (this: Self, ...args: Args) => Self; /** Creates a function that returns one of its arguments. */ export declare function returnsArg(idx: number): (this: Self, ...args: Arg[]) => Arg; /** Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end. */ export declare function returnsArgs(start?: number, end?: number): (this: Self, ...args: Args) => Args; /** Creates a function that returns the iterable values. Any iterable values that are errors will be thrown. */ export declare function returnsNext(values: Iterable): (this: Self, ...args: Args) => Return; /** Creates a function that resolves the awaited iterable values. Any awaited iterable values that are errors will be thrown. */ export declare function resolvesNext(iterable: Iterable> | AsyncIterable>): (this: Self, ...args: Args) => Promise;