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/dist/index.d.cts
ADDED
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
declare const MATCHER_SYMBOL: unique symbol;
|
|
2
|
+
type MatcherDiffer = (actual: any) => {
|
|
3
|
+
actual: any;
|
|
4
|
+
expected: any;
|
|
5
|
+
};
|
|
6
|
+
type MatcherOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* Will be called when printing the diff between an expectation and the
|
|
9
|
+
* (mismatching) received arguments.
|
|
10
|
+
*
|
|
11
|
+
* With this function you can pretty print the `actual` and `expected` values
|
|
12
|
+
* according to your matcher's logic.
|
|
13
|
+
*
|
|
14
|
+
* @param actual The actual value received by this matcher, same as the one
|
|
15
|
+
* in `matches`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const neverMatcher = It.matches(() => false, {
|
|
19
|
+
* getDiff: (actual) => ({ actual, expected: 'never' })
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* when(() => fn(neverMatcher)).thenReturn(42);
|
|
23
|
+
*
|
|
24
|
+
* fn(42);
|
|
25
|
+
* // - Expected
|
|
26
|
+
* // + Received
|
|
27
|
+
* //
|
|
28
|
+
* // - 'never'
|
|
29
|
+
* // + 42
|
|
30
|
+
*/
|
|
31
|
+
getDiff: MatcherDiffer;
|
|
32
|
+
/**
|
|
33
|
+
* Will be called when printing arguments for an unexpected or unmet expectation.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const neverMatcher = It.matches(() => false, {
|
|
37
|
+
* toString: () => 'never'
|
|
38
|
+
* });
|
|
39
|
+
* when(() => fn(neverMatcher)).thenReturn(42);
|
|
40
|
+
*
|
|
41
|
+
* fn(42);
|
|
42
|
+
* // Unmet expectations:
|
|
43
|
+
* // when(() => fn(never)).thenReturn(42)
|
|
44
|
+
*/
|
|
45
|
+
toString: () => string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* You MUST use {@link It.matches} to create this branded type.
|
|
49
|
+
*/
|
|
50
|
+
interface Matcher extends MatcherOptions {
|
|
51
|
+
[MATCHER_SYMBOL]: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Will be called with the received value and should return whether it matches
|
|
54
|
+
* the expectation.
|
|
55
|
+
*/
|
|
56
|
+
matches: (actual: any) => boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* This takes the shape of T to satisfy call sites, but strong-mock will only
|
|
60
|
+
* care about the matcher type.
|
|
61
|
+
*/
|
|
62
|
+
type TypeMatcher<T> = T & Matcher;
|
|
63
|
+
/**
|
|
64
|
+
* Create a custom matcher.
|
|
65
|
+
*
|
|
66
|
+
* @param predicate Will receive the actual value and return whether it matches the expectation.
|
|
67
|
+
* @param options
|
|
68
|
+
* @param options.toString An optional function that should return a string that will be
|
|
69
|
+
* used when the matcher needs to be printed in an error message. By default,
|
|
70
|
+
* it stringifies `predicate`.
|
|
71
|
+
* @param options.getDiff An optional function that will be called when printing the
|
|
72
|
+
* diff for a failed expectation. It will only be called if there's a mismatch
|
|
73
|
+
* between the expected and received values i.e. `predicate(actual)` fails.
|
|
74
|
+
* By default, the `toString` method will be used to format the expected value,
|
|
75
|
+
* while the received value will be returned as-is.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* // Create a matcher for positive numbers.
|
|
79
|
+
* const fn = mock<(x: number) => number>();
|
|
80
|
+
* when(() => fn(It.matches(x => x >= 0))).thenReturn(42);
|
|
81
|
+
*
|
|
82
|
+
* fn(2) === 42
|
|
83
|
+
* fn(-1) // throws
|
|
84
|
+
*/
|
|
85
|
+
declare const matches: <T>(predicate: (actual: T) => boolean, options?: Partial<MatcherOptions>) => TypeMatcher<T>;
|
|
86
|
+
|
|
87
|
+
type ConcreteMatcher = <T>(expected: T) => Matcher;
|
|
88
|
+
declare enum UnexpectedProperty {
|
|
89
|
+
/**
|
|
90
|
+
* Throw an error immediately.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* // Will throw "Didn't expect foo to be accessed".
|
|
94
|
+
* const { foo } = service;
|
|
95
|
+
*
|
|
96
|
+
* // Will throw "Didn't expect foo to be accessed",
|
|
97
|
+
* // without printing the arguments.
|
|
98
|
+
* foo(42);
|
|
99
|
+
*/
|
|
100
|
+
THROW = 0,
|
|
101
|
+
/**
|
|
102
|
+
* Return a function that will throw if called. This can be useful if your
|
|
103
|
+
* code destructures a function but never calls it.
|
|
104
|
+
*
|
|
105
|
+
* It will also improve error messages for unexpected calls because arguments
|
|
106
|
+
* will be captured instead of throwing immediately on the property access.
|
|
107
|
+
*
|
|
108
|
+
* The function will be returned even if the property is not supposed to be a
|
|
109
|
+
* function. This could cause weird behavior at runtime, when your code expects
|
|
110
|
+
* e.g. a number and gets a function instead.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // This will NOT throw.
|
|
114
|
+
* const { foo } = service;
|
|
115
|
+
*
|
|
116
|
+
* // This will NOT throw, and might produce unexpected results.
|
|
117
|
+
* foo > 0
|
|
118
|
+
*
|
|
119
|
+
* // Will throw "Didn't expect foo(42) to be called".
|
|
120
|
+
* foo(42);
|
|
121
|
+
*/
|
|
122
|
+
CALL_THROW = 1
|
|
123
|
+
}
|
|
124
|
+
interface MockOptions {
|
|
125
|
+
/**
|
|
126
|
+
* The name of the mock that will be used in error messages. Defaults to `mock`.
|
|
127
|
+
*
|
|
128
|
+
* This can be useful when you want to easily identify the mock from the test
|
|
129
|
+
* output, especially if you have multiple mocks in the same test.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const service = mock<Service>({ name: 'Service' });
|
|
133
|
+
* service.foo() // "Didn't expect Service.foo() to be called"
|
|
134
|
+
*/
|
|
135
|
+
name?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Controls what should be returned for a property with no expectations.
|
|
138
|
+
*
|
|
139
|
+
* A property with no expectations is a property that has no `when`
|
|
140
|
+
* expectations set on it. It can also be a property that ran out of `when`
|
|
141
|
+
* expectations.
|
|
142
|
+
*
|
|
143
|
+
* The default is to return a function that will throw when called.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* const foo = mock<{ bar: () => number }>();
|
|
147
|
+
* foo.bar() // unexpected property access
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const foo = mock<{ bar: () => number }>();
|
|
151
|
+
* when(() => foo.bar()).thenReturn(42);
|
|
152
|
+
* foo.bar() === 42
|
|
153
|
+
* foo.bar() // unexpected property access
|
|
154
|
+
*/
|
|
155
|
+
unexpectedProperty?: UnexpectedProperty;
|
|
156
|
+
/**
|
|
157
|
+
* If `true`, the number of received arguments in a function/method call has to
|
|
158
|
+
* match the number of arguments set in the expectation.
|
|
159
|
+
*
|
|
160
|
+
* If `false`, extra parameters are considered optional and checked by the
|
|
161
|
+
* TypeScript compiler instead.
|
|
162
|
+
*
|
|
163
|
+
* You may want to set this to `true` if you're not using TypeScript,
|
|
164
|
+
* or if you want to be extra strict.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* const fn = mock<(value?: number) => number>({ exactParams: true });
|
|
168
|
+
* when(() => fn()).thenReturn(42);
|
|
169
|
+
*
|
|
170
|
+
* fn(100) // throws with exactParams, returns 42 without
|
|
171
|
+
*/
|
|
172
|
+
exactParams?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* The matcher that will be used when one isn't specified explicitly.
|
|
175
|
+
*
|
|
176
|
+
* The most common use case is replacing the default {@link It.deepEquals}
|
|
177
|
+
* matcher with {@link It.is}, but you can also use {@link It.matches} to
|
|
178
|
+
* create a custom matcher.
|
|
179
|
+
*
|
|
180
|
+
* @param expected The concrete expected value received from the
|
|
181
|
+
* {@link when} expectation.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* const fn = mock<(value: number[]) => string>({
|
|
185
|
+
* concreteMatcher: It.is
|
|
186
|
+
* });
|
|
187
|
+
*
|
|
188
|
+
* const expected = [1, 2, 3];
|
|
189
|
+
* when(() => fn(expected).thenReturn('matched');
|
|
190
|
+
*
|
|
191
|
+
* fn([1, 2, 3]); // throws because different array instance
|
|
192
|
+
* fn(expected); // matched
|
|
193
|
+
*/
|
|
194
|
+
concreteMatcher?: ConcreteMatcher;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
type Mock<T> = T;
|
|
198
|
+
/**
|
|
199
|
+
* Create a type safe mock.
|
|
200
|
+
*
|
|
201
|
+
* @see {@link when} Set expectations on the mock using `when`.
|
|
202
|
+
*
|
|
203
|
+
* @param options Configure the options for this specific mock, overriding any
|
|
204
|
+
* defaults that were set with {@link setDefaults}.
|
|
205
|
+
* @param options.name The name of the mock that appears in error messages.
|
|
206
|
+
* @param options.unexpectedProperty Controls what happens when an unexpected
|
|
207
|
+
* property is accessed.
|
|
208
|
+
* @param options.concreteMatcher The matcher that will be used when one isn't
|
|
209
|
+
* specified explicitly.
|
|
210
|
+
* @param options.exactParams Controls whether the number of received arguments
|
|
211
|
+
* has to match the expectation.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* const fn = mock<() => number>();
|
|
215
|
+
*
|
|
216
|
+
* when(() => fn()).thenReturn(23);
|
|
217
|
+
*
|
|
218
|
+
* fn() === 23;
|
|
219
|
+
*/
|
|
220
|
+
declare const mock: <T>({ name, unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => Mock<T>;
|
|
221
|
+
|
|
222
|
+
type Property = string | symbol;
|
|
223
|
+
|
|
224
|
+
interface InvocationCount {
|
|
225
|
+
/**
|
|
226
|
+
* Expect a call to be made at least `min` times and at most `max` times.
|
|
227
|
+
*/
|
|
228
|
+
between: (min: number, max: number) => void;
|
|
229
|
+
/**
|
|
230
|
+
* Expect a call to be made exactly `exact` times.
|
|
231
|
+
*
|
|
232
|
+
* Shortcut for `between(exact, exact)`.
|
|
233
|
+
*/
|
|
234
|
+
times: (exact: number) => void;
|
|
235
|
+
/**
|
|
236
|
+
* Expect a call to be made any number of times, including never.
|
|
237
|
+
*
|
|
238
|
+
* Shortcut for `between(0, Infinity)`.
|
|
239
|
+
*/
|
|
240
|
+
anyTimes: () => void;
|
|
241
|
+
/**
|
|
242
|
+
* Expect a call to be made at least `min` times.
|
|
243
|
+
*
|
|
244
|
+
* Shortcut for `between(min, Infinity)`.
|
|
245
|
+
*/
|
|
246
|
+
atLeast: (min: number) => void;
|
|
247
|
+
/**
|
|
248
|
+
* Expect a call to be made at most `max` times.
|
|
249
|
+
*
|
|
250
|
+
* Shortcut for `between(0, max)`.
|
|
251
|
+
*/
|
|
252
|
+
atMost: (max: number) => void;
|
|
253
|
+
/**
|
|
254
|
+
* Expect a call to be made exactly once.
|
|
255
|
+
*
|
|
256
|
+
* Shortcut for `times(1)`.
|
|
257
|
+
*/
|
|
258
|
+
once: () => void;
|
|
259
|
+
/**
|
|
260
|
+
* Expect a call to be made exactly twice.
|
|
261
|
+
*
|
|
262
|
+
* Shortcut for `times(2)`.
|
|
263
|
+
*/
|
|
264
|
+
twice: () => void;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
type PromiseStub<R, P> = {
|
|
268
|
+
/**
|
|
269
|
+
* Set the return value for the current call.
|
|
270
|
+
*
|
|
271
|
+
* @param value This needs to be of the same type as the value returned
|
|
272
|
+
* by the call inside `when`.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* when(() => fn()).thenReturn(Promise.resolve(23));
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* when(() => fn()).thenReturn(Promise.reject({ foo: 'bar' });
|
|
279
|
+
*/
|
|
280
|
+
thenReturn: (value: P) => InvocationCount;
|
|
281
|
+
/**
|
|
282
|
+
* Set the return value for the current call.
|
|
283
|
+
*
|
|
284
|
+
* @param promiseValue This needs to be of the same type as the value inside
|
|
285
|
+
* the promise returned by the `when` callback.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* when(() => fn()).thenResolve('foo');
|
|
289
|
+
*/
|
|
290
|
+
thenResolve: (promiseValue: R) => InvocationCount;
|
|
291
|
+
/**
|
|
292
|
+
* Make the current call reject with the given error.
|
|
293
|
+
*
|
|
294
|
+
* @param error An `Error` instance. You can pass just a message, and
|
|
295
|
+
* it will be wrapped in an `Error` instance. If you want to reject with
|
|
296
|
+
* a non error then use the {@link thenReturn} method.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* when(() => fn()).thenReject(new Error('oops'));
|
|
300
|
+
*/
|
|
301
|
+
thenReject: ((error: Error) => InvocationCount) & ((message: string) => InvocationCount) & (() => InvocationCount);
|
|
302
|
+
};
|
|
303
|
+
type NonPromiseStub<R> = {
|
|
304
|
+
/**
|
|
305
|
+
* Set the return value for the current call.
|
|
306
|
+
*
|
|
307
|
+
* @param returnValue This needs to be of the same type as the value returned
|
|
308
|
+
* by the `when` callback.
|
|
309
|
+
*/
|
|
310
|
+
thenReturn: (returnValue: R) => InvocationCount;
|
|
311
|
+
/**
|
|
312
|
+
* Make the current call throw the given error.
|
|
313
|
+
*
|
|
314
|
+
* @param error The error instance. If you want to throw a simple `Error`
|
|
315
|
+
* you can pass just the message.
|
|
316
|
+
*/
|
|
317
|
+
thenThrow: ((error: Error) => InvocationCount) & ((message: string) => InvocationCount) & (() => InvocationCount);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
interface When {
|
|
321
|
+
<R>(expectation: () => Promise<R>): PromiseStub<R, Promise<R>>;
|
|
322
|
+
<R>(expectation: () => R): NonPromiseStub<R>;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Set an expectation on a mock.
|
|
326
|
+
*
|
|
327
|
+
* The expectation must be finished by setting a return value, even if the value
|
|
328
|
+
* is `undefined`.
|
|
329
|
+
*
|
|
330
|
+
* If a call happens that was not expected, then the mock will throw an error.
|
|
331
|
+
* By default, the call is expected only once. Use the invocation count helpers
|
|
332
|
+
* to expect a call multiple times.
|
|
333
|
+
*
|
|
334
|
+
* @param expectation A callback to set the expectation on your mock. The
|
|
335
|
+
* callback must return the value from the mock to properly infer types.
|
|
336
|
+
*
|
|
337
|
+
* @see {@link It.deepEquals} All values are wrapped in the default matcher.
|
|
338
|
+
* @see {@link It} for more matchers.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* const fn = mock<() => void>();
|
|
342
|
+
* when(() => fn()).thenReturn(undefined);
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* const fn = mock<() => number>();
|
|
346
|
+
* when(() => fn()).thenReturn(42).atMost(3);
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* const fn = mock<(x: number) => Promise<number>();
|
|
350
|
+
* when(() => fn(23)).thenResolve(42);
|
|
351
|
+
*/
|
|
352
|
+
declare const when: When;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Remove any remaining expectations on the given mock.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* const fn = mock<() => number>();
|
|
359
|
+
*
|
|
360
|
+
* when(() => fn()).thenReturn(23);
|
|
361
|
+
*
|
|
362
|
+
* reset(fn);
|
|
363
|
+
*
|
|
364
|
+
* fn(); // throws
|
|
365
|
+
*/
|
|
366
|
+
declare const reset: (mock: Mock<any>) => void;
|
|
367
|
+
/**
|
|
368
|
+
* Reset all existing mocks.
|
|
369
|
+
*
|
|
370
|
+
* @see reset
|
|
371
|
+
*/
|
|
372
|
+
declare const resetAll: () => void;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Verify that all expectations on the given mock have been met.
|
|
376
|
+
*
|
|
377
|
+
* @throws Will throw if there are remaining expectations that were set
|
|
378
|
+
* using `when` and that weren't met.
|
|
379
|
+
*
|
|
380
|
+
* @throws Will throw if any unexpected calls happened. Normally those
|
|
381
|
+
* calls throw on their own, but the error might be caught by the code
|
|
382
|
+
* being tested.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* const fn = mock<() => number>();
|
|
386
|
+
*
|
|
387
|
+
* when(() => fn()).thenReturn(23);
|
|
388
|
+
*
|
|
389
|
+
* verify(fn); // throws
|
|
390
|
+
*/
|
|
391
|
+
declare const verify: <T>(mock: Mock<T>) => void;
|
|
392
|
+
/**
|
|
393
|
+
* Verify all existing mocks.
|
|
394
|
+
*
|
|
395
|
+
* @see verify
|
|
396
|
+
*/
|
|
397
|
+
declare const verifyAll: () => void;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Override strong-mock's defaults.
|
|
401
|
+
*
|
|
402
|
+
* @param newDefaults These will be applied to the library defaults. Multiple
|
|
403
|
+
* calls don't stack e.g. calling this with `{}` will clear any previously
|
|
404
|
+
* applied defaults.
|
|
405
|
+
*/
|
|
406
|
+
declare const setDefaults: (newDefaults: MockOptions) => void;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Compare values using deep equality.
|
|
410
|
+
*
|
|
411
|
+
* This is the default matcher that's automatically used when no other matcher
|
|
412
|
+
* is specified. You can change it with the `concreteMatcher` option when creating
|
|
413
|
+
* a {@link mock}, or set a new default with {@link setDefaults}.
|
|
414
|
+
*
|
|
415
|
+
* @param expected Supports nested matchers for objects, arrays, and Maps.
|
|
416
|
+
* @param strict By default, this matcher will treat a missing key in an object
|
|
417
|
+
* and a key with the value `undefined` as not equal. It will also consider
|
|
418
|
+
* non `Object` instances with different constructors as not equal. Setting
|
|
419
|
+
* this to `false` will consider the objects in both cases as equal.
|
|
420
|
+
*
|
|
421
|
+
* @see {@link It.containsObject} or {@link It.isArray} for partially matching
|
|
422
|
+
* objects or arrays respectively.
|
|
423
|
+
* @see {@link It.is} for strict equality.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* const fn = mock<(x: { foo: { bar: number } }) => number>();
|
|
427
|
+
*
|
|
428
|
+
* // deepEquals is the default matcher.
|
|
429
|
+
* when(() => fn({ foo: { bar: 42 } })).thenReturn(1);
|
|
430
|
+
*
|
|
431
|
+
* // With nested matchers:
|
|
432
|
+
* when(() => fn({ foo: { bar: It.isNumber() } })).thenReturn(2);
|
|
433
|
+
*/
|
|
434
|
+
declare const deepEquals: <T>(expected: T, { strict, }?: {
|
|
435
|
+
strict?: boolean;
|
|
436
|
+
}) => TypeMatcher<T>;
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Compare values using `Object.is`.
|
|
440
|
+
*
|
|
441
|
+
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
442
|
+
*
|
|
443
|
+
* @see It.deepEquals A matcher that uses deep equality.
|
|
444
|
+
*/
|
|
445
|
+
declare const is: <T = unknown>(expected: T) => TypeMatcher<T>;
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Match any value, including `undefined` and `null`.
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* const fn = mock<(x: number, y: string) => number>();
|
|
452
|
+
* when(() => fn(It.isAny(), It.isAny())).thenReturn(1);
|
|
453
|
+
*
|
|
454
|
+
* fn(23, 'foobar') === 1
|
|
455
|
+
*/
|
|
456
|
+
declare const isAny: () => TypeMatcher<any>;
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Match an array.
|
|
460
|
+
*
|
|
461
|
+
* Supports nested matchers.
|
|
462
|
+
*
|
|
463
|
+
* @param containing If given, the matched array has to contain ALL of these
|
|
464
|
+
* elements in ANY order. Use {@link It.deepEquals} or {@link It.matches} if
|
|
465
|
+
* you want more control.
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* const fn = mock<(arr: number[]) => number>();
|
|
469
|
+
* when(() => fn(It.isArray())).thenReturn(1);
|
|
470
|
+
* when(() => fn(It.isArray([2, 3]))).thenReturn(2);
|
|
471
|
+
*
|
|
472
|
+
* fn({ length: 1, 0: 42 }) // throws
|
|
473
|
+
* fn([]) === 1
|
|
474
|
+
* fn([3, 2, 1]) === 2
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* It.isArray([It.isString({ containing: 'foobar' })])
|
|
478
|
+
*/
|
|
479
|
+
declare const isArray: <T extends unknown[]>(containing?: T) => TypeMatcher<T>;
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Match any number.
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* const fn = mock<(x: number) => number>();
|
|
486
|
+
* when(() => fn(It.isNumber())).returns(42);
|
|
487
|
+
*
|
|
488
|
+
* fn(20.5) === 42
|
|
489
|
+
* fn(NaN) // throws
|
|
490
|
+
*/
|
|
491
|
+
declare const isNumber: () => TypeMatcher<number>;
|
|
492
|
+
|
|
493
|
+
type ObjectType$1 = Record<Property, unknown>;
|
|
494
|
+
/**
|
|
495
|
+
* Matches any plain object e.g. object literals or objects created with `Object.create()`.
|
|
496
|
+
*
|
|
497
|
+
* Classes, arrays, maps, sets etc. are not considered plain objects.
|
|
498
|
+
* You can use {@link containsObject} or {@link matches} to match those.
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
* const fn = mock<({ foo: string }) => number>();
|
|
502
|
+
* when(() => fn(It.isPlainObject())).thenReturn(42);
|
|
503
|
+
*
|
|
504
|
+
* fn({ foo: 'bar' }) // returns 42
|
|
505
|
+
*/
|
|
506
|
+
declare const isPlainObject: <T extends ObjectType$1>() => TypeMatcher<T>;
|
|
507
|
+
|
|
508
|
+
type ObjectType = Record<Property, unknown>;
|
|
509
|
+
type NonEmptyObject<T extends ObjectType> = keyof T extends never ? never : T;
|
|
510
|
+
type DeepPartial<T> = T extends ObjectType ? {
|
|
511
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
512
|
+
} : T;
|
|
513
|
+
/**
|
|
514
|
+
* Check if an object recursively contains the expected properties,
|
|
515
|
+
* i.e. the expected object is a subset of the received object.
|
|
516
|
+
*
|
|
517
|
+
* @param partial A subset of the expected object that will be recursively matched.
|
|
518
|
+
* Supports nested matchers.
|
|
519
|
+
* Concrete values will be compared with {@link deepEquals}.
|
|
520
|
+
*
|
|
521
|
+
* @see {@link isPlainObject} if you want to match any plain object.
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* const fn = mock<(pos: { x: number, y: number }) => number>();
|
|
525
|
+
* when(() => fn(It.containsObject({ x: 23 }))).returns(42);
|
|
526
|
+
*
|
|
527
|
+
* fn({ x: 23, y: 200 }) // returns 42
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* It.containsObject({ foo: It.isString() })
|
|
531
|
+
*/
|
|
532
|
+
declare const containsObject: <T, K extends DeepPartial<T>>(partial: K extends ObjectType ? NonEmptyObject<K> : never) => TypeMatcher<T>;
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Match any string.
|
|
536
|
+
*
|
|
537
|
+
* @param matching An optional string or RegExp to match the string against.
|
|
538
|
+
* If it's a string, a case-sensitive search will be performed.
|
|
539
|
+
*
|
|
540
|
+
* @example
|
|
541
|
+
* const fn = mock<(x: string, y: string) => number>();
|
|
542
|
+
* when(() => fn(It.isString(), It.isString('bar'))).returns(42);
|
|
543
|
+
*
|
|
544
|
+
* fn('foo', 'baz') // throws
|
|
545
|
+
* fn('foo', 'bar') === 42
|
|
546
|
+
*/
|
|
547
|
+
declare const isString: (matching?: string | RegExp) => TypeMatcher<string>;
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Matches anything and stores the received value.
|
|
551
|
+
*
|
|
552
|
+
* This should not be needed for most cases, but can be useful if you need
|
|
553
|
+
* access to a complex argument outside the expectation e.g. to test a
|
|
554
|
+
* callback.
|
|
555
|
+
*
|
|
556
|
+
* @param name If given, this name will be printed in error messages.
|
|
557
|
+
*
|
|
558
|
+
* @example
|
|
559
|
+
* const fn = mock<(cb: (value: number) => number) => void>();
|
|
560
|
+
* const matcher = It.willCapture();
|
|
561
|
+
* when(() => fn(matcher)).thenReturn();
|
|
562
|
+
*
|
|
563
|
+
* fn(x => x + 1);
|
|
564
|
+
* matcher.value?.(3) === 4
|
|
565
|
+
*/
|
|
566
|
+
declare const willCapture: <T = unknown>(name?: string) => TypeMatcher<T> & {
|
|
567
|
+
value: T | undefined;
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
declare const it_containsObject: typeof containsObject;
|
|
571
|
+
declare const it_deepEquals: typeof deepEquals;
|
|
572
|
+
declare const it_is: typeof is;
|
|
573
|
+
declare const it_isAny: typeof isAny;
|
|
574
|
+
declare const it_isArray: typeof isArray;
|
|
575
|
+
declare const it_isNumber: typeof isNumber;
|
|
576
|
+
declare const it_isPlainObject: typeof isPlainObject;
|
|
577
|
+
declare const it_isString: typeof isString;
|
|
578
|
+
declare const it_matches: typeof matches;
|
|
579
|
+
declare const it_willCapture: typeof willCapture;
|
|
580
|
+
declare namespace it {
|
|
581
|
+
export { it_containsObject as containsObject, it_deepEquals as deepEquals, it_is as is, it_isAny as isAny, it_isArray as isArray, it_isNumber as isNumber, it_isPlainObject as isPlainObject, it_isString as isString, it_matches as matches, it_willCapture as willCapture };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export { it as It, type Matcher, type MatcherOptions, type MockOptions, UnexpectedProperty, mock, reset, resetAll, setDefaults, verify, verifyAll, when };
|