strong-mock 8.0.1 → 9.0.0-beta.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 +52 -30
- package/dist/errors/api.d.ts +13 -0
- package/dist/errors/unexpected-access.d.ts +5 -0
- package/dist/errors/unexpected-call.d.ts +17 -0
- package/dist/errors/verify.d.ts +8 -0
- package/dist/expectation/expectation.d.ts +27 -30
- package/dist/expectation/repository/expectation-repository.d.ts +90 -90
- package/dist/expectation/repository/flexible-repository.d.ts +38 -38
- package/dist/expectation/repository/return-value.d.ts +13 -13
- package/dist/expectation/strong-expectation.d.ts +30 -30
- package/dist/index.d.ts +14 -9
- package/dist/index.js +810 -512
- package/dist/index.js.map +1 -1
- package/dist/matchers/deep-equals.d.ts +16 -0
- package/dist/matchers/is-any.d.ts +11 -0
- package/dist/matchers/is-array.d.ts +22 -0
- package/dist/matchers/is-number.d.ts +12 -0
- package/dist/matchers/is-object.d.ts +27 -0
- package/dist/matchers/is-string.d.ts +15 -0
- package/dist/matchers/is.d.ts +9 -0
- package/dist/matchers/it.d.ts +9 -0
- package/dist/matchers/matcher.d.ts +92 -0
- package/dist/matchers/will-capture.d.ts +21 -0
- package/dist/mock/defaults.d.ts +11 -11
- package/dist/mock/map.d.ts +16 -16
- package/dist/mock/mock.d.ts +29 -29
- package/dist/mock/options.d.ts +99 -99
- package/dist/mock/stub.d.ts +5 -5
- package/dist/print.d.ts +10 -10
- package/dist/proxy.d.ts +48 -48
- package/dist/return/invocation-count.d.ts +44 -44
- package/dist/return/returns.d.ts +61 -87
- package/dist/verify/reset.d.ts +20 -20
- package/dist/verify/verify.d.ts +27 -27
- package/dist/when/{pending-expectation.d.ts → expectation-builder.d.ts} +26 -31
- package/dist/when/when.d.ts +32 -32
- package/package.json +20 -16
- package/dist/errors.d.ts +0 -28
- package/dist/expectation/it.d.ts +0 -29
- package/dist/expectation/matcher.d.ts +0 -21
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export declare const MATCHER_SYMBOL: unique symbol;
|
|
2
|
+
export type MatcherOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Will be called when printing the diff between an expectation and the
|
|
5
|
+
* (mismatching) received arguments.
|
|
6
|
+
*
|
|
7
|
+
* With this function you can pretty print the `actual` and `expected` values
|
|
8
|
+
* according to your matcher's logic.
|
|
9
|
+
*
|
|
10
|
+
* @param actual The actual value received by this matcher, same as the one
|
|
11
|
+
* in `matches`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const neverMatcher = It.matches(() => false, {
|
|
15
|
+
* getDiff: (actual) => ({ actual, expected: 'never' })
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* when(() => fn(neverMatcher)).thenReturn(42);
|
|
19
|
+
*
|
|
20
|
+
* fn(42);
|
|
21
|
+
* // - Expected
|
|
22
|
+
* // + Received
|
|
23
|
+
* //
|
|
24
|
+
* // - 'never'
|
|
25
|
+
* // + 42
|
|
26
|
+
*/
|
|
27
|
+
getDiff: (actual: any) => {
|
|
28
|
+
actual: any;
|
|
29
|
+
expected: any;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Will be called when printing arguments for an unexpected or unmet expectation.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const neverMatcher = It.matches(() => false, {
|
|
36
|
+
* toString: () => 'never'
|
|
37
|
+
* });
|
|
38
|
+
* when(() => fn(neverMatcher)).thenReturn(42);
|
|
39
|
+
*
|
|
40
|
+
* fn(42);
|
|
41
|
+
* // Unmet expectations:
|
|
42
|
+
* // when(() => fn(never)).thenReturn(42)
|
|
43
|
+
*/
|
|
44
|
+
toString: () => string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* You MUST use {@link It.matches} to create this branded type.
|
|
48
|
+
*/
|
|
49
|
+
export interface Matcher extends MatcherOptions {
|
|
50
|
+
[MATCHER_SYMBOL]: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Will be called with the received value and should return whether it matches
|
|
53
|
+
* the expectation.
|
|
54
|
+
*/
|
|
55
|
+
matches: (actual: any) => boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* This takes the shape of T to satisfy call sites, but strong-mock will only
|
|
59
|
+
* care about the matcher type.
|
|
60
|
+
*/
|
|
61
|
+
export type TypeMatcher<T> = T & Matcher;
|
|
62
|
+
/**
|
|
63
|
+
* Used to test if an expectation is an argument is a custom matcher.
|
|
64
|
+
*/
|
|
65
|
+
export declare function isMatcher(f: unknown): f is Matcher;
|
|
66
|
+
export declare const getMatcherDiffs: (matchers: Matcher[], args: unknown[]) => {
|
|
67
|
+
actual: unknown[];
|
|
68
|
+
expected: unknown[];
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Create a custom matcher.
|
|
72
|
+
*
|
|
73
|
+
* @param predicate Will receive the actual value and return whether it matches the expectation.
|
|
74
|
+
* @param options
|
|
75
|
+
* @param options.toString An optional function that should return a string that will be
|
|
76
|
+
* used when the matcher needs to be printed in an error message. By default,
|
|
77
|
+
* it stringifies `predicate`.
|
|
78
|
+
* @param options.getDiff An optional function that will be called when printing the
|
|
79
|
+
* diff for a failed expectation. It will only be called if there's a mismatch
|
|
80
|
+
* between the expected and received values i.e. `predicate(actual)` fails.
|
|
81
|
+
* By default, the `toString` method will be used to format the expected value,
|
|
82
|
+
* while the received value will be returned as-is.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* // Create a matcher for positive numbers.
|
|
86
|
+
* const fn = mock<(x: number) => number>();
|
|
87
|
+
* when(() => fn(It.matches(x => x >= 0))).thenReturn(42);
|
|
88
|
+
*
|
|
89
|
+
* fn(2) === 42
|
|
90
|
+
* fn(-1) // throws
|
|
91
|
+
*/
|
|
92
|
+
export declare const matches: <T>(predicate: (actual: T) => boolean, options?: Partial<MatcherOptions>) => TypeMatcher<T>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Matcher, TypeMatcher } from './matcher';
|
|
2
|
+
/**
|
|
3
|
+
* Matches anything and stores the received value.
|
|
4
|
+
*
|
|
5
|
+
* This should not be needed for most cases, but can be useful if you need
|
|
6
|
+
* access to a complex argument outside the expectation e.g. to test a
|
|
7
|
+
* callback.
|
|
8
|
+
*
|
|
9
|
+
* @param name If given, this name will be printed in error messages.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const fn = mock<(cb: (value: number) => number) => void>();
|
|
13
|
+
* const matcher = It.willCapture();
|
|
14
|
+
* when(() => fn(matcher)).thenReturn();
|
|
15
|
+
*
|
|
16
|
+
* fn(x => x + 1);
|
|
17
|
+
* matcher.value?.(3) === 4
|
|
18
|
+
*/
|
|
19
|
+
export declare const willCapture: <T = unknown>(name?: string) => T & Matcher & {
|
|
20
|
+
value: T | undefined;
|
|
21
|
+
};
|
package/dist/mock/defaults.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { MockOptions } from './options';
|
|
2
|
-
export type StrongMockDefaults = Required<MockOptions>;
|
|
3
|
-
export declare let currentDefaults: StrongMockDefaults;
|
|
4
|
-
/**
|
|
5
|
-
* Override strong-mock's defaults.
|
|
6
|
-
*
|
|
7
|
-
* @param newDefaults These will be applied to the library defaults. Multiple
|
|
8
|
-
* calls don't stack e.g. calling this with `{}` will clear any previously
|
|
9
|
-
* applied defaults.
|
|
10
|
-
*/
|
|
11
|
-
export declare const setDefaults: (newDefaults: MockOptions) => void;
|
|
1
|
+
import type { MockOptions } from './options';
|
|
2
|
+
export type StrongMockDefaults = Required<MockOptions>;
|
|
3
|
+
export declare let currentDefaults: StrongMockDefaults;
|
|
4
|
+
/**
|
|
5
|
+
* Override strong-mock's defaults.
|
|
6
|
+
*
|
|
7
|
+
* @param newDefaults These will be applied to the library defaults. Multiple
|
|
8
|
+
* calls don't stack e.g. calling this with `{}` will clear any previously
|
|
9
|
+
* applied defaults.
|
|
10
|
+
*/
|
|
11
|
+
export declare const setDefaults: (newDefaults: MockOptions) => void;
|
package/dist/mock/map.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
|
|
2
|
-
import type {
|
|
3
|
-
import type { StrongMockDefaults } from './defaults';
|
|
4
|
-
import type { Mock } from './mock';
|
|
5
|
-
export declare const setActiveMock: (mock: Mock<any>) => void;
|
|
6
|
-
export declare const clearActiveMock: () => void;
|
|
7
|
-
export declare const getActiveMock: () => Mock<any>;
|
|
8
|
-
type MockState = {
|
|
9
|
-
repository: ExpectationRepository;
|
|
10
|
-
|
|
11
|
-
options: StrongMockDefaults;
|
|
12
|
-
};
|
|
13
|
-
export declare const getMockState: (mock: Mock<any>) => MockState;
|
|
14
|
-
export declare const setMockState: (mock: Mock<any>, state: MockState) => void;
|
|
15
|
-
export declare const getAllMocks: () => [Mock<any>, MockState][];
|
|
16
|
-
export {};
|
|
1
|
+
import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
|
|
2
|
+
import type { ExpectationBuilder } from '../when/expectation-builder';
|
|
3
|
+
import type { StrongMockDefaults } from './defaults';
|
|
4
|
+
import type { Mock } from './mock';
|
|
5
|
+
export declare const setActiveMock: (mock: Mock<any>) => void;
|
|
6
|
+
export declare const clearActiveMock: () => void;
|
|
7
|
+
export declare const getActiveMock: () => Mock<any>;
|
|
8
|
+
type MockState = {
|
|
9
|
+
repository: ExpectationRepository;
|
|
10
|
+
builder: ExpectationBuilder;
|
|
11
|
+
options: StrongMockDefaults;
|
|
12
|
+
};
|
|
13
|
+
export declare const getMockState: (mock: Mock<any>) => MockState;
|
|
14
|
+
export declare const setMockState: (mock: Mock<any>, state: MockState) => void;
|
|
15
|
+
export declare const getAllMocks: () => [Mock<any>, MockState][];
|
|
16
|
+
export {};
|
package/dist/mock/mock.d.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import type { MockOptions } from './options';
|
|
2
|
-
export type Mock<T> = T;
|
|
3
|
-
export declare enum Mode {
|
|
4
|
-
EXPECT = 0,
|
|
5
|
-
CALL = 1
|
|
6
|
-
}
|
|
7
|
-
export declare const setMode: (mode: Mode) => void;
|
|
8
|
-
/**
|
|
9
|
-
* Create a type safe mock.
|
|
10
|
-
*
|
|
11
|
-
* @see {@link when} Set expectations on the mock using `when`.
|
|
12
|
-
*
|
|
13
|
-
* @param options Configure the options for this specific mock, overriding any
|
|
14
|
-
* defaults that were set with {@link setDefaults}.
|
|
15
|
-
* @param options.unexpectedProperty Controls what happens when an unexpected
|
|
16
|
-
* property is accessed.
|
|
17
|
-
* @param options.concreteMatcher The matcher that will be used when one isn't
|
|
18
|
-
* specified explicitly.
|
|
19
|
-
* @param options.exactParams Controls whether the number of received arguments
|
|
20
|
-
* has to match the expectation.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* const fn = mock<() => number>();
|
|
24
|
-
*
|
|
25
|
-
* when(() => fn()).thenReturn(23);
|
|
26
|
-
*
|
|
27
|
-
* fn() === 23;
|
|
28
|
-
*/
|
|
29
|
-
export declare const mock: <T>({ unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => T;
|
|
1
|
+
import type { MockOptions } from './options';
|
|
2
|
+
export type Mock<T> = T;
|
|
3
|
+
export declare enum Mode {
|
|
4
|
+
EXPECT = 0,
|
|
5
|
+
CALL = 1
|
|
6
|
+
}
|
|
7
|
+
export declare const setMode: (mode: Mode) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Create a type safe mock.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link when} Set expectations on the mock using `when`.
|
|
12
|
+
*
|
|
13
|
+
* @param options Configure the options for this specific mock, overriding any
|
|
14
|
+
* defaults that were set with {@link setDefaults}.
|
|
15
|
+
* @param options.unexpectedProperty Controls what happens when an unexpected
|
|
16
|
+
* property is accessed.
|
|
17
|
+
* @param options.concreteMatcher The matcher that will be used when one isn't
|
|
18
|
+
* specified explicitly.
|
|
19
|
+
* @param options.exactParams Controls whether the number of received arguments
|
|
20
|
+
* has to match the expectation.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const fn = mock<() => number>();
|
|
24
|
+
*
|
|
25
|
+
* when(() => fn()).thenReturn(23);
|
|
26
|
+
*
|
|
27
|
+
* fn() === 23;
|
|
28
|
+
*/
|
|
29
|
+
export declare const mock: <T>({ unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => T;
|
package/dist/mock/options.d.ts
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
import type { Matcher } from '../
|
|
2
|
-
export type ConcreteMatcher = <T>(expected: T) => Matcher;
|
|
3
|
-
export declare enum UnexpectedProperty {
|
|
4
|
-
/**
|
|
5
|
-
* Throw an error immediately.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* // Will throw "Didn't expect foo to be accessed".
|
|
9
|
-
* const { foo } = service;
|
|
10
|
-
*
|
|
11
|
-
* // Will throw "Didn't expect foo to be accessed",
|
|
12
|
-
* // without printing the arguments.
|
|
13
|
-
* foo(42);
|
|
14
|
-
*/
|
|
15
|
-
THROW = 0,
|
|
16
|
-
/**
|
|
17
|
-
* Return a function that will throw if called. This can be useful if your
|
|
18
|
-
* code destructures a function but never calls it.
|
|
19
|
-
*
|
|
20
|
-
* It will also improve error messages for unexpected calls because arguments
|
|
21
|
-
* will be captured instead of throwing immediately on the property access.
|
|
22
|
-
*
|
|
23
|
-
* The function will be returned even if the property is not supposed to be a
|
|
24
|
-
* function. This could cause weird behavior at runtime, when your code expects
|
|
25
|
-
* e.g. a number and gets a function instead.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* // This will NOT throw.
|
|
29
|
-
* const { foo } = service;
|
|
30
|
-
*
|
|
31
|
-
* // This will NOT throw, and might produce unexpected results.
|
|
32
|
-
* foo > 0
|
|
33
|
-
*
|
|
34
|
-
* // Will throw "Didn't expect foo(42) to be called".
|
|
35
|
-
* foo(42);
|
|
36
|
-
*/
|
|
37
|
-
CALL_THROW = 1
|
|
38
|
-
}
|
|
39
|
-
export interface MockOptions {
|
|
40
|
-
/**
|
|
41
|
-
* Controls what should be returned for a property with no expectations.
|
|
42
|
-
*
|
|
43
|
-
* A property with no expectations is a property that has no `when`
|
|
44
|
-
* expectations set on it. It can also be a property that ran out of `when`
|
|
45
|
-
* expectations.
|
|
46
|
-
*
|
|
47
|
-
* The default is to return a function that will throw when called.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* const foo = mock<{ bar: () => number }>();
|
|
51
|
-
* foo.bar() // unexpected property access
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* const foo = mock<{ bar: () => number }>();
|
|
55
|
-
* when(() => foo.bar()).thenReturn(42);
|
|
56
|
-
* foo.bar() === 42
|
|
57
|
-
* foo.bar() // unexpected property access
|
|
58
|
-
*/
|
|
59
|
-
unexpectedProperty?: UnexpectedProperty;
|
|
60
|
-
/**
|
|
61
|
-
* If `true`, the number of received arguments in a function/method call has to
|
|
62
|
-
* match the number of arguments set in the expectation.
|
|
63
|
-
*
|
|
64
|
-
* If `false`, extra parameters are considered optional and checked by the
|
|
65
|
-
* TypeScript compiler instead.
|
|
66
|
-
*
|
|
67
|
-
* You may want to set this to `true` if you're not using TypeScript,
|
|
68
|
-
* or if you want to be extra strict.
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* const fn = mock<(value?: number) => number>({ exactParams: true });
|
|
72
|
-
* when(() => fn()).thenReturn(42);
|
|
73
|
-
*
|
|
74
|
-
* fn(100) // throws with exactParams, returns 42 without
|
|
75
|
-
*/
|
|
76
|
-
exactParams?: boolean;
|
|
77
|
-
/**
|
|
78
|
-
* The matcher that will be used when one isn't specified explicitly.
|
|
79
|
-
*
|
|
80
|
-
* The most common use case is replacing the default {@link It.deepEquals}
|
|
81
|
-
* matcher with {@link It.is}, but you can also use {@link It.matches} to
|
|
82
|
-
* create a custom matcher.
|
|
83
|
-
*
|
|
84
|
-
* @param expected The concrete expected value received from the
|
|
85
|
-
* {@link when} expectation.
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* const fn = mock<(value: number[]) => boolean>({
|
|
89
|
-
* concreteMatcher: It.is
|
|
90
|
-
* });
|
|
91
|
-
*
|
|
92
|
-
* const expected = [1, 2, 3];
|
|
93
|
-
* when(() => fn(expected).thenReturn(true);
|
|
94
|
-
*
|
|
95
|
-
* fn([1, 2, 3]); // throws because different array instance
|
|
96
|
-
* fn(expected); // OK
|
|
97
|
-
*/
|
|
98
|
-
concreteMatcher?: ConcreteMatcher;
|
|
99
|
-
}
|
|
1
|
+
import type { Matcher } from '../matchers/matcher';
|
|
2
|
+
export type ConcreteMatcher = <T>(expected: T) => Matcher;
|
|
3
|
+
export declare enum UnexpectedProperty {
|
|
4
|
+
/**
|
|
5
|
+
* Throw an error immediately.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Will throw "Didn't expect foo to be accessed".
|
|
9
|
+
* const { foo } = service;
|
|
10
|
+
*
|
|
11
|
+
* // Will throw "Didn't expect foo to be accessed",
|
|
12
|
+
* // without printing the arguments.
|
|
13
|
+
* foo(42);
|
|
14
|
+
*/
|
|
15
|
+
THROW = 0,
|
|
16
|
+
/**
|
|
17
|
+
* Return a function that will throw if called. This can be useful if your
|
|
18
|
+
* code destructures a function but never calls it.
|
|
19
|
+
*
|
|
20
|
+
* It will also improve error messages for unexpected calls because arguments
|
|
21
|
+
* will be captured instead of throwing immediately on the property access.
|
|
22
|
+
*
|
|
23
|
+
* The function will be returned even if the property is not supposed to be a
|
|
24
|
+
* function. This could cause weird behavior at runtime, when your code expects
|
|
25
|
+
* e.g. a number and gets a function instead.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // This will NOT throw.
|
|
29
|
+
* const { foo } = service;
|
|
30
|
+
*
|
|
31
|
+
* // This will NOT throw, and might produce unexpected results.
|
|
32
|
+
* foo > 0
|
|
33
|
+
*
|
|
34
|
+
* // Will throw "Didn't expect foo(42) to be called".
|
|
35
|
+
* foo(42);
|
|
36
|
+
*/
|
|
37
|
+
CALL_THROW = 1
|
|
38
|
+
}
|
|
39
|
+
export interface MockOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Controls what should be returned for a property with no expectations.
|
|
42
|
+
*
|
|
43
|
+
* A property with no expectations is a property that has no `when`
|
|
44
|
+
* expectations set on it. It can also be a property that ran out of `when`
|
|
45
|
+
* expectations.
|
|
46
|
+
*
|
|
47
|
+
* The default is to return a function that will throw when called.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* const foo = mock<{ bar: () => number }>();
|
|
51
|
+
* foo.bar() // unexpected property access
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* const foo = mock<{ bar: () => number }>();
|
|
55
|
+
* when(() => foo.bar()).thenReturn(42);
|
|
56
|
+
* foo.bar() === 42
|
|
57
|
+
* foo.bar() // unexpected property access
|
|
58
|
+
*/
|
|
59
|
+
unexpectedProperty?: UnexpectedProperty;
|
|
60
|
+
/**
|
|
61
|
+
* If `true`, the number of received arguments in a function/method call has to
|
|
62
|
+
* match the number of arguments set in the expectation.
|
|
63
|
+
*
|
|
64
|
+
* If `false`, extra parameters are considered optional and checked by the
|
|
65
|
+
* TypeScript compiler instead.
|
|
66
|
+
*
|
|
67
|
+
* You may want to set this to `true` if you're not using TypeScript,
|
|
68
|
+
* or if you want to be extra strict.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const fn = mock<(value?: number) => number>({ exactParams: true });
|
|
72
|
+
* when(() => fn()).thenReturn(42);
|
|
73
|
+
*
|
|
74
|
+
* fn(100) // throws with exactParams, returns 42 without
|
|
75
|
+
*/
|
|
76
|
+
exactParams?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* The matcher that will be used when one isn't specified explicitly.
|
|
79
|
+
*
|
|
80
|
+
* The most common use case is replacing the default {@link It.deepEquals}
|
|
81
|
+
* matcher with {@link It.is}, but you can also use {@link It.matches} to
|
|
82
|
+
* create a custom matcher.
|
|
83
|
+
*
|
|
84
|
+
* @param expected The concrete expected value received from the
|
|
85
|
+
* {@link when} expectation.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* const fn = mock<(value: number[]) => boolean>({
|
|
89
|
+
* concreteMatcher: It.is
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* const expected = [1, 2, 3];
|
|
93
|
+
* when(() => fn(expected).thenReturn(true);
|
|
94
|
+
*
|
|
95
|
+
* fn([1, 2, 3]); // throws because different array instance
|
|
96
|
+
* fn(expected); // OK
|
|
97
|
+
*/
|
|
98
|
+
concreteMatcher?: ConcreteMatcher;
|
|
99
|
+
}
|
package/dist/mock/stub.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
|
|
2
|
-
import type {
|
|
3
|
-
import type { Mock } from './mock';
|
|
4
|
-
import { Mode } from './mock';
|
|
5
|
-
export declare const createStub: <T>(repo: ExpectationRepository,
|
|
1
|
+
import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
|
|
2
|
+
import type { ExpectationBuilder } from '../when/expectation-builder';
|
|
3
|
+
import type { Mock } from './mock';
|
|
4
|
+
import { Mode } from './mock';
|
|
5
|
+
export declare const createStub: <T>(repo: ExpectationRepository, builder: ExpectationBuilder, getCurrentMode: () => Mode) => T;
|
package/dist/print.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { Expectation } from './expectation/expectation';
|
|
2
|
-
import type { ReturnValue } from './expectation/repository/return-value';
|
|
3
|
-
import type { Property } from './proxy';
|
|
4
|
-
export declare const printProperty: (property: Property) => string;
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const printCall: (property: Property, args
|
|
7
|
-
export declare const printReturns: ({ isError, isPromise, value }: ReturnValue, min: number, max: number) => string;
|
|
8
|
-
export declare const printWhen: (property: Property, args: any[] | undefined) => string;
|
|
9
|
-
export declare const printExpectation: (property: Property, args: any[] | undefined, returnValue: ReturnValue, min: number, max: number) => string;
|
|
10
|
-
export declare const printRemainingExpectations: (expectations: Expectation[]) => string;
|
|
1
|
+
import type { Expectation } from './expectation/expectation';
|
|
2
|
+
import type { ReturnValue } from './expectation/repository/return-value';
|
|
3
|
+
import type { Property } from './proxy';
|
|
4
|
+
export declare const printProperty: (property: Property) => string;
|
|
5
|
+
export declare const printValue: (arg: unknown) => string;
|
|
6
|
+
export declare const printCall: (property: Property, args?: any[]) => string;
|
|
7
|
+
export declare const printReturns: ({ isError, isPromise, value }: ReturnValue, min: number, max: number) => string;
|
|
8
|
+
export declare const printWhen: (property: Property, args: any[] | undefined) => string;
|
|
9
|
+
export declare const printExpectation: (property: Property, args: any[] | undefined, returnValue: ReturnValue, min: number, max: number) => string;
|
|
10
|
+
export declare const printRemainingExpectations: (expectations: Expectation[]) => string;
|
package/dist/proxy.d.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import type { Mock } from './mock/mock';
|
|
2
|
-
export type Property = string | symbol;
|
|
3
|
-
export interface ProxyTraps {
|
|
4
|
-
/**
|
|
5
|
-
* Called when accessing any property on an object, except for
|
|
6
|
-
* `.call`, `.apply` and `.bind`.
|
|
7
|
-
*/
|
|
8
|
-
property: (property: Property) => unknown;
|
|
9
|
-
/**
|
|
10
|
-
* Called when calling a function.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```
|
|
14
|
-
* fn(...args)
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```
|
|
19
|
-
* fn.call(this, ...args)
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```
|
|
24
|
-
* fn.apply(this, [...args])
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```
|
|
29
|
-
* Reflect.apply(fn, this, [...args])
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
apply: (args:
|
|
33
|
-
/**
|
|
34
|
-
* Called when getting the proxy's own enumerable keys.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```
|
|
38
|
-
* Object.keys(proxy);
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```
|
|
43
|
-
* const foo = { ...proxy };
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
ownKeys: () => Property[];
|
|
47
|
-
}
|
|
48
|
-
export declare const createProxy: <T>(traps: ProxyTraps) => T;
|
|
1
|
+
import type { Mock } from './mock/mock';
|
|
2
|
+
export type Property = string | symbol;
|
|
3
|
+
export interface ProxyTraps {
|
|
4
|
+
/**
|
|
5
|
+
* Called when accessing any property on an object, except for
|
|
6
|
+
* `.call`, `.apply` and `.bind`.
|
|
7
|
+
*/
|
|
8
|
+
property: (property: Property) => unknown;
|
|
9
|
+
/**
|
|
10
|
+
* Called when calling a function.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```
|
|
14
|
+
* fn(...args)
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```
|
|
19
|
+
* fn.call(this, ...args)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```
|
|
24
|
+
* fn.apply(this, [...args])
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```
|
|
29
|
+
* Reflect.apply(fn, this, [...args])
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
apply: (args: unknown[]) => unknown;
|
|
33
|
+
/**
|
|
34
|
+
* Called when getting the proxy's own enumerable keys.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```
|
|
38
|
+
* Object.keys(proxy);
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```
|
|
43
|
+
* const foo = { ...proxy };
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
ownKeys: () => Property[];
|
|
47
|
+
}
|
|
48
|
+
export declare const createProxy: <T>(traps: ProxyTraps) => T;
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import type { Expectation } from '../expectation/expectation';
|
|
2
|
-
export interface InvocationCount {
|
|
3
|
-
/**
|
|
4
|
-
* Expect a call to be made at least `min` times and at most `max` times.
|
|
5
|
-
*/
|
|
6
|
-
between(min: number, max: number)
|
|
7
|
-
/**
|
|
8
|
-
* Expect a call to be made exactly `exact` times.
|
|
9
|
-
*
|
|
10
|
-
* Shortcut for `between(exact, exact)`.
|
|
11
|
-
*/
|
|
12
|
-
times(exact: number)
|
|
13
|
-
/**
|
|
14
|
-
* Expect a call to be made any number of times, including never.
|
|
15
|
-
*
|
|
16
|
-
* Shortcut for `between(0, Infinity)`.
|
|
17
|
-
*/
|
|
18
|
-
anyTimes()
|
|
19
|
-
/**
|
|
20
|
-
* Expect a call to be made at least `min` times.
|
|
21
|
-
*
|
|
22
|
-
* Shortcut for `between(min, Infinity)`.
|
|
23
|
-
*/
|
|
24
|
-
atLeast(min: number)
|
|
25
|
-
/**
|
|
26
|
-
* Expect a call to be made at most `max` times.
|
|
27
|
-
*
|
|
28
|
-
* Shortcut for `between(0, max)`.
|
|
29
|
-
*/
|
|
30
|
-
atMost(max: number)
|
|
31
|
-
/**
|
|
32
|
-
* Expect a call to be made exactly once.
|
|
33
|
-
*
|
|
34
|
-
* Shortcut for `times(1)`.
|
|
35
|
-
*/
|
|
36
|
-
once()
|
|
37
|
-
/**
|
|
38
|
-
* Expect a call to be made exactly twice.
|
|
39
|
-
*
|
|
40
|
-
* Shortcut for `times(2)`.
|
|
41
|
-
*/
|
|
42
|
-
twice()
|
|
43
|
-
}
|
|
44
|
-
export declare const createInvocationCount: (expectation: Expectation) => InvocationCount;
|
|
1
|
+
import type { Expectation } from '../expectation/expectation';
|
|
2
|
+
export interface InvocationCount {
|
|
3
|
+
/**
|
|
4
|
+
* Expect a call to be made at least `min` times and at most `max` times.
|
|
5
|
+
*/
|
|
6
|
+
between: (min: number, max: number) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Expect a call to be made exactly `exact` times.
|
|
9
|
+
*
|
|
10
|
+
* Shortcut for `between(exact, exact)`.
|
|
11
|
+
*/
|
|
12
|
+
times: (exact: number) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Expect a call to be made any number of times, including never.
|
|
15
|
+
*
|
|
16
|
+
* Shortcut for `between(0, Infinity)`.
|
|
17
|
+
*/
|
|
18
|
+
anyTimes: () => void;
|
|
19
|
+
/**
|
|
20
|
+
* Expect a call to be made at least `min` times.
|
|
21
|
+
*
|
|
22
|
+
* Shortcut for `between(min, Infinity)`.
|
|
23
|
+
*/
|
|
24
|
+
atLeast: (min: number) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Expect a call to be made at most `max` times.
|
|
27
|
+
*
|
|
28
|
+
* Shortcut for `between(0, max)`.
|
|
29
|
+
*/
|
|
30
|
+
atMost: (max: number) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Expect a call to be made exactly once.
|
|
33
|
+
*
|
|
34
|
+
* Shortcut for `times(1)`.
|
|
35
|
+
*/
|
|
36
|
+
once: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Expect a call to be made exactly twice.
|
|
39
|
+
*
|
|
40
|
+
* Shortcut for `times(2)`.
|
|
41
|
+
*/
|
|
42
|
+
twice: () => void;
|
|
43
|
+
}
|
|
44
|
+
export declare const createInvocationCount: (expectation: Expectation) => InvocationCount;
|