Sinon Stub with TypeScript


This post goes over how to use Sinon stubs with TypeScript.

Stub

Given @types/sinon is installed, stubs are automatically typed:

const object = {
  method: (name: string) => name,
};
const methodStub = sinon.stub(object, 'method');

SinonStubbedMember

Use SinonStubbedMember to type a stubbed function:

let methodStub: sinon.SinonStubbedMember<typeof object.method>;
methodStub = sinon.stub(object, 'method');

SinonStub

Use type assertion to specify a SinonStub:

object.method as sinon.SinonStub;


Please support this site and join our Discord!