ts-sinon
Sinon extension providing functions to:
- stub all object methods
- stub interface
- stub object constructor
Prerequisites
- You have a version of Node.js >= v8.4.0
- You have installed Typescript
Installation
npm install --save-dev ts-sinon
or
yarn add --dev ts-sinon
Object stubs example
Importing stubObject function:
- import single function:
;
- import as part of sinon singleton:
; ;
Stub all object methods:
;; testStub.method.returns"stubbed"; expecttestStub.method.to.equal"stubbed";
Partial stub:
;// second argument must be existing class method name, in this case only "methodA" or "methodB" are accepted.; expecttestStub.methodA.to.be.undefined;expecttestStub.methodB.to.equal"B: original";
Stub with predefined return values (type-safe):
;; expecttestStub.method.to.equal"stubbed";
Interface stubs example
Importing stubInterface function:
- import single function:
;
- import as part of sinon singleton:
; ;
Interface stub (stub all methods):
; expecttestStub.method.to.be.undefined; testStub.method.returns"stubbed"; expecttestStub.method.to.equal"stubbed";
Interface stub with predefined return values (type-safe):
// method property has to be the same type as method() return type; expecttestStub.method.to.equal"stubbed";
Object constructor stub example
Importing stubConstructor function:
- import single function:
;
- import as part of sinon singleton:
; ;
Object constructor stub (stub all methods):
- without passing predefined args to the constructor:
// type will be guessed automatically; expecttestStub.method.to.be.undefined; testStub.method.returns"stubbed"; expecttestStub.method.to.equal"stubbed"; expecttestStub.someVar.to.equal10; testStub.someVar = 20; expecttestStub.someVar.to.equal20;
- with passing predefined args to the constructor:
// it won't allow to pass incorrect args; expecttestStub.someVar.to.equal"someValue";
Sinon methods
By importing 'ts-sinon' you have access to all sinon methods.
; ;;// ...
or
;;; // ...
Packages
Dependencies:
Dev Dependencies:
Tests
npm test