strong-mock 9.0.1 → 9.1.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.
Files changed (42) hide show
  1. package/README.md +25 -25
  2. package/dist/index.cjs +1175 -0
  3. package/dist/index.d.cts +572 -0
  4. package/dist/index.d.ts +572 -14
  5. package/dist/index.js +628 -828
  6. package/package.json +35 -21
  7. package/dist/errors/api.d.ts +0 -13
  8. package/dist/errors/diff.d.ts +0 -4
  9. package/dist/errors/unexpected-access.d.ts +0 -5
  10. package/dist/errors/unexpected-call.d.ts +0 -14
  11. package/dist/errors/verify.d.ts +0 -8
  12. package/dist/expectation/expectation.d.ts +0 -27
  13. package/dist/expectation/repository/expectation-repository.d.ts +0 -90
  14. package/dist/expectation/repository/flexible-repository.d.ts +0 -38
  15. package/dist/expectation/repository/return-value.d.ts +0 -13
  16. package/dist/expectation/strong-expectation.d.ts +0 -30
  17. package/dist/index.js.map +0 -1
  18. package/dist/matchers/contains-object.d.ts +0 -28
  19. package/dist/matchers/deep-equals.d.ts +0 -16
  20. package/dist/matchers/is-any.d.ts +0 -11
  21. package/dist/matchers/is-array.d.ts +0 -22
  22. package/dist/matchers/is-number.d.ts +0 -12
  23. package/dist/matchers/is-plain-object.d.ts +0 -17
  24. package/dist/matchers/is-string.d.ts +0 -15
  25. package/dist/matchers/is.d.ts +0 -9
  26. package/dist/matchers/it.d.ts +0 -10
  27. package/dist/matchers/matcher.d.ts +0 -93
  28. package/dist/matchers/will-capture.d.ts +0 -21
  29. package/dist/mock/defaults.d.ts +0 -11
  30. package/dist/mock/map.d.ts +0 -16
  31. package/dist/mock/mock.d.ts +0 -24
  32. package/dist/mock/mode.d.ts +0 -6
  33. package/dist/mock/options.d.ts +0 -99
  34. package/dist/mock/stub.d.ts +0 -5
  35. package/dist/print.d.ts +0 -10
  36. package/dist/proxy.d.ts +0 -48
  37. package/dist/return/invocation-count.d.ts +0 -44
  38. package/dist/return/returns.d.ts +0 -61
  39. package/dist/verify/reset.d.ts +0 -20
  40. package/dist/verify/verify.d.ts +0 -27
  41. package/dist/when/expectation-builder.d.ts +0 -26
  42. package/dist/when/when.d.ts +0 -32
package/README.md CHANGED
@@ -33,7 +33,6 @@ console.log(foo.bar(23)); // 'I am strong!'
33
33
  - [Awesome error messages](#awesome-error-messages)
34
34
  - [Works with Promises and Errors](#works-with-promises-and-errors)
35
35
  - [Installation](#installation)
36
- - [Requirements](#requirements)
37
36
  - [API](#api)
38
37
  - [Mock](#mock)
39
38
  - [Mocking types and interfaces](#mocking-types-and-interfaces)
@@ -42,7 +41,7 @@ console.log(foo.bar(23)); // 'I am strong!'
42
41
  - [Setting expectations](#setting-expectations)
43
42
  - [Setting multiple expectations](#setting-multiple-expectations)
44
43
  - [Matchers](#matchers-1)
45
- - [Creating your own matcher](#creating-your-own-matcher)
44
+ - [Custom matchers](#custom-matchers)
46
45
  - [Then](#then)
47
46
  - [Setting invocation count expectations](#setting-invocation-count-expectations)
48
47
  - [Returning promises](#returning-promises)
@@ -127,6 +126,8 @@ try {
127
126
 
128
127
  ## Installation
129
128
 
129
+ strong-mock supports the latest LTS and Maintenance [Node.js versions](https://nodejs.org/en/about/previous-releases). It may work on older versions, but it's not guaranteed.
130
+
130
131
  ```shell
131
132
  npm i -D strong-mock
132
133
  ```
@@ -139,10 +140,6 @@ yarn add -D strong-mock
139
140
  pnpm add -D strong-mock
140
141
  ```
141
142
 
142
- ## Requirements
143
-
144
- strong-mock requires an environment that supports the [ES6 Proxy object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This is necessary to create dynamic mocks from types because TypeScript does not support reflection i.e. exposing the type info at runtime.
145
-
146
143
  ## API
147
144
 
148
145
  ### Mock
@@ -212,7 +209,7 @@ const fn = mock<
212
209
  >();
213
210
 
214
211
  when(() => fn(
215
- It.isAny(),
212
+ It.isNumber(),
216
213
  It.containsObject({ values: [1, 2, 3] })
217
214
  )).thenReturn('matched!');
218
215
 
@@ -229,7 +226,7 @@ when(() => fn(42, It.isPlainObject())).thenReturn('matched');
229
226
  ```
230
227
 
231
228
  Available matchers:
232
- - `deepEquals` - the default, uses deep equality,
229
+ - `deepEquals` - the default ([can be changed](#concrete-matcher), uses deep equality,
233
230
  - `is` - uses `Object.is` for comparison,
234
231
  - `isAny` - matches anything,
235
232
  - `isNumber` - matches any number,
@@ -237,8 +234,8 @@ Available matchers:
237
234
  - `isArray` - matches any array, can search for subsets,
238
235
  - `isPlainObject` - matches any plain object,
239
236
  - `containsObject` - recursively matches a subset of an object,
240
- - `willCapture` - matches anything and stores the received value,
241
- - `matches` - [build your own matcher](#creating-your-own-matcher).
237
+ - `willCapture` - matches anything and [stores](#custom-matchers) the received value,
238
+ - `matches` - [build your own matcher](#custom-matchers).
242
239
 
243
240
  The following table illustrates the differences between the equality matchers:
244
241
 
@@ -249,21 +246,26 @@ The following table illustrates the differences between the equality matchers:
249
246
  | `{ }` | `{ foo: undefined }` | not equal | not equal | equal |
250
247
  | `new (class {})()` | `new (class {})()` | not equal | not equal | equal |
251
248
 
252
- Some matchers, like `containsObject` and `isArray` support nesting matchers:
249
+ You can nest matchers in `deepEquals`, `containsObject` and `isArray`:
253
250
 
254
251
  ```typescript
255
- It.containsObject({
256
- foo: It.isString()
257
- })
252
+ type Point = { label: string; value: number };
253
+ const fn = mock<(data: { points: Point[]; title: string }) => number>();
258
254
 
259
- It.isArray([
260
- It.containsObject({
261
- foo: It.isString(/foo/)
262
- })
263
- ])
255
+ // deepEquals is the default matcher so you can omit it.
256
+ when(() => fn({
257
+ points: It.isArray([
258
+ It.containsObject({
259
+ value: It.matches(x => x > 0)
260
+ })
261
+ ]),
262
+ title: It.isString(/foo/)
263
+ })).thenReturn(100);
264
264
  ```
265
265
 
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.
266
+ #### Custom matchers
267
+
268
+ `It.willCapture` 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.
267
269
 
268
270
  ```ts
269
271
  type Cb = (value: number) => number;
@@ -277,9 +279,7 @@ console.log(fn(23, (x) => x + 1)); // 42
277
279
  console.log(matcher.value?.(3)); // 4
278
280
  ```
279
281
 
280
- #### Creating your own matcher
281
-
282
- You can create arbitrarily complex and type safe matchers with `It.matches()`:
282
+ With `It.matches` you can create arbitrarily complex and type safe matchers:
283
283
 
284
284
  ```typescript
285
285
  const fn = mock<(x: number, y: string) => string>();
@@ -498,7 +498,7 @@ propertiesThrow.bar(42);
498
498
 
499
499
  ### Exact params
500
500
 
501
- By default, function/method expectations will allow more arguments to be received than expected. Since the expectations are type safe, the TypeScript compiler will never allow expecting less arguments than required. Unspecified optional arguments will be considered ignored, as if they've been replaced with [matchers](#matchers-1).
501
+ By default, function/method expectations will allow more arguments to be received than expected. Since the expectations are type-safe, the TypeScript compiler will never allow expecting fewer arguments than required. Unspecified optional arguments will be considered ignored, as if they've been replaced with [matchers](#matchers-1).
502
502
 
503
503
  ```typescript
504
504
  import { mock } from 'strong-mock';
@@ -577,7 +577,7 @@ No, passing a concrete implementation to `mock()` will be the same as passing a
577
577
 
578
578
  ### How do I set expectations on setters?
579
579
 
580
- You currently can't do that. Please use a normal method instead e.g. `setFoo()` vs `set foo()`.
580
+ Calling setters inside `when` is not currently supported. If you can redesign the code under test, you should consider using a setter method e.g. `setFoo(value)` instead of `set foo(value)`.
581
581
 
582
582
  ### How do I provide a function for the mock to call?
583
583