Skip to content

Stubbed method only has one of the overloaded signatures #198

@luke10x

Description

@luke10x

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions