strong-mock 9.0.0-beta.2 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,17 +35,22 @@ console.log(foo.bar(23)); // 'I am strong!'
35
35
  - [Installation](#installation)
36
36
  - [Requirements](#requirements)
37
37
  - [API](#api)
38
- - [Setting expectations](#setting-expectations)
39
- - [Setting multiple expectations](#setting-multiple-expectations)
40
- - [Setting invocation count expectations](#setting-invocation-count-expectations)
41
- - [Mocking interfaces](#mocking-interfaces)
42
- - [Mocking functions](#mocking-functions)
43
- - [Mocking promises](#mocking-promises)
44
- - [Throwing errors](#throwing-errors)
45
- - [Verifying expectations](#verifying-expectations)
46
- - [Resetting expectations](#resetting-expectations)
47
- - [Matchers](#matchers-1)
38
+ - [Mock](#mock)
39
+ - [Mocking types and interfaces](#mocking-types-and-interfaces)
40
+ - [Mocking functions](#mocking-functions)
41
+ - [When](#when)
42
+ - [Setting expectations](#setting-expectations)
43
+ - [Setting multiple expectations](#setting-multiple-expectations)
44
+ - [Matchers](#matchers-1)
48
45
  - [Creating your own matcher](#creating-your-own-matcher)
46
+ - [Then](#then)
47
+ - [Setting invocation count expectations](#setting-invocation-count-expectations)
48
+ - [Returning promises](#returning-promises)
49
+ - [Throwing errors](#throwing-errors)
50
+ - [Verify](#verify)
51
+ - [Verifying expectations](#verifying-expectations)
52
+ - [Reset](#reset)
53
+ - [Resetting expectations](#resetting-expectations)
49
54
  - [Mock options](#mock-options)
50
55
  - [Unexpected property return value](#unexpected-property-return-value)
51
56
  - [Exact params](#exact-params)
@@ -89,7 +94,7 @@ const fn = mock<(pos: { x: number; y: number }) => boolean>();
89
94
 
90
95
  when(() =>
91
96
  fn(
92
- It.isPartial({
97
+ It.containsObject({
93
98
  x: It.isNumber(),
94
99
  y: It.matches<number>((y) => y > 0)
95
100
  })
@@ -140,46 +145,11 @@ strong-mock requires an environment that supports the [ES6 Proxy object](https:/
140
145
 
141
146
  ## API
142
147
 
143
- ### Setting expectations
148
+ ### Mock
144
149
 
145
- Expectations are set by calling the mock inside a `when` callback and setting a return value.
146
-
147
- ```typescript
148
- when(() => foo.bar(23)).thenReturn('awesome');
149
- ```
150
-
151
- ### Setting multiple expectations
152
-
153
- You can set as many expectations as you want by calling `when` multiple times. If you have multiple expectations with the same arguments they will be consumed in the order they were created.
154
-
155
- ```typescript
156
- when(() => foo.bar(23)).thenReturn('awesome');
157
- when(() => foo.bar(23)).thenReturn('even more awesome');
158
-
159
- console.log(foo.bar(23)); // awesome
160
- console.log(foo.bar(23)); // even more awesome
161
- ```
162
-
163
- ### Setting invocation count expectations
164
-
165
- By default, each call is expected to be made only once. You can expect a call to be made multiple times by using the invocation count helpers `between`, `atLeast`, `times`, `anyTimes` etc.:
166
-
167
- ```typescript
168
- const fn = mock<(x: number) => number>();
169
-
170
- when(() => fn(1)).thenReturn(1).between(2, 3);
171
-
172
- console.log(fn(1)); // 1
173
- console.log(fn(1)); // 1
174
- console.log(fn(1)); // 1
175
- console.log(fn(1)); // throws because the expectation is finished
176
- ```
177
-
178
- You'll notice there is no `never()` helper - if you expect a call to not be made simply don't set an expectation on it and the mock will throw if the call happens.
150
+ #### Mocking types and interfaces
179
151
 
180
- ### Mocking interfaces
181
-
182
- Pass in the interface to the generic argument of `mock`:
152
+ Pass in the type or interface to the generic argument of `mock`:
183
153
 
184
154
  ```typescript
185
155
  interface Foo {
@@ -196,9 +166,9 @@ console.log(foo.bar(23)); // 'awesome'
196
166
  console.log(foo.baz); // 100
197
167
  ```
198
168
 
199
- ### Mocking functions
169
+ #### Mocking functions
200
170
 
201
- You can also mock functions similarly to interfaces:
171
+ You can also mock function types:
202
172
 
203
173
  ```typescript
204
174
  type Fn = (x: number) => number;
@@ -210,98 +180,29 @@ when(() => fn(1)).thenReturn(2);
210
180
  console.log(fn(1)); // 2
211
181
  ```
212
182
 
213
- ### Mocking promises
214
-
215
- If you're mocking something that returns a promise then you'll be able to use the `thenResolve` promise helper to set the return value.
216
-
217
- ```typescript
218
- type Fn = (x: number) => Promise<number>;
219
-
220
- const fn = mock<Fn>();
221
-
222
- when(() => fn(1)).thenResolve(2);
223
-
224
- console.log(await fn(1)); // 2
225
- ```
226
-
227
- ### Throwing errors
228
-
229
- Use `thenThrow` or `thenReject` to throw an `Error` instance. You can customize the error message, or even pass a derived class.
230
-
231
- ```typescript
232
- type Fn = (x: number) => void;
233
- type FnWithPromise = (x: number) => Promise<void>;
234
-
235
- class MyError extends Error {}
183
+ ### When
236
184
 
237
- const fn = mock<Fn>();
238
- const fnWithPromise = mock<FnWithPromise>();
185
+ #### Setting expectations
239
186
 
240
- // All of these will throw an Error instance.
241
- when(() => fn(1)).thenThrow();
242
- when(() => fn(2)).thenThrow(MyError);
243
- when(() => fnWithPromise(1)).thenReject('oops');
244
- ```
245
-
246
- ### Verifying expectations
247
-
248
- Calling `verify(myMock)` will make sure that all expectations set on the mock have been met, and that no additional calls have been made.
249
-
250
- ```typescript
251
- const fn = mock<(x: number) => number>();
252
-
253
- when(() => fn(1)).thenReturn(1).between(2, 10);
254
-
255
- verify(fn); // throws UnmetExpectations
256
- ```
257
-
258
- It will also throw if any unexpected calls happened that were maybe caught in the code under test.
259
-
260
- ```typescript
261
- const fn = mock<() => void>();
262
-
263
- try {
264
- fn(); // throws because the call is unexpected
265
- } catch(e) {
266
- // your code might transition to an error state here
267
- }
268
-
269
- verify(fn); // throws UnexpectedCalls
270
- ```
271
-
272
- It is recommended that you call `verify()` on your mocks at the end of every test. This will make sure you don't have any unused expectations in your tests and that your code did not silently catch any of the errors that are thrown when an unexpected call happens. You can use `verifyAll()` to check all existing mocks.
187
+ Expectations are set by calling the mock inside a `when` callback and setting a return value.
273
188
 
274
189
  ```typescript
275
- afterEach(() => {
276
- verifyAll();
277
- });
190
+ when(() => foo.bar(23)).thenReturn('awesome');
278
191
  ```
279
192
 
280
- ![verify error](media/verify.png)
193
+ #### Setting multiple expectations
281
194
 
282
- ### Resetting expectations
195
+ You can set as many expectations as you want by calling `when` multiple times. If you have multiple expectations with the same arguments they will be consumed in the order they were created.
283
196
 
284
- You can remove all expectations from a mock by using the `reset()` method:
285
-
286
197
  ```typescript
287
- const fn = mock<(x: number) => number>();
288
-
289
- when(() => fn(1)).thenReturn(1);
290
-
291
- reset(fn);
292
-
293
- fn(1); // throws
294
- ```
295
-
296
- If you create common mocks that are shared by multiple tests you should reset them before each test. You can use `resetAll()` to reset all existing mocks.
198
+ when(() => foo.bar(23)).thenReturn('awesome');
199
+ when(() => foo.bar(23)).thenReturn('even more awesome');
297
200
 
298
- ```typescript
299
- beforeEach(() => {
300
- resetAll();
301
- });
201
+ console.log(foo.bar(23)); // awesome
202
+ console.log(foo.bar(23)); // even more awesome
302
203
  ```
303
204
 
304
- ### Matchers
205
+ #### Matchers
305
206
 
306
207
  Sometimes you're not interested in specifying all the arguments in an expectation. Maybe they've been covered in another test, maybe they're hard to specify e.g. callbacks, or maybe you want to match just a property from an argument.
307
208
 
@@ -312,7 +213,7 @@ const fn = mock<
312
213
 
313
214
  when(() => fn(
314
215
  It.isAny(),
315
- It.isPartial({ values: [1, 2, 3] })
216
+ It.containsObject({ values: [1, 2, 3] })
316
217
  )).thenReturn('matched!');
317
218
 
318
219
  console.log(fn(
@@ -335,7 +236,7 @@ Available matchers:
335
236
  - `isString` - matches any string, can search for substrings and patterns,
336
237
  - `isArray` - matches any array, can search for subsets,
337
238
  - `isPlainObject` - matches any plain object,
338
- - `isPartial` - recursively matches a subset of an object,
239
+ - `containsObject` - recursively matches a subset of an object,
339
240
  - `willCapture` - matches anything and stores the received value,
340
241
  - `matches` - [build your own matcher](#creating-your-own-matcher).
341
242
 
@@ -348,14 +249,18 @@ The following table illustrates the differences between the equality matchers:
348
249
  | `{ }` | `{ foo: undefined }` | not equal | not equal | equal |
349
250
  | `new (class {})()` | `new (class {})()` | not equal | not equal | equal |
350
251
 
351
- Some matchers, like `isPartial` and `isArray` support nesting matchers:
252
+ Some matchers, like `containsObject` and `isArray` support nesting matchers:
352
253
 
353
254
  ```typescript
354
- It.isPartial({ foo: It.isString() })
255
+ It.containsObject({
256
+ foo: It.isString()
257
+ })
355
258
 
356
- It.isArray([ It.isPartial({
357
- foo: It.isString({ matching: /foo/ })
358
- })])
259
+ It.isArray([
260
+ It.containsObject({
261
+ foo: It.isString(/foo/)
262
+ })
263
+ ])
359
264
  ```
360
265
 
361
266
  `It.willCapture` is a special matcher that will match any value and store it, so you can access it outside an expectation. This could be useful to capture a callback and then test it separately.
@@ -421,7 +326,127 @@ when(() => fn(closeTo(1), 'foo')).thenReturn('matched');
421
326
  fn(2, 'foo');
422
327
  ```
423
328
 
424
- ![Error message for custom matcher](media/custom-matcher.png)
329
+ ![Error message for custom matcher](media/custom-matcher-error.png)
330
+
331
+ ### Then
332
+
333
+ #### Setting invocation count expectations
334
+
335
+ By default, each call is expected to be made only once. You can expect a call to be made multiple times by using the invocation count helpers `between`, `atLeast`, `times`, `anyTimes` etc.:
336
+
337
+ ```typescript
338
+ const fn = mock<(x: number) => number>();
339
+
340
+ when(() => fn(1)).thenReturn(1).between(2, 3);
341
+
342
+ console.log(fn(1)); // 1
343
+ console.log(fn(1)); // 1
344
+ console.log(fn(1)); // 1
345
+ console.log(fn(1)); // throws because the expectation is finished
346
+ ```
347
+
348
+ You'll notice there is no `never()` helper - if you expect a call to not be made simply don't set an expectation on it and the mock will throw if the call happens.
349
+
350
+ #### Returning promises
351
+
352
+ If you're mocking something that returns a promise then you'll be able to use the `thenResolve` promise helper to set the return value.
353
+
354
+ ```typescript
355
+ type Fn = (x: number) => Promise<number>;
356
+
357
+ const fn = mock<Fn>();
358
+
359
+ when(() => fn(1)).thenResolve(42);
360
+
361
+ console.log(await fn(1)); // 42
362
+ ```
363
+
364
+ You can also use `thenReturn` with a Promise value:
365
+
366
+ ```typescript
367
+ when(() => fn(1)).thenReturn(Promise.resolve(42));
368
+ ```
369
+
370
+ #### Throwing errors
371
+
372
+ Use `thenThrow` or `thenReject` to throw an `Error` instance. You can customize the error message, or even pass a derived class.
373
+
374
+ ```typescript
375
+ type Fn = (x: number) => void;
376
+ type FnWithPromise = (x: number) => Promise<void>;
377
+
378
+ class MyError extends Error {}
379
+
380
+ const fn = mock<Fn>();
381
+ const fnWithPromise = mock<FnWithPromise>();
382
+
383
+ // All of these will throw an Error instance.
384
+ when(() => fn(1)).thenThrow();
385
+ when(() => fn(2)).thenThrow(MyError);
386
+ when(() => fnWithPromise(1)).thenReject('oops');
387
+ ```
388
+
389
+ ### Verify
390
+
391
+ #### Verifying expectations
392
+
393
+ Calling `verify(myMock)` will make sure that all expectations set on the mock have been met, and that no additional calls have been made.
394
+
395
+ ```typescript
396
+ const fn = mock<(x: number) => number>();
397
+
398
+ when(() => fn(1)).thenReturn(1).between(2, 10);
399
+
400
+ verify(fn); // throws UnmetExpectations
401
+ ```
402
+
403
+ It will also throw if any unexpected calls happened that were maybe caught in the code under test.
404
+
405
+ ```typescript
406
+ const fn = mock<() => void>();
407
+
408
+ try {
409
+ fn(); // throws because the call is unexpected
410
+ } catch(e) {
411
+ // your code might transition to an error state here
412
+ }
413
+
414
+ verify(fn); // throws UnexpectedCalls
415
+ ```
416
+
417
+ It is recommended that you call `verify()` on your mocks at the end of every test. This will make sure you don't have any unused expectations in your tests and that your code did not silently catch any of the errors that are thrown when an unexpected call happens. You can use `verifyAll()` to check all existing mocks.
418
+
419
+ ```typescript
420
+ afterEach(() => {
421
+ verifyAll();
422
+ });
423
+ ```
424
+
425
+ ![verify error](media/verify.png)
426
+
427
+ ### Reset
428
+
429
+ #### Resetting expectations
430
+
431
+ You can remove all expectations from a mock by using the `reset()` method:
432
+
433
+ ```typescript
434
+ const fn = mock<(x: number) => number>();
435
+
436
+ when(() => fn(1)).thenReturn(1);
437
+
438
+ reset(fn);
439
+
440
+ fn(1); // throws
441
+ ```
442
+
443
+ If you create common mocks that are shared by multiple tests you should reset them before each test. You can use `resetAll()` to reset all existing mocks.
444
+
445
+ ```typescript
446
+ beforeEach(() => {
447
+ resetAll();
448
+ });
449
+ ```
425
450
 
426
451
  ## Mock options
427
452