strong-mock 8.0.0-beta.2 → 8.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 +18 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -127,7 +127,7 @@ console.log(foo.bar(23)); // even more awesome
|
|
|
127
127
|
|
|
128
128
|
### Setting invocation count expectations
|
|
129
129
|
|
|
130
|
-
By default, each call is expected to be
|
|
130
|
+
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.:
|
|
131
131
|
|
|
132
132
|
```typescript
|
|
133
133
|
const fn = mock<(x: number) => number>();
|
|
@@ -177,7 +177,7 @@ console.log(fn(1)); // 2
|
|
|
177
177
|
|
|
178
178
|
### Mocking promises
|
|
179
179
|
|
|
180
|
-
If you're mocking something that returns a promise then you'll be able to use the promise
|
|
180
|
+
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.
|
|
181
181
|
|
|
182
182
|
```typescript
|
|
183
183
|
type Fn = (x: number) => Promise<number>;
|
|
@@ -191,15 +191,21 @@ console.log(await fn()); // 2
|
|
|
191
191
|
|
|
192
192
|
### Throwing errors
|
|
193
193
|
|
|
194
|
+
Use `thenThrow` or `thenReject` to throw an `Error` instance. You can customize the error message, or even pass a derived class.
|
|
195
|
+
|
|
194
196
|
```typescript
|
|
195
197
|
type Fn = (x: number) => void;
|
|
196
198
|
type FnWithPromise = (x: number) => Promise<void>;
|
|
197
199
|
|
|
200
|
+
class MyError extends Error {}
|
|
201
|
+
|
|
198
202
|
const fn = mock<Fn>();
|
|
199
203
|
const fnWithPromise = mock<FnWithPromise>();
|
|
200
204
|
|
|
205
|
+
// All of these will throw an Error instance.
|
|
201
206
|
when(() => fn(1)).thenThrow();
|
|
202
|
-
when(() =>
|
|
207
|
+
when(() => fn(2)).thenThrow(MyError);
|
|
208
|
+
when(() => fnWithPromise(1)).thenReject('oops');
|
|
203
209
|
```
|
|
204
210
|
|
|
205
211
|
### Verifying expectations
|
|
@@ -423,18 +429,23 @@ console.log(fn(1)); // throws
|
|
|
423
429
|
|
|
424
430
|
### Concrete matcher
|
|
425
431
|
|
|
426
|
-
You can
|
|
432
|
+
You can configure the [matcher](#argument-matchers) that will be used in expectations with concrete values e.g. `42` or `{ foo: "bar" }`. This matcher can always be overwritten inside an expectation with another matcher.
|
|
427
433
|
|
|
428
434
|
```typescript
|
|
429
435
|
import { mock, when, It } from 'strong-mock';
|
|
430
436
|
|
|
431
437
|
// Use strict equality instead of deep equality.
|
|
432
|
-
const fn = mock<(x: number[]) => boolean>({
|
|
438
|
+
const fn = mock<(x: number[], y: string) => boolean>({
|
|
433
439
|
concreteMatcher: It.is
|
|
434
440
|
});
|
|
435
|
-
when(() => fn([1, 2, 3])).thenReturn(true);
|
|
441
|
+
when(() => fn([1, 2, 3], 'foo')).thenReturn(true);
|
|
442
|
+
|
|
443
|
+
fn([1, 2, 3], 'foo'); // throws because different array instances
|
|
436
444
|
|
|
437
|
-
|
|
445
|
+
const arr = [1, 2, 3];
|
|
446
|
+
// The matcher will only apply to non-matcher arguments.
|
|
447
|
+
when(() => fn(arr, It.isString())).thenReturn(true);
|
|
448
|
+
console.log(fn(arr, 'any string')); // true
|
|
438
449
|
```
|
|
439
450
|
|
|
440
451
|
## FAQ
|