prostub 1.1.0 → 1.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 +115 -2
- package/index.d.ts +465 -94
- package/index.js +1 -582
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
interface Type<T extends object> {
|
|
2
|
-
new (...args: never[]): T;
|
|
3
|
-
prototype: T;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
1
|
declare const mock$1: unique symbol;
|
|
7
2
|
declare const stub$1: unique symbol;
|
|
8
3
|
|
|
9
|
-
interface
|
|
4
|
+
interface InteractionRecord {
|
|
5
|
+
time: bigint;
|
|
6
|
+
}
|
|
7
|
+
interface InvocationRecord<TArgs extends Array<unknown>, TReturn> extends InteractionRecord {
|
|
10
8
|
args: TArgs;
|
|
11
9
|
returnValue?: TReturn;
|
|
12
|
-
invocationTime: Date;
|
|
13
10
|
exception?: unknown;
|
|
14
11
|
}
|
|
15
|
-
interface AssignmentRecord<TValue> {
|
|
12
|
+
interface AssignmentRecord<TValue> extends InteractionRecord {
|
|
16
13
|
newValue: TValue;
|
|
17
|
-
assignmentTime: Date;
|
|
18
14
|
}
|
|
19
|
-
interface ReadRecord<TValue> {
|
|
15
|
+
interface ReadRecord<TValue> extends InteractionRecord {
|
|
20
16
|
value: TValue;
|
|
21
|
-
readTime: Date;
|
|
22
17
|
}
|
|
23
18
|
interface FunctionStub<TThis extends object, TKey extends keyof TThis, TArgs extends Array<unknown>, TReturn> {
|
|
24
19
|
[stub$1]: {
|
|
@@ -31,84 +26,221 @@ interface PropertyStubMetadata<TValue> {
|
|
|
31
26
|
reads: Array<ReadRecord<TValue>>;
|
|
32
27
|
assignments: Array<AssignmentRecord<TValue>>;
|
|
33
28
|
}
|
|
34
|
-
|
|
29
|
+
interface FunctionStubMetadata<TArgs extends Array<unknown>, TReturn> {
|
|
35
30
|
calls: Array<InvocationRecord<TArgs, TReturn>>;
|
|
36
|
-
}
|
|
31
|
+
}
|
|
37
32
|
interface PropertyStub<TThis extends object, TKey extends keyof TThis> {
|
|
38
33
|
[stub$1]: {
|
|
34
|
+
metadata: PropertyStubMetadata<TThis[TKey]>;
|
|
39
35
|
type: "property";
|
|
40
36
|
get(thisArg: TThis, propertyName: TKey): TThis[TKey];
|
|
41
37
|
set(thisArg: TThis, propertyName: TKey, newValue: TThis[TKey]): void;
|
|
42
|
-
metadata: {
|
|
43
|
-
reads: Array<ReadRecord<TThis[TKey]>>;
|
|
44
|
-
assignments: Array<AssignmentRecord<TThis[TKey]>>;
|
|
45
|
-
};
|
|
46
38
|
};
|
|
47
39
|
}
|
|
48
40
|
type Stubbable<T extends object> = {
|
|
49
41
|
-readonly [K in keyof T]: Stub<T, K>;
|
|
50
42
|
};
|
|
51
43
|
type Stub<TThis extends object, TKey extends keyof TThis> = TThis[TKey] extends (...args: infer TArgs) => infer TReturn ? FunctionStub<TThis, TKey, TArgs, TReturn> : PropertyStub<TThis, TKey>;
|
|
52
|
-
type Mock<T extends
|
|
44
|
+
type Mock<T extends object> = T & {
|
|
53
45
|
[mock$1]: {
|
|
54
46
|
stubs: {
|
|
55
|
-
-readonly [TKey in keyof T]
|
|
47
|
+
-readonly [TKey in keyof T]: Stub<T, TKey>;
|
|
56
48
|
};
|
|
57
49
|
};
|
|
58
50
|
};
|
|
59
51
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
} & {
|
|
65
|
-
[mock$1]: {
|
|
66
|
-
stubs: TObject & {
|
|
67
|
-
[mock$1]: {
|
|
68
|
-
stubs: { -readonly [TKey_1 in keyof TObject]?: Stub<TObject, TKey_1>; };
|
|
69
|
-
};
|
|
70
|
-
} extends infer T extends object ? { -readonly [TKey in keyof T]?: Stub<T, TKey> | undefined; } : never;
|
|
71
|
-
};
|
|
72
|
-
};
|
|
52
|
+
interface Type<T extends object> {
|
|
53
|
+
prototype: T;
|
|
54
|
+
new (...args: Array<never>): T;
|
|
55
|
+
}
|
|
73
56
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Creates a mock instance of the given class.
|
|
59
|
+
* @param clazz The class to mock.
|
|
60
|
+
* @returns A mock instance of the class.
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* class MyClass {
|
|
64
|
+
* myMethod() {
|
|
65
|
+
* return "real value";
|
|
66
|
+
* }
|
|
67
|
+
* }
|
|
68
|
+
* const myClassMock = mock(MyClass);
|
|
69
|
+
* // Now you can stub methods and properties on myClassMock as needed.
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @see {@link stub} for stubbing methods and properties on the mock.
|
|
73
|
+
* @see {@link spy} for creating spies on real instances.
|
|
74
|
+
*/
|
|
75
|
+
declare function mock<const TObject extends object>(clazz: Type<TObject>): Mock<TObject>;
|
|
81
76
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Creates a spy for the given real object. A spy wraps the real object and allows you to stub methods and properties while still calling through to the real implementations by default.
|
|
79
|
+
* @returns A mock object that wraps the real object and allows stubbing.
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* class MyClass {
|
|
83
|
+
* myMethod() {
|
|
84
|
+
* return "real value";
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* const myRealInstance = new MyClass();
|
|
88
|
+
* const mySpy = spy(myRealInstance);
|
|
89
|
+
* // Now you can stub methods and properties on mySpy as needed.
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @see {@link stub} for stubbing methods and properties on the spy.
|
|
93
|
+
* @see {@link mock} for creating mock instances of classes.
|
|
94
|
+
* @param realObject The real object to create a spy for.
|
|
95
|
+
*/
|
|
96
|
+
declare function spy<const TObject extends object>(realObject: TObject): Mock<TObject>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Creates a {@link Stubbable} for the given mock or spy. The returned object allows you to define stubs for methods and properties on the mock or spy.
|
|
100
|
+
* @returns A stubbable object for defining stubs on the mock or spy.
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const myMock = mock(MyClass);
|
|
104
|
+
* const myStubs = stub(myMock);
|
|
105
|
+
* myStubs.myMethod = returnFixed("stubbed value");
|
|
106
|
+
* myStubs.myProperty = fixedValue(42);
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @see {@link mock} for creating mock instances of classes.
|
|
110
|
+
* @see {@link spy} for creating spies on real instances.
|
|
111
|
+
* @param mockOrSpy The mock or spy to create stubs for. Must be created using the `mock` or `spy` functions.
|
|
112
|
+
*/
|
|
113
|
+
declare function stub<const TObject extends object>(mockOrSpy: Mock<TObject>): Stubbable<TObject>;
|
|
89
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Creates a function stub that uses the provided function to generate return values or throw exceptions when invoked.
|
|
117
|
+
* It also records each invocation's arguments, return values, exceptions, and timestamps.
|
|
118
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
119
|
+
* @typeParam TKey - The key of the function within the object.
|
|
120
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
121
|
+
* @typeParam TReturn - The return type of the function.
|
|
122
|
+
* @param answerFunction - The function to be used as the stub implementation.
|
|
123
|
+
* @returns A function stub that records invocations and uses the provided function for behavior.
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* stub(myStubbedObject).someMethod = answer(function(this: MyObject, arg1: number, arg2: string): boolean {
|
|
127
|
+
* if (arg1 > 0) {
|
|
128
|
+
* return true;
|
|
129
|
+
* } else {
|
|
130
|
+
* throw new Error("arg1 must be positive");
|
|
131
|
+
* }
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
90
135
|
declare function answer<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>, const TReturn>(answerFunction: (this: TObject, ...args: TArgs) => TReturn): FunctionStub<TObject, TKey, TArgs, TReturn>;
|
|
91
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Creates a function stub that calls through to the original implementation of the function when invoked.
|
|
139
|
+
* It also records each invocation's arguments, return values, exceptions, and timestamps.
|
|
140
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
141
|
+
* @typeParam TKey - The key of the function within the object.
|
|
142
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
143
|
+
* @typeParam TReturn - The return type of the function.
|
|
144
|
+
* @returns A function stub that records invocations and calls through to the original implementation.
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* stub(mySpyObject).someMethod = callThrough();
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
92
150
|
declare function callThrough<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>, const TReturn>(): FunctionStub<TObject, TKey, TArgs, TReturn>;
|
|
93
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Creates a function stub that delegates its implementation to the provided delegate function.
|
|
154
|
+
* It also records each invocation's arguments, return values, and timestamps.
|
|
155
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
156
|
+
* @typeParam TKey - The key of the function within the object.
|
|
157
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
158
|
+
* @typeParam TReturn - The return type of the function.
|
|
159
|
+
* @returns A function stub that records invocations and delegates to the provided function.
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* stub(myStubbedObject).someMethod = delegateTo(function(this: MyObject, arg1: number, arg2: string): boolean {
|
|
163
|
+
* return arg1 > 0 && arg2.length > 0;
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
* @param delegateFunction - The function to which calls will be delegated.
|
|
167
|
+
*/
|
|
94
168
|
declare function delegateTo<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>, const TReturn>(delegateFunction: (this: TObject, ...args: TArgs) => TReturn): FunctionStub<TObject, TKey, TArgs, TReturn>;
|
|
95
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Creates a no-operation function stub that records each invocation's arguments and timestamps.
|
|
172
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
173
|
+
* @typeParam TKey - The key of the function within the object.
|
|
174
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
175
|
+
* @returns A no-operation function stub that records invocations.
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* stub(myStubbedObject).someMethod = noop();
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
declare function noop<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>>(): FunctionStub<TObject, TKey, TArgs, void>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Creates a function stub that always returns a fixed value when invoked.
|
|
185
|
+
* It also records each invocation's arguments, return values, and timestamps.
|
|
186
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
187
|
+
* @typeParam TKey - The key of the function within the object.
|
|
188
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
189
|
+
* @typeParam TReturn - The return type of the function.
|
|
190
|
+
* @returns A function stub that always returns the specified fixed value and records invocations.
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* stub(myStubbedObject).someMethod = returnFixed(42);
|
|
194
|
+
* ```
|
|
195
|
+
* @param returnValue
|
|
196
|
+
*/
|
|
96
197
|
declare function returnFixed<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>, const TReturn>(returnValue: TReturn): FunctionStub<TObject, TKey, TArgs, TReturn>;
|
|
97
198
|
|
|
199
|
+
/**
|
|
200
|
+
* Creates a function stub that returns a series of specified values in order when invoked.
|
|
201
|
+
* Once all values have been returned, further invocations will throw an error.
|
|
202
|
+
* It also records each invocation's arguments, return values, exceptions, and timestamps.
|
|
203
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
204
|
+
* @typeParam TKey - The key of the function within the object.
|
|
205
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
206
|
+
* @typeParam TReturn - The return type of the function.
|
|
207
|
+
* @returns A function stub that returns the specified values in order and records invocations.
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* stub(myStubbedObject).someMethod = returnSerial(1, 2, 3);
|
|
211
|
+
* ```
|
|
212
|
+
* @param values
|
|
213
|
+
*/
|
|
98
214
|
declare function returnSerial<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>, const TReturn>(...values: Array<TReturn>): FunctionStub<TObject, TKey, TArgs, TReturn>;
|
|
99
215
|
|
|
100
|
-
|
|
101
|
-
|
|
216
|
+
/**
|
|
217
|
+
* Creates a function stub that throws an error when invoked.
|
|
218
|
+
* It also records each invocation's arguments, exceptions, and timestamps.
|
|
219
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
220
|
+
* @typeParam TKey - The key of the function within the object.
|
|
221
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
222
|
+
* @returns A function stub that throws an error on invocation and records invocations.
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* stub(myStubbedObject).someMethod = throwOnCall(() => new Error("method was called"));
|
|
226
|
+
* ```
|
|
227
|
+
* @param errorFactory
|
|
228
|
+
*/
|
|
102
229
|
declare function throwOnCall<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>>(errorFactory: () => Error): FunctionStub<TObject, TKey, TArgs, never>;
|
|
103
230
|
|
|
104
231
|
type ArgumentPredicate<TArgs extends Array<unknown>> = TArgs extends [infer TFirst, ...infer TRest] ? [
|
|
105
232
|
((arg: TFirst) => boolean),
|
|
106
233
|
...ArgumentPredicate<TRest>
|
|
107
|
-
] : TArgs extends [infer TSingle] ? [
|
|
108
|
-
(arg: TSingle) => boolean
|
|
109
|
-
] : TArgs extends [] ? [
|
|
110
|
-
] : never;
|
|
234
|
+
] : TArgs extends [infer TSingle] ? [(arg: TSingle) => boolean] : TArgs extends [] ? [] : never;
|
|
111
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Ongoing function stub that matches specific argument predicates.
|
|
238
|
+
* It allows chaining multiple stubs with different argument matching criteria.
|
|
239
|
+
* @typeParam TThis - The type of the object containing the function.
|
|
240
|
+
* @typeParam TKey - The key of the function within the object.
|
|
241
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
242
|
+
* @typeParam TReturn - The return type of the function.
|
|
243
|
+
*/
|
|
112
244
|
declare class OngoingFunctionStub<const TThis extends object, const TKey extends keyof TThis, const TArgs extends Array<unknown>, const TReturn> implements FunctionStub<TThis, TKey, TArgs, TReturn> {
|
|
113
245
|
#private;
|
|
114
246
|
get [stub$1](): {
|
|
@@ -116,9 +248,27 @@ declare class OngoingFunctionStub<const TThis extends object, const TKey extends
|
|
|
116
248
|
type: "function";
|
|
117
249
|
metadata: FunctionStubMetadata<TArgs, TReturn>;
|
|
118
250
|
};
|
|
119
|
-
constructor(argumentPredicates: ArgumentPredicate<TArgs>,
|
|
251
|
+
constructor(argumentPredicates: ArgumentPredicate<TArgs>, functionStub: FunctionStub<TThis, TKey, TArgs, TReturn>, parentStub?: OngoingFunctionStub<TThis, TKey, TArgs, TReturn>);
|
|
252
|
+
/**
|
|
253
|
+
* Begins the definition of an additional function stub that matches specific argument predicates.
|
|
254
|
+
* Use the returned builder to complete the stub definition with the `then` method.
|
|
255
|
+
* @returns An ongoing function stub builder for defining the stub behavior.
|
|
256
|
+
* @typeParam TThis - The type of the object containing the function.
|
|
257
|
+
* @typeParam TKey - The key of the function within the object.
|
|
258
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
259
|
+
* @typeParam TReturn - The return type of the function.
|
|
260
|
+
* @param argumentPredicates - The predicates to match the function arguments.
|
|
261
|
+
*/
|
|
120
262
|
when(...argumentPredicates: ArgumentPredicate<TArgs>): OngoingFunctionStubBuilder<TThis, TKey, TArgs, TReturn>;
|
|
121
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Builder for defining ongoing function stubs with argument matching.
|
|
266
|
+
* Use the `then` method to complete the stub definition.
|
|
267
|
+
* @typeParam TThis - The type of the object containing the function.
|
|
268
|
+
* @typeParam TKey - The key of the function within the object.
|
|
269
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
270
|
+
* @typeParam TReturn - The return type of the function.
|
|
271
|
+
*/
|
|
122
272
|
declare class OngoingFunctionStubBuilder<const TThis extends object, const TKey extends keyof TThis, const TArgs extends Array<unknown>, const TReturn> implements FunctionStub<TThis, TKey, TArgs, TReturn> {
|
|
123
273
|
#private;
|
|
124
274
|
get [stub$1](): {
|
|
@@ -129,83 +279,304 @@ declare class OngoingFunctionStubBuilder<const TThis extends object, const TKey
|
|
|
129
279
|
};
|
|
130
280
|
};
|
|
131
281
|
constructor(argumentPredicates: ArgumentPredicate<TArgs>, parentStub?: OngoingFunctionStub<TThis, TKey, TArgs, TReturn>);
|
|
132
|
-
|
|
282
|
+
/**
|
|
283
|
+
* Completes the stub definition by specifying the function stub to use when the argument predicates match.
|
|
284
|
+
* @returns An ongoing function stub that can be further extended with additional argument matching.
|
|
285
|
+
* @typeParam TThis - The type of the object containing the function.
|
|
286
|
+
* @typeParam TKey - The key of the function within the object.
|
|
287
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
288
|
+
* @typeParam TReturn - The return type of the function.
|
|
289
|
+
* @param nextStub - The function stub to use when the argument predicates match.
|
|
290
|
+
*/
|
|
291
|
+
then(nextStub: FunctionStub<TThis, TKey, TArgs, TReturn>): OngoingFunctionStub<TThis, TKey, TArgs, TReturn>;
|
|
133
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* Begins the definition of a function stub that matches specific argument predicates.
|
|
295
|
+
* Use the returned builder to complete the stub definition with the `then` method.
|
|
296
|
+
* @typeParam TObject - The type of the object containing the function.
|
|
297
|
+
* @typeParam TKey - The key of the function within the object.
|
|
298
|
+
* @typeParam TArgs - The types of the arguments the function accepts.
|
|
299
|
+
* @typeParam TReturn - The return type of the function.
|
|
300
|
+
* @returns An ongoing function stub builder for defining the stub behavior.
|
|
301
|
+
* @example
|
|
302
|
+
* ```ts
|
|
303
|
+
* stub(myStubbedObject).someMethod = when(
|
|
304
|
+
* arg1 => arg1 > 0,
|
|
305
|
+
* arg2 => arg2 === "test"
|
|
306
|
+
* )
|
|
307
|
+
* .then(returnValue(42))
|
|
308
|
+
* .when(
|
|
309
|
+
* arg1 => arg1 <= 0,
|
|
310
|
+
* arg2 => arg2 === "test"
|
|
311
|
+
* )
|
|
312
|
+
* .then(throwError(new Error("arg1 must be positive")));
|
|
313
|
+
* ```
|
|
314
|
+
* @param argumentPredicates - The predicates to match the function arguments.
|
|
315
|
+
*/
|
|
134
316
|
declare function when<const TObject extends object, const TKey extends keyof TObject, const TArgs extends Array<unknown>, const TReturn>(...argumentPredicates: ArgumentPredicate<TArgs>): OngoingFunctionStubBuilder<TObject, TKey, TArgs, TReturn>;
|
|
135
317
|
|
|
318
|
+
/**
|
|
319
|
+
* Creates a property stub that returns a default value.
|
|
320
|
+
*
|
|
321
|
+
* The property stub will track reads and assignments to the property.
|
|
322
|
+
* @typeParam TObject - The type of the object containing the property.
|
|
323
|
+
* @typeParam TKey - The key of the property within the object.
|
|
324
|
+
* @returns A property stub that returns the specified default value and tracks reads and assignments.
|
|
325
|
+
* @example
|
|
326
|
+
* ```ts
|
|
327
|
+
* stub(myStubbedObject).someProperty = defaultValue(10);
|
|
328
|
+
* ```
|
|
329
|
+
* @param value - The default value to be returned by the property stub.
|
|
330
|
+
*/
|
|
136
331
|
declare function defaultValue<const TObject extends object, const TKey extends keyof TObject>(value: TObject[TKey]): PropertyStub<TObject, TKey>;
|
|
137
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Creates a property stub that always returns a fixed value.
|
|
335
|
+
*
|
|
336
|
+
* The property stub will track reads and assignments to the property.
|
|
337
|
+
* Attempts to set a new value will result in an error.
|
|
338
|
+
* @typeParam TThis - The type of the object containing the property.
|
|
339
|
+
* @typeParam TKey - The key of the property within the object.
|
|
340
|
+
* @returns A property stub that always returns the specified fixed value and tracks reads and assignments.
|
|
341
|
+
* @example
|
|
342
|
+
* ```ts
|
|
343
|
+
* stub(myStubbedObject).someProperty = fixedValue(10);
|
|
344
|
+
* ```
|
|
345
|
+
* @param value - The fixed value to be returned by the property stub.
|
|
346
|
+
*/
|
|
138
347
|
declare function fixedValue<const TThis extends object, const TKey extends keyof TThis>(value: TThis[TKey]): PropertyStub<TThis, TKey>;
|
|
139
348
|
|
|
349
|
+
/**
|
|
350
|
+
* Creates a property stub that tracks reads and assignments to the property.
|
|
351
|
+
* The property stub will return the current value of the property.
|
|
352
|
+
* @typeParam TObject - The type of the object containing the property.
|
|
353
|
+
* @typeParam TKey - The key of the property within the object.
|
|
354
|
+
* @returns A property stub that tracks reads and assignments to the property.
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* stub(myStubbedObject).someProperty = trackValue();
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
140
360
|
declare function trackValue<const TObject extends object, const TKey extends keyof TObject>(): PropertyStub<TObject, TKey>;
|
|
141
361
|
|
|
142
|
-
|
|
362
|
+
/**
|
|
363
|
+
* Abstract base class for verifying interactions.
|
|
364
|
+
* @typeParam TRecord The type of interaction record.
|
|
365
|
+
*/
|
|
366
|
+
declare abstract class InteractionVerification<TRecord extends InteractionRecord> {
|
|
367
|
+
protected readonly interactionRecord: TRecord;
|
|
368
|
+
protected constructor(record: TRecord);
|
|
369
|
+
/**
|
|
370
|
+
* Checks if this interaction occurred before another interaction.
|
|
371
|
+
* @param otherInteractionVerification - The other interaction verification to compare against.
|
|
372
|
+
*/
|
|
373
|
+
wasBefore(otherInteractionVerification: InteractionVerification<InteractionRecord>): void;
|
|
374
|
+
/**
|
|
375
|
+
* Checks if this interaction occurred before a specific point in time.
|
|
376
|
+
* @param date - The point in time to compare against.
|
|
377
|
+
*/
|
|
378
|
+
wasBefore(date: Date): void;
|
|
379
|
+
/**
|
|
380
|
+
* Checks if this interaction occurred after another interaction.
|
|
381
|
+
* @param otherInteractionVerification - The other interaction verification to compare against.
|
|
382
|
+
*/
|
|
383
|
+
wasAfter(otherInteractionVerification: InteractionVerification<InteractionRecord>): void;
|
|
384
|
+
/**
|
|
385
|
+
* Checks if this interaction occurred after a specific point in time.
|
|
386
|
+
* @param date - The point in time to compare against.
|
|
387
|
+
*/
|
|
388
|
+
wasAfter(date: Date): void;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
declare class InvocationVerification<const TArgs extends Array<unknown>, TReturn> extends InteractionVerification<InvocationRecord<TArgs, TReturn>> {
|
|
392
|
+
constructor(call: InvocationRecord<TArgs, TReturn>);
|
|
393
|
+
/**
|
|
394
|
+
* Checks the returned value against a provided validator function.
|
|
395
|
+
* @param validator - A function that validates the returned value.
|
|
396
|
+
*/
|
|
397
|
+
returned(validator: (result: TReturn) => boolean): void;
|
|
398
|
+
/**
|
|
399
|
+
* Checks the thrown exception against a provided validator function.
|
|
400
|
+
* @param validator - A function that validates the thrown exception.
|
|
401
|
+
*/
|
|
402
|
+
threw(validator: (error: unknown) => boolean): void;
|
|
403
|
+
/**
|
|
404
|
+
* Checks that the invocation did not throw an exception.
|
|
405
|
+
*/
|
|
406
|
+
didNotThrow(): void;
|
|
407
|
+
/**
|
|
408
|
+
* Checks the invocation arguments against provided validators.
|
|
409
|
+
* @param validators - An array of functions that validate each argument.
|
|
410
|
+
*/
|
|
411
|
+
hadArguments(...validators: ArgumentPredicate<TArgs>): void;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Provides verification methods for function invocations.
|
|
416
|
+
* @typeParam TArgs - The types of the function arguments.
|
|
417
|
+
* @typeParam TReturn - The return type of the function.
|
|
418
|
+
*/
|
|
419
|
+
declare class FunctionVerification<const TArgs extends Array<unknown>, const TReturn> {
|
|
143
420
|
#private;
|
|
421
|
+
constructor(functionStubMetadata: FunctionStubMetadata<TArgs, TReturn>);
|
|
422
|
+
/**
|
|
423
|
+
* Checks if the function was called and verifies the invocation count against a provided predicate.
|
|
424
|
+
* @param invocationCountPredicate
|
|
425
|
+
*/
|
|
426
|
+
wasCalled(invocationCountPredicate?: (n: number) => boolean): void;
|
|
427
|
+
/**
|
|
428
|
+
* Checks if the function was never called.
|
|
429
|
+
*/
|
|
430
|
+
wasNotCalled(): void;
|
|
431
|
+
/**
|
|
432
|
+
* Checks the number of times the function was called against an expected count.
|
|
433
|
+
* @param expectedCallCount - The expected number of times the function should have been called.
|
|
434
|
+
*/
|
|
435
|
+
wasCalledTimes(expectedCallCount: number): void;
|
|
436
|
+
/**
|
|
437
|
+
* Retrieves the verification for the nth invocation of the function.
|
|
438
|
+
* @param invocationIndex
|
|
439
|
+
*/
|
|
440
|
+
nthInvocation(invocationIndex: number): InvocationVerification<TArgs, TReturn>;
|
|
441
|
+
/**
|
|
442
|
+
* Retrieves the verification for the first invocation of the function.
|
|
443
|
+
*/
|
|
444
|
+
firstInvocation(): InvocationVerification<TArgs, TReturn>;
|
|
445
|
+
/**
|
|
446
|
+
* Retrieves the verification for the last invocation of the function.
|
|
447
|
+
*/
|
|
448
|
+
lastInvocation(): InvocationVerification<TArgs, TReturn>;
|
|
449
|
+
/**
|
|
450
|
+
* Finds the first invocation that matches the provided predicate.
|
|
451
|
+
* @param predicate - A function that tests the arguments of each invocation.
|
|
452
|
+
* @deprecated Use `firstInvocationMatching` instead. This is only kept for backward compatibility.
|
|
453
|
+
*/
|
|
454
|
+
invocationMatching(predicate: (...args: TArgs) => boolean): InvocationVerification<TArgs, TReturn>;
|
|
455
|
+
/**
|
|
456
|
+
* Finds the first invocation that matches the provided predicate.
|
|
457
|
+
* @param predicate - A function that tests the recorded information of each invocation
|
|
458
|
+
*/
|
|
459
|
+
firstInvocationMatching(predicate: (invocationRecord: InvocationRecord<TArgs, TReturn>) => boolean): InvocationVerification<TArgs, TReturn>;
|
|
460
|
+
/**
|
|
461
|
+
* Finds the last invocation that matches the provided predicate.
|
|
462
|
+
* @param predicate - A function that tests the recorded information of each invocation
|
|
463
|
+
*/
|
|
464
|
+
lastInvocationMatching(predicate: (invocationRecord: InvocationRecord<TArgs, TReturn>) => boolean): InvocationVerification<TArgs, TReturn>;
|
|
465
|
+
/**
|
|
466
|
+
* Finds all invocations that match the provided predicate.
|
|
467
|
+
* @param predicate - A function that tests the recorded information of each invocation
|
|
468
|
+
*/
|
|
469
|
+
allInvocationsMatching(predicate: (invocationRecord: InvocationRecord<TArgs, TReturn>) => boolean): Array<InvocationVerification<TArgs, TReturn>>;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Provides verification methods for assignment interactions.
|
|
474
|
+
* @typeParam TValue The type of the value being assigned.
|
|
475
|
+
*/
|
|
476
|
+
declare class AssignmentVerification<const TValue> extends InteractionVerification<AssignmentRecord<TValue>> {
|
|
144
477
|
constructor(assignmentRecord: AssignmentRecord<TValue>);
|
|
145
|
-
|
|
146
|
-
|
|
478
|
+
/**
|
|
479
|
+
* Checks if the assigned value satisfies the provided validator function.
|
|
480
|
+
* @param validator - A function that validates the assigned value.
|
|
481
|
+
*/
|
|
147
482
|
hasValue(validator: (result: TValue) => boolean): void;
|
|
148
483
|
}
|
|
149
484
|
|
|
150
|
-
|
|
151
|
-
|
|
485
|
+
/**
|
|
486
|
+
* Verification class for read interactions.
|
|
487
|
+
* @typeParam TValue The type of the value being read.
|
|
488
|
+
*/
|
|
489
|
+
declare class ReadVerification<const TValue> extends InteractionVerification<ReadRecord<TValue>> {
|
|
152
490
|
constructor(read: ReadRecord<TValue>);
|
|
153
|
-
|
|
154
|
-
|
|
491
|
+
/**
|
|
492
|
+
* Checks the read value against a provided validator function.
|
|
493
|
+
* @param validator
|
|
494
|
+
*/
|
|
155
495
|
hadValue(validator: (result: TValue) => boolean): void;
|
|
156
496
|
}
|
|
157
497
|
|
|
498
|
+
/**
|
|
499
|
+
* Provides verification methods for a spied or mocked property's interactions.
|
|
500
|
+
*/
|
|
158
501
|
declare class PropertyVerification<const TValue> {
|
|
159
502
|
#private;
|
|
160
503
|
constructor(propertyStubMetadata: PropertyStubMetadata<TValue>);
|
|
504
|
+
/**
|
|
505
|
+
* Checks the number of occurred reads against a fixed number.
|
|
506
|
+
* @param expectedGetCallCount How many reads are expected to have occurred
|
|
507
|
+
*/
|
|
161
508
|
wasReadNTimes(expectedGetCallCount?: number): void;
|
|
162
|
-
|
|
509
|
+
/**
|
|
510
|
+
* Checks the number of occurred reads against a predicate function.
|
|
511
|
+
* @param readCountPredicate A predicate function that checks if the number of occurred reads matches the expectation
|
|
512
|
+
*/
|
|
513
|
+
wasRead(readCountPredicate?: (n: number) => boolean): void;
|
|
514
|
+
/**
|
|
515
|
+
* Checks the number of occurred assignments against a predicate function.
|
|
516
|
+
* @param writeCountPredicate A predicate function that checks if the number of occurred assignments matches the expectation
|
|
517
|
+
*/
|
|
518
|
+
wasAssigned(writeCountPredicate?: (n: number) => boolean): void;
|
|
519
|
+
/**
|
|
520
|
+
* Checks if the property has never been assigned.
|
|
521
|
+
*/
|
|
163
522
|
wasNotAssigned(): void;
|
|
523
|
+
/**
|
|
524
|
+
* Checks if the property has never been read.
|
|
525
|
+
*/
|
|
164
526
|
wasNotRead(): void;
|
|
527
|
+
/**
|
|
528
|
+
* Checks the number of occurred assignments against a fixed number.
|
|
529
|
+
* @param expectedSetCallCount How many assignments are expected to have occurred
|
|
530
|
+
*/
|
|
165
531
|
wasAssignedNTimes(expectedSetCallCount?: number): void;
|
|
166
|
-
|
|
532
|
+
/**
|
|
533
|
+
* Gets the nth assignment verification.
|
|
534
|
+
* @param index The zero-based index of the assignment to retrieve
|
|
535
|
+
*/
|
|
167
536
|
nthAssignment(index: number): AssignmentVerification<TValue>;
|
|
537
|
+
/**
|
|
538
|
+
* Gets the first assignment verification.
|
|
539
|
+
*/
|
|
168
540
|
firstAssignment(): AssignmentVerification<TValue>;
|
|
541
|
+
/**
|
|
542
|
+
* Gets the last assignment verification.
|
|
543
|
+
*/
|
|
169
544
|
lastAssignment(): AssignmentVerification<TValue>;
|
|
545
|
+
/**
|
|
546
|
+
* Gets the first read verification.
|
|
547
|
+
*/
|
|
170
548
|
firstRead(): ReadVerification<TValue>;
|
|
549
|
+
/**
|
|
550
|
+
* Gets the last read verification.
|
|
551
|
+
*/
|
|
171
552
|
lastRead(): ReadVerification<TValue>;
|
|
553
|
+
/**
|
|
554
|
+
* Gets the nth read verification.
|
|
555
|
+
* @param index The zero-based index of the read to retrieve
|
|
556
|
+
*/
|
|
172
557
|
nthRead(index: number): ReadVerification<TValue>;
|
|
173
558
|
}
|
|
174
559
|
|
|
175
|
-
|
|
176
|
-
#private;
|
|
177
|
-
constructor(call: InvocationRecord<TArgs, TReturn>);
|
|
178
|
-
wasBefore(otherInvocation: InvocationVerification<TArgs, TReturn>): void;
|
|
179
|
-
wasAfter(otherInvocation: InvocationVerification<TArgs, TReturn>): void;
|
|
180
|
-
returned(validator: (result: TReturn) => boolean): void;
|
|
181
|
-
threw(validator: (error: unknown) => boolean): void;
|
|
182
|
-
didNotThrow(): void;
|
|
183
|
-
hadArguments(...validators: ArgumentPredicate<TArgs>): void;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
declare class FunctionVerification<const TArgs extends Array<unknown>, TReturn> {
|
|
187
|
-
#private;
|
|
188
|
-
constructor(functionStubMetadata: FunctionStubMetadata<TArgs, TReturn>);
|
|
189
|
-
wasCalled(): void;
|
|
190
|
-
wasNotCalled(): void;
|
|
191
|
-
wasCalledTimes(expectedCallCount: number): void;
|
|
192
|
-
nthInvocation(invocationIndex: number): InvocationVerification<TArgs, TReturn>;
|
|
193
|
-
firstInvocation(): InvocationVerification<TArgs, TReturn>;
|
|
194
|
-
lastInvocation(): InvocationVerification<TArgs, TReturn>;
|
|
195
|
-
invocationMatching(predicate: (...args: TArgs) => boolean): InvocationVerification<TArgs, TReturn>;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
type Verifiable<T> = {
|
|
560
|
+
type Verification<T> = {
|
|
199
561
|
readonly [TKey in keyof T]: T[TKey] extends (...args: infer TArgs) => infer TReturn ? FunctionVerification<TArgs, TReturn> : PropertyVerification<T[TKey]>;
|
|
200
562
|
};
|
|
201
563
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
564
|
+
/**
|
|
565
|
+
* starts a stub verification. The Verification API always follows the pattern `verify(<mockOrSpy>).<member>.<verification>`.
|
|
566
|
+
* @param mockOrSpy
|
|
567
|
+
* @returns A proxy that provides access to the verification API for the specified mock or spy.
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* verify(myMock).someMethod.firstInvocation().didNotThrow();
|
|
571
|
+
* ```
|
|
572
|
+
*
|
|
573
|
+
* @see {@link FunctionVerification} for function call verifications.
|
|
574
|
+
* @see {@link PropertyVerification} for property interaction verifications.
|
|
575
|
+
* @see {@link spy} for creating spies on real instances.
|
|
576
|
+
* @see {@link mock} for creating mock instances of classes.
|
|
577
|
+
* @see {@link stub} for stubbing methods and properties on the mock.
|
|
578
|
+
*/
|
|
579
|
+
declare function verify<const TObject extends object>(mockOrSpy: Mock<TObject>): Verification<TObject>;
|
|
209
580
|
|
|
210
581
|
export { answer, callThrough, defaultValue, delegateTo, fixedValue, mock, noop, returnFixed, returnSerial, spy, stub, throwOnCall, trackValue, verify, when };
|
|
211
582
|
export type { Mock };
|