A fork of ts-sinon that lets you BYO sinon. Can probably be retired if ttarnowski/ts-sinon#255 is ever merged.
Stub all object methods
import Sinon from 'sinon'import { stubObject } from 'ts-sinon'class Test { method() { return 'original' }}const test = new Test()const testStub = stubObject<Test>(test)testStub.method.returns('stubbed')expect(testStub.method()).to.equal('stubbed') Copy
import Sinon from 'sinon'import { stubObject } from 'ts-sinon'class Test { method() { return 'original' }}const test = new Test()const testStub = stubObject<Test>(test)testStub.method.returns('stubbed')expect(testStub.method()).to.equal('stubbed')
Partial stub
import Sinon from 'sinon'import { stubObject } from 'ts-sinon'class Test { method() { return 'original' }}const test = new Test()const testStub = stubObject<Test>(test, { method: Sinon.stub().returns('stubbed')})expect(testStub.method()).to.equal('stubbed') Copy
import Sinon from 'sinon'import { stubObject } from 'ts-sinon'class Test { method() { return 'original' }}const test = new Test()const testStub = stubObject<Test>(test, { method: Sinon.stub().returns('stubbed')})expect(testStub.method()).to.equal('stubbed')
Interface stub (stub all methods)
import Sinon from 'sinon'import { stubInterface } from 'ts-sinon'interface Test { method(): string}const testStub = stubInterface<Test>()expect(testStub.method()).to.be.undefinedtestStub.method.returns('stubbed')expect(testStub.method()).to.equal('stubbed') Copy
import Sinon from 'sinon'import { stubInterface } from 'ts-sinon'interface Test { method(): string}const testStub = stubInterface<Test>()expect(testStub.method()).to.be.undefinedtestStub.method.returns('stubbed')expect(testStub.method()).to.equal('stubbed')
Interface stub with predefined return values (type-safe)
import Sinon from 'sinon'import { stubInterface } from 'ts-sinon'interface Test { method(): string}// method property has to be the same type as method() return typeconst testStub = stubInterface<Test>({ method: Sinon.stub().returns('stubbed')})expect(testStub.method()).to.equal('stubbed') Copy
import Sinon from 'sinon'import { stubInterface } from 'ts-sinon'interface Test { method(): string}// method property has to be the same type as method() return typeconst testStub = stubInterface<Test>({ method: Sinon.stub().returns('stubbed')})expect(testStub.method()).to.equal('stubbed')
Object constructor stub (stub all methods)
import Sinon from 'sinon'import { stubConstructor } from 'ts-sinon'class Test { public someVar: number = 10 method(): string { return 'value' }}// type will be guessed automaticallyconst testStub = stubConstructor(Test)expect(testStub.method()).to.be.undefinedtestStub.method.returns('stubbed')expect(testStub.method()).to.equal('stubbed')expect(testStub.someVar).to.equal(10)testStub.someVar = 20expect(testStub.someVar).to.equal(20) Copy
import Sinon from 'sinon'import { stubConstructor } from 'ts-sinon'class Test { public someVar: number = 10 method(): string { return 'value' }}// type will be guessed automaticallyconst testStub = stubConstructor(Test)expect(testStub.method()).to.be.undefinedtestStub.method.returns('stubbed')expect(testStub.method()).to.equal('stubbed')expect(testStub.someVar).to.equal(10)testStub.someVar = 20expect(testStub.someVar).to.equal(20)
Passing predefined args to the constructor
import Sinon from 'sinon'import { stubConstructor } from 'ts-sinon'class Test { constructor(public someVar: string, y: boolean) {} // ...}// it won't allow to pass incorrect argsconst testStub = stubConstructor(Test, 'someValue', true)expect(testStub.someVar).to.equal('someValue') Copy
import Sinon from 'sinon'import { stubConstructor } from 'ts-sinon'class Test { constructor(public someVar: string, y: boolean) {} // ...}// it won't allow to pass incorrect argsconst testStub = stubConstructor(Test, 'someValue', true)expect(testStub.someVar).to.equal('someValue')
Generated using TypeDoc
A fork of ts-sinon that lets you BYO sinon. Can probably be retired if ttarnowski/ts-sinon#255 is ever merged.
Prerequisites
Example
Stub all object methods
Example
Partial stub
Example
Interface stub (stub all methods)
Example
Interface stub with predefined return values (type-safe)
Example
Object constructor stub (stub all methods)
Example
Passing predefined args to the constructor