// Type definitions for simple-mock // Project: https://github.com/jupiter/simple-mock // Definitions by: Leon Yu // Definitions: https://github.com/borisyankov/DefinitelyTyped declare namespace Simple { type Fn = { (...args: any[]): T } export interface Static { /** * Restores all current mocks. */ restore(): void; /** * Wraps fn in a spy and sets this on the obj, restorable with all mocks. */ mock(obj: any, key: string, fn: Fn): Stub; /** * Sets the value on this object. E.g. mock(config, 'title', 'test') is the same as config.title = 'test', but restorable with all mocks. */ mock(obj: any, key: string, mockValue: T): T; /** * If obj has already has this function, it is wrapped in a spy. The resulting spy can be turned into a stub by further configuration. Restores with all mocks. */ mock(obj: any, key: string): Stub; mock(obj: any, key: string): Stub; /** * Wraps fn in a spy. */ spy(fn: Fn): Spy; /** * Wraps fn in a spy. */ mock(fn: Fn): Spy; /** * Returns a stub function that is also a spy. */ stub(): Stub; stub(): Stub; /** * Returns a stub function that is also a spy. */ mock(): Stub; mock(): Stub; Promise?: PromiseConstructorLike; } interface Calls { /** * an array of arguments received on the call */ args: any[]; /** * first argument */ arg: any; /** * the context (this) of the call */ context: any; /** * the value returned by the wrapped function */ returned: T; /** * the error thrown by the wrapped function */ threw: Error; /** * autoincrementing number, can be compared to evaluate call order */ k: number; } export interface Spy{ (...args: any[]): T; called: boolean; /** * Number of times the function was called. */ callCount: number; calls: Calls[]; firstCall: Calls; /** * The last call object. (This is often also the first and only call.) */ lastCall: Calls; /** * Resets all counts and properties to the original state. */ reset(): void; } interface Action { /** * arguments to call back with */ cbArgs: ArrayLike; returnValue: T; throwError: Error; } export interface Stub extends Spy { /** * Configures this stub to call this function, returning its return value. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ callFn(fn: Fn): Stub; /** * Configures this stub to call the original, unstubbed function, returning its return value. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ callOriginal(): Stub; /** * Configures this stub to return with this value. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ returnWith(val: R): Stub; /** * Configures this stub to throw this error. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ throwWith(err: Error): Stub; /** * Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ callback(...args: any[]): Stub; /** * Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ callbackWith(...args: any[]): Stub; /** * Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ callbackAtIndex(cbArgumentIndex: number, ...args: any[]): Stub; /** * Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex. * Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. */ callbackArgWith(cbArgumentIndex: number, ...args: any[]): Stub; /** * Configures the last configured function or callback to be called in this context, i.e. this will be obj. */ inThisContext(obj: any): Stub; /** * Configures the stub to return a Promise (where available] resolving to this value. Same as stub.returnWith(Promise.resolve(val)). * You can use a custom Promise-conforming library, i.e. simple.Promise = require('bluebird') or simple.Promise = $q. */ resolveWith(val: V): Stub>; /** * Configures the stub to return a Promise (where available) rejecting with this error. Same as stub.returnWith(Promise.reject(val)). * You can use a custom Promise-conforming library, i.e. simple.Promise = require('bluebird') or simple.Promise = $q. */ rejectWith(val: V): Stub>; /** * An array of behaviours, each having one of these properties: */ actions: Action[]; /** * setting whether the queue of actions for this stub should repeat. * @default true */ loop: boolean; } } declare module "simple-mock" { var simple: Simple.Static; export = simple; }