feat(sinon): Make stub factory method optionally generic (#31606)

This commit is contained in:
Nico Jansen 2018-12-31 17:01:20 +01:00 committed by Wesley Wigham
parent d77cdfe11e
commit b68f2ee2ac
2 changed files with 21 additions and 1 deletions

View File

@ -626,10 +626,13 @@ declare namespace Sinon {
}
interface SinonStubStatic {
// Disable rule so assignment to typed stub works (see examples in tests).
/**
* Creates an anonymous stub function
*/
(): SinonStub;
// tslint:disable-next-line no-unnecessary-generics
<TArgs extends any[]= any[], R = any>(): SinonStub<TArgs, R>;
/**
* Stubs all the objects methods.
* Note that its usually better practice to stub individual methods, particularly on objects that you dont understand or control all the methods for (e.g. library dependencies).

View File

@ -483,6 +483,23 @@ function testStub() {
stub.withArgs('a', 2).returns(true);
}
function testTypedStub() {
class Foo {
bar(baz: number, qux: string): boolean {
return true;
}
}
let stub: sinon.SinonStub<[number, string], boolean> = sinon.stub();
let stub2 = sinon.stub<[number, string], boolean>();
const foo = new Foo();
stub = sinon.stub(foo, 'bar');
stub2 = sinon.stub(foo, 'bar');
const result: boolean = stub(42, 'qux');
const fooStub: sinon.SinonStubbedInstance<Foo> = {
bar: sinon.stub()
};
}
function testMock() {
const obj = {};
const mock = sinon.mock(obj);