From e63af8d856a7c09ee8e34e21cdb4fc59a4b843c7 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Thu, 28 Feb 2019 20:51:56 +0100 Subject: [PATCH] Extract inspectable spy API into own interface --- types/sinon/ts3.1/index.d.ts | 89 ++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/types/sinon/ts3.1/index.d.ts b/types/sinon/ts3.1/index.d.ts index aefc93f624..924fa3936e 100644 --- a/types/sinon/ts3.1/index.d.ts +++ b/types/sinon/ts3.1/index.d.ts @@ -145,7 +145,32 @@ declare namespace Sinon { calledAfter(call: SinonSpyCall): boolean; } - interface SinonSpy + /** + * A test spy is a function that records arguments, return value, + * the value of this and exception thrown (if any) for all its calls. + */ + interface SinonSpy extends SinonInspectable { + // Methods + (...args: TArgs): TReturnValue; + + /** + * Creates a spy that only records calls when the received arguments match those passed to withArgs. + * This is useful to be more expressive in your assertions, where you can access the spy with the same call. + * @param args Expected args + */ + withArgs(...args: MatchArguments): SinonSpy; + + /** + * Set the displayName of the spy or stub. + * @param name + */ + named(name: string): SinonSpy; + } + + /** + * The part of the spy API that allows inspecting the calls made on a spy. + */ + interface SinonInspectable extends Pick< SinonSpyCallApi, Exclude, 'args'> @@ -209,9 +234,6 @@ declare namespace Sinon { * If the call did not explicitly return a value, the value at the call’s location in .returnValues will be undefined. */ returnValues: TReturnValue[]; - - // Methods - (...args: TArgs): TReturnValue; /** * Returns true if the spy was called before @param anotherSpy * @param anotherSpy @@ -232,12 +254,6 @@ declare namespace Sinon { * @param anotherSpy */ calledImmediatelyAfter(anotherSpy: SinonSpy): boolean; - /** - * Creates a spy that only records calls when the received arguments match those passed to withArgs. - * This is useful to be more expressive in your assertions, where you can access the spy with the same call. - * @param args Expected args - */ - withArgs(...args: MatchArguments): SinonSpy; /** * Returns true if the spy was always called with @param obj as this. * @param obj @@ -292,11 +308,6 @@ declare namespace Sinon { * Returns an Array with all callbacks return values in the order they were called, if no error is thrown. */ invokeCallback(...args: TArgs): void; - /** - * Set the displayName of the spy or stub. - * @param name - */ - named(name: string): SinonSpy; /** * Returns the nth call. * Accessing individual calls helps with more detailed behavior verification when the spy is called more than once. @@ -1155,60 +1166,60 @@ declare namespace Sinon { * Passes if spy was never called * @param spy */ - notCalled(spy: SinonSpy): void; + notCalled(spy: SinonInspectable): void; /** * Passes if spy was called at least once. */ - called(spy: SinonSpy): void; + called(spy: SinonInspectable): void; /** * Passes if spy was called once and only once. */ - calledOnce(spy: SinonSpy): void; + calledOnce(spy: SinonInspectable): void; /** * Passes if spy was called exactly twice. */ - calledTwice(spy: SinonSpy): void; + calledTwice(spy: SinonInspectable): void; /** * Passes if spy was called exactly three times. */ - calledThrice(spy: SinonSpy): void; + calledThrice(spy: SinonInspectable): void; /** * Passes if spy was called exactly num times. */ - callCount(spy: SinonSpy, count: number): void; + callCount(spy: SinonInspectable, count: number): void; /** * Passes if provided spies were called in the specified order. * @param spies */ - callOrder(...spies: Array>): void; + callOrder(...spies: Array): void; /** * Passes if spy was ever called with obj as its this value. * It’s possible to assert on a dedicated spy call: sinon.assert.calledOn(spy.firstCall, arg1, arg2, ...);. */ - calledOn(spyOrSpyCall: SinonSpy | SinonSpyCall, obj: any): void; + calledOn(spyOrSpyCall: SinonInspectable | SinonSpyCall, obj: any): void; /** * Passes if spy was always called with obj as its this value. */ - alwaysCalledOn(spy: SinonSpy, obj: any): void; + alwaysCalledOn(spy: SinonInspectable, obj: any): void; /** * Passes if spy was called with the provided arguments. * It’s possible to assert on a dedicated spy call: sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);. * @param spyOrSpyCall * @param args */ - calledWith(spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: MatchArguments): void; + calledWith(spyOrSpyCall: SinonInspectable | SinonSpyCall, ...args: MatchArguments): void; /** * Passes if spy was always called with the provided arguments. * @param spy * @param args */ - alwaysCalledWith(spy: SinonSpy, ...args: MatchArguments): void; + alwaysCalledWith(spy: SinonInspectable, ...args: MatchArguments): void; /** * Passes if spy was never called with the provided arguments. * @param spy * @param args */ - neverCalledWith(spy: SinonSpy, ...args: MatchArguments): void; + neverCalledWith(spy: SinonInspectable, ...args: MatchArguments): void; /** * Passes if spy was called with the provided arguments and no others. * It’s possible to assert on a dedicated spy call: sinon.assert.calledWithExactly(spy.getCall(1), arg1, arg2, ...);. @@ -1216,68 +1227,68 @@ declare namespace Sinon { * @param args */ calledWithExactly( - spyOrSpyCall: SinonSpy | SinonSpyCall, + spyOrSpyCall: SinonInspectable | SinonSpyCall, ...args: MatchArguments ): void; /** * Passes if spy was always called with the provided arguments and no others. */ - alwaysCalledWithExactly(spy: SinonSpy, ...args: MatchArguments): void; + alwaysCalledWithExactly(spy: SinonInspectable, ...args: MatchArguments): void; /** * Passes if spy was called with matching arguments. * This behaves the same way as sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...). * It’s possible to assert on a dedicated spy call: sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);. */ calledWithMatch( - spyOrSpyCall: SinonSpy | SinonSpyCall, + spyOrSpyCall: SinonInspectable | SinonSpyCall, ...args: TArgs ): void; /** * Passes if spy was always called with matching arguments. * This behaves the same way as sinon.assert.alwaysCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...). */ - alwaysCalledWithMatch(spy: SinonSpy, ...args: TArgs): void; + alwaysCalledWithMatch(spy: SinonInspectable, ...args: TArgs): void; /** * Passes if spy was never called with matching arguments. * This behaves the same way as sinon.assert.neverCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...). * @param spy * @param args */ - neverCalledWithMatch(spy: SinonSpy, ...args: TArgs): void; + neverCalledWithMatch(spy: SinonInspectable, ...args: TArgs): void; /** * Passes if spy was called with the new operator. * It’s possible to assert on a dedicated spy call: sinon.assert.calledWithNew(spy.secondCall, arg1, arg2, ...);. * @param spyOrSpyCall */ - calledWithNew(spyOrSpyCall: SinonSpy | SinonSpyCall): void; + calledWithNew(spyOrSpyCall: SinonInspectable | SinonSpyCall): void; /** * Passes if spy threw any exception. */ - threw(spyOrSpyCall: SinonSpy | SinonSpyCall): void; + threw(spyOrSpyCall: SinonInspectable | SinonSpyCall): void; /** * Passes if spy threw the given exception. * The exception is an actual object. * It’s possible to assert on a dedicated spy call: sinon.assert.threw(spy.thirdCall, exception);. */ - threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: string): void; + threw(spyOrSpyCall: SinonInspectable | SinonSpyCall, exception: string): void; /** * Passes if spy threw the given exception. * The exception is a String denoting its type. * It’s possible to assert on a dedicated spy call: sinon.assert.threw(spy.thirdCall, exception);. */ - threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: any): void; + threw(spyOrSpyCall: SinonInspectable | SinonSpyCall, exception: any): void; /** * Like threw, only required for all calls to the spy. */ - alwaysThrew(spy: SinonSpy): void; + alwaysThrew(spy: SinonInspectable): void; /** * Like threw, only required for all calls to the spy. */ - alwaysThrew(spy: SinonSpy, exception: string): void; + alwaysThrew(spy: SinonInspectable, exception: string): void; /** * Like threw, only required for all calls to the spy. */ - alwaysThrew(spy: SinonSpy, exception: any): void; + alwaysThrew(spy: SinonInspectable, exception: any): void; /** * Uses sinon.match to test if the arguments can be considered a match. */