Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

Callable

  • SinonExpectation(...args: any[]): any
  • Parameters

    • Rest ...args: any[]

    Returns any

Index

Properties

args

args: any[][]

Array of arguments received, spy.args[0] is an array of arguments received in the first call.

callCount

callCount: number

The number of recorded calls.

called

called: boolean

true if the spy was called at least once

calledOnce

calledOnce: boolean

true if spy was called exactly once

calledThrice

calledThrice: boolean

true if the spy was called exactly thrice

calledTwice

calledTwice: boolean

true if the spy was called exactly twice

exceptions

exceptions: any[]

Array of exception objects thrown, spy.exceptions[0] is the exception thrown by the first call. If the call did not throw an error, the value at the call’s location in .exceptions will be undefined.

firstCall

firstCall: SinonSpyCall<any[], any>

The first call

lastCall

lastCall: SinonSpyCall<any[], any>

The last call

notCalled

notCalled: boolean

true if the spy was not called

returnValues

returnValues: any[]

Array of return values, spy.returnValues[0] is the return value of the first call. If the call did not explicitly return a value, the value at the call’s location in .returnValues will be undefined.

secondCall

secondCall: SinonSpyCall<any[], any>

The second call

thirdCall

thirdCall: SinonSpyCall<any[], any>

The third call

thisValues

thisValues: any[]

Array of this objects, spy.thisValues[0] is the this object for the first call.

Methods

alwaysCalledOn

  • alwaysCalledOn(obj: any): boolean
  • Returns true if the spy was always called with @param obj as this.

    Parameters

    • obj: any

    Returns boolean

alwaysCalledWith

  • alwaysCalledWith(...args: any[]): boolean
  • Returns true if spy was always called with the provided arguments (and possibly others).

    Parameters

    • Rest ...args: any[]

    Returns boolean

alwaysCalledWithExactly

  • alwaysCalledWithExactly(...args: any[]): boolean
  • Returns true if spy was always called with the exact provided arguments.

    Parameters

    • Rest ...args: any[]

    Returns boolean

alwaysCalledWithMatch

  • alwaysCalledWithMatch(...args: any[]): boolean
  • Returns true if spy was always called with matching arguments (and possibly others). This behaves the same as spy.alwaysCalledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    • Rest ...args: any[]

    Returns boolean

alwaysReturned

  • alwaysReturned(obj: any): boolean
  • Returns true if spy always returned the provided value.

    Parameters

    • obj: any

    Returns boolean

alwaysThrew

  • alwaysThrew(): boolean
  • alwaysThrew(type: string): boolean
  • alwaysThrew(obj: any): boolean
  • Returns true if spy always threw an exception.

    Returns boolean

  • Returns true if spy always threw an exception of the provided type.

    Parameters

    • type: string

    Returns boolean

  • Returns true if spy always threw the provided exception object.

    Parameters

    • obj: any

    Returns boolean

atLeast

  • Specify the minimum amount of calls expected.

    Parameters

    • n: number

    Returns SinonExpectation

atMost

  • Specify the maximum amount of calls expected.

    Parameters

    • n: number

    Returns SinonExpectation

callArg

  • callArg(pos: number): unknown[]
  • Like yield, but with an explicit argument number specifying which callback to call. Useful if a function is called with more than one callback, and simply calling the first callback is not desired.

    Parameters

    • pos: number

    Returns unknown[]

callArgOn

  • callArgOn(pos: number, obj: any, ...args: any[]): unknown[]
  • Parameters

    • pos: number
    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

callArgOnWith

  • callArgOnWith(pos: number, obj: any, ...args: any[]): unknown[]
  • Parameters

    • pos: number
    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

callArgWith

  • callArgWith(pos: number, ...args: any[]): unknown[]
  • Like callArg, but with arguments.

    Parameters

    • pos: number
    • Rest ...args: any[]

    Returns unknown[]

callThrough

  • Causes the original method wrapped into the stub to be called when none of the conditional stubs are matched.

    Returns SinonStub<any[], any>

calledAfter

  • calledAfter(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if the spy was called after @param anotherSpy

    Parameters

    Returns boolean

calledBefore

  • calledBefore(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if the spy was called before @param anotherSpy

    Parameters

    Returns boolean

calledImmediatelyAfter

  • calledImmediatelyAfter(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if spy was called after @param anotherSpy, and no spy calls occurred between @param anotherSpy and spy.

    Parameters

    Returns boolean

calledImmediatelyBefore

  • calledImmediatelyBefore(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if spy was called before @param anotherSpy, and no spy calls occurred between spy and @param anotherSpy.

    Parameters

    Returns boolean

calledOn

  • calledOn(obj: any): boolean
  • Returns true if the spy was called at least once with @param obj as this. calledOn also accepts a matcher spyCall.calledOn(sinon.match(fn)) (see matchers).

    Parameters

    • obj: any

    Returns boolean

calledOnceWith

  • calledOnceWith(...args: any[]): boolean
  • Returns true if spy was called at exactly once with the provided arguments.

    Parameters

    • Rest ...args: any[]

    Returns boolean

calledOnceWithExactly

  • calledOnceWithExactly(...args: any[]): boolean

calledWith

  • calledWith(...args: any[]): boolean
  • Returns true if spy was called at least once with the provided arguments. Can be used for partial matching, Sinon only checks the provided arguments against actual arguments, so a call that received the provided arguments (in the same spots) and possibly others as well will return true.

    Parameters

    • Rest ...args: any[]

    Returns boolean

calledWithExactly

  • calledWithExactly(...args: any[]): boolean
  • Returns true if spy was called at least once with the provided arguments and no others.

    Parameters

    • Rest ...args: any[]

    Returns boolean

calledWithMatch

  • calledWithMatch(...args: any[]): boolean
  • Returns true if spy was called with matching arguments (and possibly others). This behaves the same as spy.calledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    • Rest ...args: any[]

    Returns boolean

calledWithNew

  • calledWithNew(): boolean
  • Returns true if spy/stub was called the new operator. Beware that this is inferred based on the value of the this object and the spy function’s prototype, so it may give false positives if you actively return the right kind of object.

    Returns boolean

callsArg

  • callsArg(index: number): SinonStub<any[], any>
  • Causes the stub to call the argument at the provided index as a callback function. stub.callsArg(0); causes the stub to call the first argument as a callback. If the argument at the provided index is not available or is not a function, a TypeError will be thrown.

    Parameters

    • index: number

    Returns SinonStub<any[], any>

callsArgAsync

  • callsArgAsync(index: number): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • index: number

    Returns SinonStub<any[], any>

callsArgOn

  • callsArgOn(index: number, context: any): SinonStub<any[], any>
  • Like stub.callsArg(index); but with an additional parameter to pass the this context.

    Parameters

    • index: number
    • context: any

    Returns SinonStub<any[], any>

callsArgOnAsync

  • callsArgOnAsync(index: number, context: any): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • index: number
    • context: any

    Returns SinonStub<any[], any>

callsArgOnWith

  • callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub<any[], any>
  • Like above but with an additional parameter to pass the this context.

    Parameters

    • index: number
    • context: any
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

callsArgOnWithAsync

  • callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • index: number
    • context: any
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

callsArgWith

  • callsArgWith(index: number, ...args: any[]): SinonStub<any[], any>
  • Like callsArg, but with arguments to pass to the callback.

    Parameters

    • index: number
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

callsArgWithAsync

  • callsArgWithAsync(index: number, ...args: any[]): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • index: number
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

callsFake

  • callsFake(func: (...args: any[]) => any): SinonStub<any[], any>
  • Makes the stub call the provided @param func when invoked.

    Parameters

    • func: (...args: any[]) => any
        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    Returns SinonStub<any[], any>

exactly

  • Expect the method to be called exactly @param n times.

    Parameters

    • n: number

    Returns SinonExpectation

get

  • get(func: () => any): SinonStub<any[], any>
  • Replaces a new getter for this stub.

    Parameters

    • func: () => any
        • (): any
        • Returns any

    Returns SinonStub<any[], any>

getCall

  • Returns the nth call. Accessing individual calls helps with more detailed behavior verification when the spy is called more than once.

    Parameters

    • n: number

      Zero based index of the spy call.

    Returns SinonSpyCall<any[], any>

getCalls

  • Returns an Array of all calls recorded by the spy.

    Returns SinonSpyCall<any[], any>[]

invokeCallback

  • invokeCallback(...args: any[]): void
  • Invoke callbacks passed to the stub with the given arguments. If the stub was never called with a function argument, yield throws an error. Returns an Array with all callbacks return values in the order they were called, if no error is thrown.

    Parameters

    • Rest ...args: any[]

    Returns void

named

  • Set the displayName of the spy or stub.

    Parameters

    • name: string

    Returns SinonStub<any[], any>

never

  • Expect the method to never be called.

    Returns SinonExpectation

neverCalledWith

  • neverCalledWith(...args: any[]): boolean
  • Returns true if the spy/stub was never called with the provided arguments.

    Parameters

    • Rest ...args: any[]

    Returns boolean

neverCalledWithMatch

  • neverCalledWithMatch(...args: any[]): boolean
  • Returns true if the spy/stub was never called with matching arguments. This behaves the same as spy.neverCalledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    • Rest ...args: any[]

    Returns boolean

notCalledWith

  • notCalledWith(...args: any[]): boolean
  • Returns true if call did not receive provided arguments.

    Parameters

    • Rest ...args: any[]

    Returns boolean

notCalledWithMatch

  • notCalledWithMatch(...args: any[]): boolean
  • Returns true if call did not receive matching arguments. This behaves the same as spyCall.notCalledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    • Rest ...args: any[]

    Returns boolean

on

  • Parameters

    • obj: any

    Returns SinonExpectation

onCall

  • Defines the behavior of the stub on the @param n call. Useful for testing sequential interactions. There are methods onFirstCall, onSecondCall,onThirdCall to make stub definitions read more naturally. onCall can be combined with all of the behavior defining methods in this section. In particular, it can be used together with withArgs.

    Parameters

    • n: number

    Returns SinonStub<any[], any>

onFirstCall

  • Alias for stub.onCall(0);

    Returns SinonStub<any[], any>

onSecondCall

onThirdCall

  • Alias for stub.onCall(2);

    Returns SinonStub<any[], any>

once

  • Expect the method to be called exactly once.

    Returns SinonExpectation

printf

  • printf(format: string, ...args: any[]): string
  • Returns the passed format string with the following replacements performed:

    • %n - the name of the spy "spy" by default)
    • %c - the number of times the spy was called, in words ("once", "twice", etc.)
    • %C - a list of string representations of the calls to the spy, with each call prefixed by a newline and four spaces
    • %t - a comma-delimited list of this values the spy was called on
    • %n - the formatted value of the nth argument passed to printf
    • %* - a comma-delimited list of the (non-format string) arguments passed to printf
    • %D - a multi-line list of the arguments received by all calls to the spy

    Parameters

    • format: string
    • Rest ...args: any[]

    Returns string

rejects

  • Causes the stub to return a Promise which rejects with an exception (Error). When constructing the Promise, sinon uses the Promise.reject method. You are responsible for providing a polyfill in environments which do not provide Promise. The Promise library can be overwritten using the usingPromise method. Since sinon@2.0.0

    Returns SinonStub<any[], any>

  • Causes the stub to return a Promise which rejects with an exception of the provided type. Since sinon@2.0.0

    Parameters

    • errorType: string

    Returns SinonStub<any[], any>

  • Causes the stub to return a Promise which rejects with the provided exception object. Since sinon@2.0.0

    Parameters

    • value: any

    Returns SinonStub<any[], any>

reset

  • reset(): void
  • Resets both behaviour and history of the stub. This is equivalent to calling both stub.resetBehavior() and stub.resetHistory() Updated in sinon@2.0.0 Since sinon@5.0.0 As a convenience, you can apply stub.reset() to all stubs using sinon.reset()

    Returns void

resetBehavior

  • resetBehavior(): void
  • Resets the stub’s behaviour to the default behaviour You can reset behaviour of all stubs using sinon.resetBehavior()

    Returns void

resetHistory

  • resetHistory(): void
  • Resets the state of a spy.

    Returns void

resolves

  • resolves(value?: any): SinonStub<any[], any>
  • Causes the stub to return a Promise which resolves to the provided value. When constructing the Promise, sinon uses the Promise.resolve method. You are responsible for providing a polyfill in environments which do not provide Promise. The Promise library can be overwritten using the usingPromise method. Since sinon@2.0.0

    Parameters

    • Optional value: any

    Returns SinonStub<any[], any>

resolvesArg

  • resolvesArg(index: number): SinonStub<any[], any>
  • Causes the stub to return a Promise which resolves to the argument at the provided index. stub.resolvesArg(0); causes the stub to return a Promise which resolves to the first argument. If the argument at the provided index is not available, a TypeError will be thrown.

    Parameters

    • index: number

    Returns SinonStub<any[], any>

resolvesThis

  • Causes the stub to return a Promise which resolves to its this value.

    Returns SinonStub<any[], any>

restore

  • restore(): void
  • Restores all mocked methods.

    Returns void

returned

  • returned(value: any): boolean
  • Returns true if spy returned the provided value at least once. Uses deep comparison for objects and arrays. Use spy.returned(sinon.match.same(obj)) for strict comparison (see matchers).

    Parameters

    • value: any

    Returns boolean

returns

  • Makes the stub return the provided @param obj value.

    Parameters

    • obj: any

    Returns SinonStub<any[], any>

returnsArg

  • returnsArg(index: number): SinonStub<any[], any>
  • Causes the stub to return the argument at the provided @param index. stub.returnsArg(0); causes the stub to return the first argument. If the argument at the provided index is not available, prior to sinon@6.1.2, an undefined value will be returned; starting from sinon@6.1.2, a TypeError will be thrown.

    Parameters

    • index: number

    Returns SinonStub<any[], any>

returnsThis

  • Causes the stub to return its this value. Useful for stubbing jQuery-style fluent APIs.

    Returns SinonStub<any[], any>

set

  • set(func: (v: any) => void): SinonStub<any[], any>
  • Defines a new setter for this stub.

    Parameters

    • func: (v: any) => void
        • (v: any): void
        • Parameters

          • v: any

          Returns void

    Returns SinonStub<any[], any>

threw

  • threw(): boolean
  • threw(type: string): boolean
  • threw(obj: any): boolean
  • Returns true if spy threw an exception at least once.

    Returns boolean

  • Returns true if spy threw an exception of the provided type at least once.

    Parameters

    • type: string

    Returns boolean

  • Returns true if spy threw the provided exception object at least once.

    Parameters

    • obj: any

    Returns boolean

thrice

  • Expect the method to be called exactly thrice.

    Returns SinonExpectation

throws

  • Causes the stub to throw an exception (Error).

    Parameters

    • Optional type: string

    Returns SinonStub<any[], any>

  • Causes the stub to throw the provided exception object.

    Parameters

    • obj: any

    Returns SinonStub<any[], any>

throwsArg

  • throwsArg(index: number): SinonStub<any[], any>
  • Causes the stub to throw the argument at the provided index. stub.throwsArg(0); causes the stub to throw the first argument as the exception. If the argument at the provided index is not available, a TypeError will be thrown. Since sinon@2.3.0

    Parameters

    • index: number

    Returns SinonStub<any[], any>

throwsException

  • throwsException(type?: string): SinonStub<any[], any>
  • throwsException(obj: any): SinonStub<any[], any>

twice

  • Expect the method to be called exactly twice.

    Returns SinonExpectation

usingPromise

  • usingPromise(promiseLibrary: any): SinonStub<any[], any>
  • Causes the stub to return promises using a specific Promise library instead of the global one when using stub.rejects or stub.resolves. Returns the stub to allow chaining.

    Parameters

    • promiseLibrary: any

    Returns SinonStub<any[], any>

value

  • Defines a new value for this stub.

    Parameters

    • val: any

    Returns SinonStub<any[], any>

verify

  • Verifies all expectations on the mock. If any expectation is not satisfied, an exception is thrown. Also restores the mocked methods.

    Returns SinonExpectation

withArgs

  • Expect the method to be called with the provided arguments and possibly others. An expectation instance only holds onto a single set of arguments specified with withArgs. Subsequent calls will overwrite the previously-specified set of arguments (even if they are different), so it is generally not intended that this method be invoked more than once per test case.

    Parameters

    • Rest ...args: any[]

    Returns SinonExpectation

withExactArgs

  • Expect the method to be called with the provided arguments and no others. An expectation instance only holds onto a single set of arguments specified with withExactArgs. Subsequent calls will overwrite the previously-specified set of arguments (even if they are different), so it is generally not intended that this method be invoked more than once per test case.

    Parameters

    • Rest ...args: any[]

    Returns SinonExpectation

wrappedMethod

  • wrappedMethod(...args: TArgs): TReturnValue
  • Holds a reference to the original method/function this stub has wrapped.

    Parameters

    • Rest ...args: TArgs

    Returns TReturnValue

yield

  • yield(...args: any[]): unknown[]
  • Invoke callbacks passed to the stub with the given arguments. If the stub was never called with a function argument, yield throws an error. Returns an Array with all callbacks return values in the order they were called, if no error is thrown. Also aliased as invokeCallback.

    Parameters

    • Rest ...args: any[]

    Returns unknown[]

yieldOn

  • yieldOn(obj: any, ...args: any[]): unknown[]
  • Parameters

    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

yieldTo

  • yieldTo(property: string, ...args: any[]): unknown[]
  • Invokes callbacks passed as a property of an object to the stub. Like yield, yieldTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.

    Parameters

    • property: string
    • Rest ...args: any[]

    Returns unknown[]

yieldToOn

  • yieldToOn(property: string, obj: any, ...args: any[]): unknown[]
  • Parameters

    • property: string
    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

yields

  • yields(...args: any[]): SinonStub<any[], any>
  • Similar to callsArg. Causes the stub to call the first callback it receives with the provided arguments (if any). If a method accepts more than one callback, you need to use callsArg to have the stub invoke other callbacks than the first one.

    Parameters

    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsAsync

  • yieldsAsync(...args: any[]): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsOn

  • yieldsOn(context: any, ...args: any[]): SinonStub<any[], any>
  • Like above but with an additional parameter to pass the this context.

    Parameters

    • context: any
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsOnAsync

  • yieldsOnAsync(context: any, ...args: any[]): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • context: any
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsRight

  • yieldsRight(...args: any[]): SinonStub<any[], any>
  • Parameters

    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsTo

  • yieldsTo(property: string, ...args: any[]): SinonStub<any[], any>
  • Causes the spy to invoke a callback passed as a property of an object to the spy. Like yields, yieldsTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.

    Parameters

    • property: string
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsToAsync

  • yieldsToAsync(property: string, ...args: any[]): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • property: string
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsToOn

  • yieldsToOn(property: string, context: any, ...args: any[]): SinonStub<any[], any>
  • Like above but with an additional parameter to pass the this context.

    Parameters

    • property: string
    • context: any
    • Rest ...args: any[]

    Returns SinonStub<any[], any>

yieldsToOnAsync

  • yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub<any[], any>
  • Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed. In Node environment the callback is deferred with process.nextTick. In a browser the callback is deferred with setTimeout(callback, 0).

    Parameters

    • property: string
    • context: any
    • Rest ...args: any[]

    Returns SinonStub<any[], any>