UNPKG

4.92 kBMarkdownView Raw
1# ts-sinon
2
3Sinon extension providing functions to:
4- stub all object methods
5- stub interface
6- stub object constructor
7
8## Prerequisites
9
101. You have a version of Node.js >= [v8.4.0](https://nodejs.org/en/download/)
112. You have installed [Typescript](https://www.typescriptlang.org/index.html#download-links)
12
13## Installation
14
15`npm install --save-dev ts-sinon`
16or
17`yarn add --dev ts-sinon`
18
19## Object stubs example
20
21Importing stubObject function:
22
23- import single function:
24```javascript
25import { stubObject } from "ts-sinon";
26```
27
28- import as part of sinon singleton:
29```javascript
30import * as sinon from "ts-sinon";
31
32const stubObject = sinon.stubObject;
33```
34
35Stub all object methods:
36
37```javascript
38class Test {
39 method() { return 'original' }
40}
41
42const test = new Test();
43const testStub = stubObject<Test>(test);
44
45testStub.method.returns('stubbed');
46
47expect(testStub.method()).to.equal('stubbed');
48```
49
50Partial stub:
51
52```javascript
53class Test {
54 methodA() { return 'A: original' }
55 methodB() { return 'B: original' }
56}
57
58const test = new Test();
59const testStub = stubObject<Test>(test, ['methodA']);
60
61expect(testStub.methodA()).to.be.undefined;
62expect(testStub.methodB()).to.equal('B: original');
63```
64
65Stub with predefined return values (deprecated - see the deprecation note):
66
67```javascript
68class Test {
69 method() { return 'original' }
70}
71
72const test = new Test();
73const testStub = stubObject<Test>(test, { method: 'stubbed' });
74
75expect(testStub.method()).to.equal('stubbed');
76```
77## Interface stubs example
78
79Importing stubInterface function:
80
81- import single function:
82```javascript
83import { stubInterface } from "ts-sinon";
84```
85
86- import as part of sinon singleton:
87```javascript
88import * as sinon from "ts-sinon";
89
90const stubInterface = sinon.stubInterface;
91```
92
93Interface stub (stub all methods):
94
95```javascript
96interface Test {
97 method(): string;
98}
99
100const testStub = stubInterface<Test>();
101
102expect(testStub.method()).to.be.undefined;
103
104testStub.method.returns('stubbed');
105
106expect(testStub.method()).to.equal('stubbed');
107```
108
109Interface stub with predefined return values (deprecated - see the deprecation note):
110
111```javascript
112interface Test {
113 method(): string;
114}
115
116const testStub = stubInterface<Test>({ method: 'stubbed' });
117
118expect(testStub.method()).to.equal('stubbed');
119```
120
121## Object constructor stub example
122
123Importing stubConstructor function:
124
125- import single function:
126```javascript
127import { stubConstructor } from "ts-sinon";
128```
129
130- import as part of sinon singleton:
131```javascript
132import * as sinon from "ts-sinon";
133
134const stubConstructor = sinon.stubConstructor;
135```
136
137Object constructor stub (stub all methods):
138
139- without passing predefined args to the constructor:
140```javascript
141class Test {
142 public someVar: number = 10;
143
144 method(): string {
145 return 'value';
146 }
147}
148
149// type will be guessed automatically
150const testStub = stubConstructor(Test);
151
152expect(testStub.method()).to.be.undefined;
153
154testStub.method.returns('stubbed');
155
156expect(testStub.method()).to.equal('stubbed');
157
158expect(testStub.someVar).to.equal(10);
159
160testStub.someVar = 20;
161
162expect(testStub.someVar).to.equal(20);
163```
164
165- with passing predefined args to the constructor:
166```javascript
167class Test {
168 constructor(public someVar: string, y: boolean) {}
169
170 // ...
171}
172
173// it won't allow to pass incorrect args
174const testStub = stubConstructor(Test, "someVar value", true);
175
176expect(testStub.someVar).to.equal("someValue");
177```
178
179## Method map argument deprecation note
180
181Due to a potential risk of overwriting return value type of stubbed method (that won't be caught by TypeScript compiler) I have decided to mark it as deprecated.
182Please look at the following example of type overwriting using method map:
183
184```javascript
185interface ITest {
186 method1(): void;
187 method2(num: number): string;
188}
189
190const interfaceStub: ITest = stubInterface<ITest>({
191 method2: 12345
192});
193
194// following expression will assign 12345 value of type number to v variable which is incorrect (it will compile without an error)
195const v: string = interfaceStub.method2(1);
196
197```
198
199## Sinon methods
200
201By importing 'ts-sinon' you have access to all sinon methods.
202
203```javascript
204import sinon, { stubInterface } from "ts-sinon";
205
206const functionStub = sinon.stub();
207const spy = sinon.spy();
208// ...
209```
210
211or
212
213```javascript
214import * as tsSinon from "ts-sinon"
215
216const functionStub = tsSinon.default.stub();
217const spy = tsSinon.default.spy();
218const tsStubInterface = tsSinon.stubInterface<T>();
219
220// ...
221```
222
223## Packages
224
225##### Dependencies:
2261. [Microsoft/TypeScript](https://github.com/Microsoft/TypeScript)
2272. [TypeStrong/ts-node](https://github.com/TypeStrong/ts-node)
2283. [sinonjs/sinon](https://github.com/sinonjs/sinon)
229
230##### Dev Dependencies:
2314. [mochajs/mocha](https://github.com/mochajs/mocha)
2325. [chaijs/chai](https://github.com/chaijs/chai)
2336. [domenic/sinon-chai](https://github.com/domenic/sinon-chai)
234
235## Tests
236
237`npm test`
\No newline at end of file