Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stubbed method only has one of the overloaded signatures #198

Open
luke10x opened this issue Apr 19, 2021 · 0 comments
Open

Stubbed method only has one of the overloaded signatures #198

luke10x opened this issue Apr 19, 2021 · 0 comments

Comments

@luke10x
Copy link

luke10x commented Apr 19, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant