UNPKG

strong-mock

Version:

Type safe mocking library for TypeScript

62 lines (61 loc) 2.44 kB
import type { ExpectationRepository } from '../expectation/repository/expectation-repository'; import type { ExpectationBuilder } from '../when/expectation-builder'; import type { InvocationCount } from './invocation-count'; export type PromiseStub<R, P> = { /** * Set the return value for the current call. * * @param value This needs to be of the same type as the value returned * by the call inside `when`. * * @example * when(() => fn()).thenReturn(Promise.resolve(23)); * * @example * when(() => fn()).thenReturn(Promise.reject({ foo: 'bar' }); */ thenReturn: (value: P) => InvocationCount; /** * Set the return value for the current call. * * @param promiseValue This needs to be of the same type as the value inside * the promise returned by the `when` callback. * * @example * when(() => fn()).thenResolve('foo'); */ thenResolve: (promiseValue: R) => InvocationCount; /** * Make the current call reject with the given error. * * @param error An `Error` instance. You can pass just a message, and * it will be wrapped in an `Error` instance. If you want to reject with * a non error then use the {@link thenReturn} method. * * @example * when(() => fn()).thenReject(new Error('oops')); */ thenReject: ((error: Error) => InvocationCount) & ((message: string) => InvocationCount) & (() => InvocationCount); }; export type NonPromiseStub<R> = { /** * Set the return value for the current call. * * @param returnValue This needs to be of the same type as the value returned * by the `when` callback. */ thenReturn: (returnValue: R) => InvocationCount; /** * Make the current call throw the given error. * * @param error The error instance. If you want to throw a simple `Error` * you can pass just the message. */ thenThrow: ((error: Error) => InvocationCount) & ((message: string) => InvocationCount) & (() => InvocationCount); }; export declare const createReturns: (builder: ExpectationBuilder, repository: ExpectationRepository) => { thenReturn: (returnValue: any) => InvocationCount; thenThrow: (errorOrMessage?: Error | string) => InvocationCount; thenResolve: (promiseValue: any) => InvocationCount; thenReject: (errorOrMessage?: Error | string) => InvocationCount; };