sinon-ts
Version:
sinon library extension to stub whole object and interfaces
145 lines • 3.81 kB
TypeScript
/**
* @packageDocumentation
*
* A fork of [ts-sinon](https://www.npmjs.com/package/ts-sinon) that lets you BYO sinon. Can probably be retired if [ttarnowski/ts-sinon#255](https://github.com/ttarnowski/ts-sinon/pull/255) is ever merged.
*
* - stub all object methods
* - stub interface
* - stub object constructor
*
* ## Prerequisites
*
* 1. You have a version of Node.js >= [v8.4.0](https://nodejs.org/en/download/)
* 2. You have installed [Typescript](https://www.typescriptlang.org/index.html#download-links)
*
* @example Stub all object methods
*
* ```javascript
* 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')
* ```
*
* @example Partial stub
*
* ```typescript
* 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')
* ```
*
* @example Interface stub (stub all methods)
*
* ```typescript
* import Sinon from 'sinon'
* import { stubInterface } from 'ts-sinon'
*
* interface Test {
* method(): string
* }
*
* const testStub = stubInterface<Test>()
*
* expect(testStub.method()).to.be.undefined
*
* testStub.method.returns('stubbed')
*
* expect(testStub.method()).to.equal('stubbed')
* ```
*
* @example Interface stub with predefined return values (type-safe)
*
* ```typescript
* import Sinon from 'sinon'
* import { stubInterface } from 'ts-sinon'
*
* interface Test {
* method(): string
* }
*
* // method property has to be the same type as method() return type
* const testStub = stubInterface<Test>({
* method: Sinon.stub().returns('stubbed')
* })
*
* expect(testStub.method()).to.equal('stubbed')
* ```
*
* @example Object constructor stub (stub all methods)
*
* - without passing predefined args to the constructor:
*
* ```typescript
* import Sinon from 'sinon'
* import { stubConstructor } from 'ts-sinon'
*
* class Test {
* public someVar: number = 10
*
* method(): string {
* return 'value'
* }
* }
*
* // type will be guessed automatically
* const testStub = stubConstructor(Test)
*
* expect(testStub.method()).to.be.undefined
*
* testStub.method.returns('stubbed')
*
* expect(testStub.method()).to.equal('stubbed')
*
* expect(testStub.someVar).to.equal(10)
*
* testStub.someVar = 20
*
* expect(testStub.someVar).to.equal(20)
* ```
*
* @example Passing predefined args to the constructor
*
* ```typescript
* import Sinon from 'sinon'
* import { stubConstructor } from 'ts-sinon'
*
* class Test {
* constructor(public someVar: string, y: boolean) {}
* // ...
* }
*
* // it won't allow to pass incorrect args
* const testStub = stubConstructor(Test, 'someValue', true)
*
* expect(testStub.someVar).to.equal('someValue')
* ```
*/
/// <reference types="sinon" />
export type StubbedInstance<T> = sinon.SinonStubbedInstance<T> & T;
export type AllowedKeys<T, Condition> = {
[Key in keyof T]: T[Key] extends Condition ? Key : never;
}[keyof T];
export declare function stubObject<T extends object>(object: T, partial?: Partial<T>): StubbedInstance<T>;
export declare function stubConstructor<T extends new (...args: any[]) => any>(constructor: T, ...constructorArgs: ConstructorParameters<T> | undefined[]): StubbedInstance<InstanceType<T>>;
export declare function stubInterface<T extends object>(methods?: Partial<T>): StubbedInstance<T>;
//# sourceMappingURL=index.d.ts.map