From 41e641d8b346f7f4c3a19821d9f388d4195f754a Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Wed, 20 Feb 2019 18:54:28 +0800 Subject: [PATCH 1/3] sinon: add overrides parameter to createStubInstance --- types/sinon/index.d.ts | 7 ++++++- types/sinon/sinon-tests.ts | 3 +++ types/sinon/ts3.1/index.d.ts | 4 +++- types/sinon/ts3.1/sinon-tests.ts | 3 +++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/types/sinon/index.d.ts b/types/sinon/index.d.ts index 7295119c66..321a6174fd 100644 --- a/types/sinon/index.d.ts +++ b/types/sinon/index.d.ts @@ -1618,10 +1618,15 @@ declare namespace Sinon { * * @template TType Type being stubbed. * @param constructor Object or class to stub. + * @param overrides An optional map overriding created stubs * @returns A stubbed version of the constructor. * @remarks The given constructor function is not invoked. See also the stub API. */ - createStubInstance(constructor: StubbableType): SinonStubbedInstance; + createStubInstance( + constructor: StubbableType, + overrides?: { [K in keyof TType]?: any } + ): SinonStubbedInstance; + } interface SinonApi { diff --git a/types/sinon/sinon-tests.ts b/types/sinon/sinon-tests.ts index aebe96ae25..9e76b20bbe 100644 --- a/types/sinon/sinon-tests.ts +++ b/types/sinon/sinon-tests.ts @@ -87,6 +87,9 @@ function testSandbox() { const privateFooFoo: sinon.SinonStub = privateFooStubbedInstance.foo; const clsBar: number = stubInstance.bar; const privateFooBar: number = privateFooStubbedInstance.bar; + sb.createStubInstance(cls, { + bar: 1 + }); } function testFakeServer() { diff --git a/types/sinon/ts3.1/index.d.ts b/types/sinon/ts3.1/index.d.ts index 7123e75d49..0ddf54a3b1 100644 --- a/types/sinon/ts3.1/index.d.ts +++ b/types/sinon/ts3.1/index.d.ts @@ -1707,11 +1707,13 @@ declare namespace Sinon { * * @template TType Type being stubbed. * @param constructor Object or class to stub. + * @param overrides An optional map overriding created stubs * @returns A stubbed version of the constructor. * @remarks The given constructor function is not invoked. See also the stub API. */ createStubInstance( - constructor: StubbableType + constructor: StubbableType, + overrides?: { [K in keyof TType]?: any } ): SinonStubbedInstance; } diff --git a/types/sinon/ts3.1/sinon-tests.ts b/types/sinon/ts3.1/sinon-tests.ts index 2db25348ae..d1f2e4c1bb 100644 --- a/types/sinon/ts3.1/sinon-tests.ts +++ b/types/sinon/ts3.1/sinon-tests.ts @@ -87,6 +87,9 @@ function testSandbox() { const privateFooFoo: sinon.SinonStub = privateFooStubbedInstance.foo; const clsBar: number = stubInstance.bar; const privateFooBar: number = privateFooStubbedInstance.bar; + sb.createStubInstance(cls, { + bar: 1 + }); } function testFakeServer() { From 5a233501941f9a4c9e3fedf5f81b0b05467d6f5e Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Wed, 20 Feb 2019 19:17:57 +0800 Subject: [PATCH 2/3] Fix a lint error --- types/sinon/index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/sinon/index.d.ts b/types/sinon/index.d.ts index 321a6174fd..d05495078c 100644 --- a/types/sinon/index.d.ts +++ b/types/sinon/index.d.ts @@ -1626,7 +1626,6 @@ declare namespace Sinon { constructor: StubbableType, overrides?: { [K in keyof TType]?: any } ): SinonStubbedInstance; - } interface SinonApi { From ce71dddafde2e36cf92f431cba647cafc311f23c Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Thu, 21 Feb 2019 13:35:40 +0800 Subject: [PATCH 3/3] Update according to review comment. --- types/sinon/ts3.1/index.d.ts | 3 ++- types/sinon/ts3.1/sinon-tests.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/types/sinon/ts3.1/index.d.ts b/types/sinon/ts3.1/index.d.ts index 0ddf54a3b1..a27355e835 100644 --- a/types/sinon/ts3.1/index.d.ts +++ b/types/sinon/ts3.1/index.d.ts @@ -1713,7 +1713,8 @@ declare namespace Sinon { */ createStubInstance( constructor: StubbableType, - overrides?: { [K in keyof TType]?: any } + overrides?: { [K in keyof TType]?: + SinonStubbedMember | TType[K] extends (...args: any[]) => infer R ? R : TType[K] } ): SinonStubbedInstance; } diff --git a/types/sinon/ts3.1/sinon-tests.ts b/types/sinon/ts3.1/sinon-tests.ts index d1f2e4c1bb..a123f560fb 100644 --- a/types/sinon/ts3.1/sinon-tests.ts +++ b/types/sinon/ts3.1/sinon-tests.ts @@ -67,7 +67,7 @@ function testSandbox() { sb.replaceSetter(replaceMe, 'setter', (v) => { }); const cls = class { - foo(arg1: string, arg2: number) { return 1; } + foo(arg1: string, arg2: number): number { return 1; } bar: number; }; const PrivateFoo = class { @@ -88,6 +88,7 @@ function testSandbox() { const clsBar: number = stubInstance.bar; const privateFooBar: number = privateFooStubbedInstance.bar; sb.createStubInstance(cls, { + foo: (arg1: string, arg2: number) => 2, bar: 1 }); }