diff --git a/types/sinon/ts3.1/index.d.ts b/types/sinon/ts3.1/index.d.ts index cab3c80b674777..6641d3510fc1ac 100644 --- a/types/sinon/ts3.1/index.d.ts +++ b/types/sinon/ts3.1/index.d.ts @@ -6,7 +6,9 @@ interface Document {} // tslint:disable-line no-empty-interface declare namespace Sinon { type MatchArguments = { - [K in keyof T]: SinonMatcher | MatchArguments | T[K]; + [K in keyof T]: SinonMatcher + | (T[K] extends object ? MatchArguments : never) + | T[K]; }; interface SinonSpyCallApi { @@ -29,7 +31,7 @@ declare namespace Sinon { * so a call that received the provided arguments (in the same spots) and possibly others as well will return true. * @param args */ - calledWith(...args: MatchArguments): boolean; + calledWith(...args: Partial>): boolean; /** * Returns true if spy was called at least once with the provided arguments and no others. */ @@ -136,41 +138,17 @@ declare namespace Sinon { /** * Returns true if the spy call occurred before another spy call. * @param call + * */ - calledBefore(call: SinonSpyCall): boolean; + calledBefore(call: SinonSpyCall): boolean; /** * Returns true if the spy call occurred after another spy call. * @param call */ - calledAfter(call: SinonSpyCall): boolean; - } - - /** - * 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; + calledAfter(call: SinonSpyCall): boolean; } - /** - * The part of the spy API that allows inspecting the calls made on a spy. - */ - interface SinonInspectable + interface SinonSpy extends Pick< SinonSpyCallApi, Exclude, 'args'> @@ -234,26 +212,37 @@ 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 */ - calledBefore(anotherSpy: SinonInspectable): boolean; + calledBefore(anotherSpy: SinonSpy): boolean; /** * Returns true if the spy was called after @param anotherSpy * @param anotherSpy */ - calledAfter(anotherSpy: SinonInspectable): boolean; + calledAfter(anotherSpy: SinonSpy): boolean; /** * Returns true if spy was called before @param anotherSpy, and no spy calls occurred between spy and @param anotherSpy. * @param anotherSpy */ - calledImmediatelyBefore(anotherSpy: SinonInspectable): boolean; + calledImmediatelyBefore(anotherSpy: SinonSpy): boolean; /** * Returns true if spy was called after @param anotherSpy, and no spy calls occurred between @param anotherSpy and spy. * @param anotherSpy */ - calledImmediatelyAfter(anotherSpy: SinonInspectable): boolean; + 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 @@ -308,6 +297,11 @@ 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. @@ -637,12 +631,14 @@ declare namespace Sinon { } interface SinonStubStatic { - // Disable rule so assignment to typed stub works (see examples in tests). + /* tslint:disable:no-unnecessary-generics */ + /** * Creates an anonymous stub function */ - // tslint:disable-next-line no-unnecessary-generics - (): SinonStub; + (): SinonStub; + + /* tslint:enable:no-unnecessary-generics */ /** * Stubs all the object’s methods. @@ -1162,64 +1158,66 @@ declare namespace Sinon { pass(assertion: any): void; // Overridable // Methods + /** * Passes if spy was never called * @param spy */ - notCalled(spy: SinonInspectable): void; + notCalled(spy: SinonSpy): void; /** * Passes if spy was called at least once. */ - called(spy: SinonInspectable): void; + called(spy: SinonSpy): void; /** * Passes if spy was called once and only once. */ - calledOnce(spy: SinonInspectable): void; + calledOnce(spy: SinonSpy): void; /** * Passes if spy was called exactly twice. */ - calledTwice(spy: SinonInspectable): void; + calledTwice(spy: SinonSpy): void; /** * Passes if spy was called exactly three times. */ - calledThrice(spy: SinonInspectable): void; + calledThrice(spy: SinonSpy): void; /** * Passes if spy was called exactly num times. */ - callCount(spy: SinonInspectable, count: number): void; + callCount(spy: SinonSpy, count: number): void; /** * Passes if provided spies were called in the specified order. * @param spies */ - callOrder(...spies: SinonInspectable[]): 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: SinonInspectable | SinonSpyCall, obj: any): void; + calledOn(spyOrSpyCall: SinonSpy | SinonSpyCall, obj: any): void; /** * Passes if spy was always called with obj as its this value. */ - alwaysCalledOn(spy: SinonInspectable, obj: any): void; + alwaysCalledOn(spy: SinonSpy, 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: SinonInspectable | SinonSpyCall, ...args: MatchArguments): void; + calledWith(spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: MatchArguments): void; /** * Passes if spy was always called with the provided arguments. * @param spy * @param args */ - alwaysCalledWith(spy: SinonInspectable, ...args: MatchArguments): void; + alwaysCalledWith(spy: SinonSpy, ...args: MatchArguments): void; /** * Passes if spy was never called with the provided arguments. * @param spy * @param args */ - neverCalledWith(spy: SinonInspectable, ...args: MatchArguments): void; + neverCalledWith(spy: SinonSpy, ...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, ...);. @@ -1227,68 +1225,70 @@ declare namespace Sinon { * @param args */ calledWithExactly( - spyOrSpyCall: SinonInspectable | SinonSpyCall, + spyOrSpyCall: SinonSpy | SinonSpyCall, ...args: MatchArguments ): void; /** * Passes if spy was always called with the provided arguments and no others. */ - alwaysCalledWithExactly(spy: SinonInspectable, ...args: MatchArguments): void; + alwaysCalledWithExactly(spy: SinonSpy, ...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: SinonInspectable | SinonSpyCall, + spyOrSpyCall: SinonSpy | 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: SinonInspectable, ...args: TArgs): void; + alwaysCalledWithMatch(spy: SinonSpy, ...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: SinonInspectable, ...args: TArgs): void; + neverCalledWithMatch(spy: SinonSpy, ...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: SinonInspectable | SinonSpyCall): void; + calledWithNew(spyOrSpyCall: SinonSpy | SinonSpyCall): void; /** * Passes if spy threw any exception. */ - threw(spyOrSpyCall: SinonInspectable | SinonSpyCall): void; + threw(spyOrSpyCall: SinonSpy | 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: SinonInspectable | SinonSpyCall, exception: string): void; + threw(spyOrSpyCall: SinonSpy | 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: SinonInspectable | SinonSpyCall, exception: any): void; + threw(spyOrSpyCall: SinonSpy | SinonSpyCall, exception: any): void; + /** * Like threw, only required for all calls to the spy. */ - alwaysThrew(spy: SinonInspectable): void; + alwaysThrew(spy: SinonSpy): void; /** * Like threw, only required for all calls to the spy. */ - alwaysThrew(spy: SinonInspectable, exception: string): void; + alwaysThrew(spy: SinonSpy, exception: string): void; /** * Like threw, only required for all calls to the spy. */ - alwaysThrew(spy: SinonInspectable, exception: any): void; + alwaysThrew(spy: SinonSpy, exception: any): void; + /** * Uses sinon.match to test if the arguments can be considered a match. */ diff --git a/types/sinon/ts3.1/sinon-tests.ts b/types/sinon/ts3.1/sinon-tests.ts index 05b582b9db0e6a..c7bd7a6c9c4dae 100644 --- a/types/sinon/ts3.1/sinon-tests.ts +++ b/types/sinon/ts3.1/sinon-tests.ts @@ -82,6 +82,7 @@ function testSandbox() { const stubInstance = sb.createStubInstance(cls); const privateFooStubbedInstance = sb.createStubInstance(PrivateFoo); stubInstance.foo.calledWith('foo', 1); + stubInstance.foo.calledWith('foo'); privateFooStubbedInstance.foo.calledWith(); const clsFoo: sinon.SinonStub<[string, number], number> = stubInstance.foo; const privateFooFoo: sinon.SinonStub<[], void> = privateFooStubbedInstance.foo;