strong-mock 9.0.1 → 9.2.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 +41 -25
- package/dist/index.cjs +1191 -0
- package/dist/index.d.cts +584 -0
- package/dist/index.d.ts +584 -14
- package/dist/index.js +658 -842
- package/package.json +35 -21
- package/dist/errors/api.d.ts +0 -13
- package/dist/errors/diff.d.ts +0 -4
- package/dist/errors/unexpected-access.d.ts +0 -5
- package/dist/errors/unexpected-call.d.ts +0 -14
- package/dist/errors/verify.d.ts +0 -8
- package/dist/expectation/expectation.d.ts +0 -27
- package/dist/expectation/repository/expectation-repository.d.ts +0 -90
- package/dist/expectation/repository/flexible-repository.d.ts +0 -38
- package/dist/expectation/repository/return-value.d.ts +0 -13
- package/dist/expectation/strong-expectation.d.ts +0 -30
- package/dist/index.js.map +0 -1
- package/dist/matchers/contains-object.d.ts +0 -28
- package/dist/matchers/deep-equals.d.ts +0 -16
- package/dist/matchers/is-any.d.ts +0 -11
- package/dist/matchers/is-array.d.ts +0 -22
- package/dist/matchers/is-number.d.ts +0 -12
- package/dist/matchers/is-plain-object.d.ts +0 -17
- package/dist/matchers/is-string.d.ts +0 -15
- package/dist/matchers/is.d.ts +0 -9
- package/dist/matchers/it.d.ts +0 -10
- package/dist/matchers/matcher.d.ts +0 -93
- package/dist/matchers/will-capture.d.ts +0 -21
- package/dist/mock/defaults.d.ts +0 -11
- package/dist/mock/map.d.ts +0 -16
- package/dist/mock/mock.d.ts +0 -24
- package/dist/mock/mode.d.ts +0 -6
- package/dist/mock/options.d.ts +0 -99
- package/dist/mock/stub.d.ts +0 -5
- package/dist/print.d.ts +0 -10
- package/dist/proxy.d.ts +0 -48
- package/dist/return/invocation-count.d.ts +0 -44
- package/dist/return/returns.d.ts +0 -61
- package/dist/verify/reset.d.ts +0 -20
- package/dist/verify/verify.d.ts +0 -27
- package/dist/when/expectation-builder.d.ts +0 -26
- 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
|
-
- [
|
|
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)
|
|
@@ -52,6 +51,7 @@ console.log(foo.bar(23)); // 'I am strong!'
|
|
|
52
51
|
- [Reset](#reset)
|
|
53
52
|
- [Resetting expectations](#resetting-expectations)
|
|
54
53
|
- [Mock options](#mock-options)
|
|
54
|
+
- [Name](#name)
|
|
55
55
|
- [Unexpected property return value](#unexpected-property-return-value)
|
|
56
56
|
- [Exact params](#exact-params)
|
|
57
57
|
- [Concrete matcher](#concrete-matcher)
|
|
@@ -127,6 +127,8 @@ try {
|
|
|
127
127
|
|
|
128
128
|
## Installation
|
|
129
129
|
|
|
130
|
+
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.
|
|
131
|
+
|
|
130
132
|
```shell
|
|
131
133
|
npm i -D strong-mock
|
|
132
134
|
```
|
|
@@ -139,10 +141,6 @@ yarn add -D strong-mock
|
|
|
139
141
|
pnpm add -D strong-mock
|
|
140
142
|
```
|
|
141
143
|
|
|
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
144
|
## API
|
|
147
145
|
|
|
148
146
|
### Mock
|
|
@@ -212,7 +210,7 @@ const fn = mock<
|
|
|
212
210
|
>();
|
|
213
211
|
|
|
214
212
|
when(() => fn(
|
|
215
|
-
It.
|
|
213
|
+
It.isNumber(),
|
|
216
214
|
It.containsObject({ values: [1, 2, 3] })
|
|
217
215
|
)).thenReturn('matched!');
|
|
218
216
|
|
|
@@ -229,7 +227,7 @@ when(() => fn(42, It.isPlainObject())).thenReturn('matched');
|
|
|
229
227
|
```
|
|
230
228
|
|
|
231
229
|
Available matchers:
|
|
232
|
-
- `deepEquals` - the default, uses deep equality,
|
|
230
|
+
- `deepEquals` - the default ([can be changed](#concrete-matcher), uses deep equality,
|
|
233
231
|
- `is` - uses `Object.is` for comparison,
|
|
234
232
|
- `isAny` - matches anything,
|
|
235
233
|
- `isNumber` - matches any number,
|
|
@@ -237,8 +235,8 @@ Available matchers:
|
|
|
237
235
|
- `isArray` - matches any array, can search for subsets,
|
|
238
236
|
- `isPlainObject` - matches any plain object,
|
|
239
237
|
- `containsObject` - recursively matches a subset of an object,
|
|
240
|
-
- `willCapture` - matches anything and stores the received value,
|
|
241
|
-
- `matches` - [build your own matcher](#
|
|
238
|
+
- `willCapture` - matches anything and [stores](#custom-matchers) the received value,
|
|
239
|
+
- `matches` - [build your own matcher](#custom-matchers).
|
|
242
240
|
|
|
243
241
|
The following table illustrates the differences between the equality matchers:
|
|
244
242
|
|
|
@@ -249,21 +247,26 @@ The following table illustrates the differences between the equality matchers:
|
|
|
249
247
|
| `{ }` | `{ foo: undefined }` | not equal | not equal | equal |
|
|
250
248
|
| `new (class {})()` | `new (class {})()` | not equal | not equal | equal |
|
|
251
249
|
|
|
252
|
-
|
|
250
|
+
You can nest matchers in `deepEquals`, `containsObject` and `isArray`:
|
|
253
251
|
|
|
254
252
|
```typescript
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
})
|
|
253
|
+
type Point = { label: string; value: number };
|
|
254
|
+
const fn = mock<(data: { points: Point[]; title: string }) => number>();
|
|
258
255
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
256
|
+
// deepEquals is the default matcher so you can omit it.
|
|
257
|
+
when(() => fn({
|
|
258
|
+
points: It.isArray([
|
|
259
|
+
It.containsObject({
|
|
260
|
+
value: It.matches(x => x > 0)
|
|
261
|
+
})
|
|
262
|
+
]),
|
|
263
|
+
title: It.isString(/foo/)
|
|
264
|
+
})).thenReturn(100);
|
|
264
265
|
```
|
|
265
266
|
|
|
266
|
-
|
|
267
|
+
#### Custom matchers
|
|
268
|
+
|
|
269
|
+
`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
270
|
|
|
268
271
|
```ts
|
|
269
272
|
type Cb = (value: number) => number;
|
|
@@ -277,9 +280,7 @@ console.log(fn(23, (x) => x + 1)); // 42
|
|
|
277
280
|
console.log(matcher.value?.(3)); // 4
|
|
278
281
|
```
|
|
279
282
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
You can create arbitrarily complex and type safe matchers with `It.matches()`:
|
|
283
|
+
With `It.matches` you can create arbitrarily complex and type safe matchers:
|
|
283
284
|
|
|
284
285
|
```typescript
|
|
285
286
|
const fn = mock<(x: number, y: string) => string>();
|
|
@@ -465,6 +466,21 @@ const superStrictMock = mock<() => void>();
|
|
|
465
466
|
const strictMock = mock<() => void>({ exactParams: false });
|
|
466
467
|
```
|
|
467
468
|
|
|
469
|
+
### Name
|
|
470
|
+
|
|
471
|
+
The name of a mock appears in error messages e.g., when an unexpected call happens. By default, all mocks are simply named `mock`, but you can set a custom name to make the error messages more descriptive.
|
|
472
|
+
|
|
473
|
+
```typescript
|
|
474
|
+
import { mock, when } from 'strong-mock';
|
|
475
|
+
|
|
476
|
+
interface Service { /* ... */}
|
|
477
|
+
const service = mock<Service>({ name: 'ServiceMock' });
|
|
478
|
+
|
|
479
|
+
when(() => service.foo(1)).thenReturn(2);
|
|
480
|
+
|
|
481
|
+
service.foo(3); // throws "Didn't expect ServiceMock.foo(3) to be called"
|
|
482
|
+
```
|
|
483
|
+
|
|
468
484
|
### Unexpected property return value
|
|
469
485
|
|
|
470
486
|
You can control what happens whenever an unexpected property is accessed, or an unexpected call is made.
|
|
@@ -498,7 +514,7 @@ propertiesThrow.bar(42);
|
|
|
498
514
|
|
|
499
515
|
### Exact params
|
|
500
516
|
|
|
501
|
-
By default, function/method expectations will allow more arguments to be received than expected. Since the expectations are type
|
|
517
|
+
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
518
|
|
|
503
519
|
```typescript
|
|
504
520
|
import { mock } from 'strong-mock';
|
|
@@ -577,7 +593,7 @@ No, passing a concrete implementation to `mock()` will be the same as passing a
|
|
|
577
593
|
|
|
578
594
|
### How do I set expectations on setters?
|
|
579
595
|
|
|
580
|
-
|
|
596
|
+
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
597
|
|
|
582
598
|
### How do I provide a function for the mock to call?
|
|
583
599
|
|