You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When stub is created for a class using stubConstructor, Then Typescript restricts the types of the parameters, that I can pass to sinon.assert.calledWithMatch following the sinon.SinonSpy instance.
But when the method is overloaded by several signatures, it restricts usage to the types defined in one of the signatures (likely it uses the last overloaded signature).
Code snippet showing this behavior:
import sinon from 'sinon';
import { stubConstructor } from 'ts-sinon'
export class Calculator {
sum(arg: string): number;
sum(a: number, b: number): number;
sum(...args: any): number {
return 5 // implementation is irrelevant now
}
}
describe('Calculator stub', () => {
const stub = stubConstructor(Calculator);
beforeEach(() => stub.sum.resetHistory())
it('can call the calculator using numbers', () => {
stub.sum(2, 3)
sinon.assert.calledWithMatch(stub.sum, 2, 3)
})
it('can call the calculator using a string', () => {
stub.sum('2 + 3')
sinon.assert.calledWithMatch(stub.sum, '2 + 3')
})
})
as a workaround could be using something like:
sinon.assert.calledWithMatch(
stub.sum as unknown as sinon.SinonSpy<[a: number, b:number], number>, 2, 3
)
or
sinon.assert.calledWithMatch(
stub.sum as unknown as sinon.SinonSpy<[exp: string], number>, '2 + 3'
)
I would expect it to create a stub with spies for methods which are already are of a of joined type like: sinon.SinonSpy<[exp: string], number> | sinon.SinonSpy<[a: number, b:number], number>
So that we don't have to do manual type suggesting when trying to use the stubs
The text was updated successfully, but these errors were encountered:
When stub is created for a class using
stubConstructor
,Then Typescript restricts the types of the parameters, that I can pass to
sinon.assert.calledWithMatch
following thesinon.SinonSpy
instance.But when the method is overloaded by several signatures, it restricts usage to the types defined in one of the signatures (likely it uses the last overloaded signature).
Code snippet showing this behavior:
as a workaround could be using something like:
or
I would expect it to create a stub with spies for methods which are already are of a of joined type like:
sinon.SinonSpy<[exp: string], number> | sinon.SinonSpy<[a: number, b:number], number>
So that we don't have to do manual type suggesting when trying to use the stubs
The text was updated successfully, but these errors were encountered: