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