@remotex-labs/xjet 1.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/LICENSE +373 -0
- package/README.md +73 -0
- package/dist/bash.js +45 -0
- package/dist/bash.js.map +8 -0
- package/dist/index.d.ts +4790 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +8 -0
- package/dist/package.json +1 -0
- package/dist/shared.d.ts +4713 -0
- package/dist/shared.js +74 -0
- package/dist/shared.js.map +8 -0
- package/package.json +81 -0
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,4713 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This file was automatically generated by xBuild.
|
|
4
|
+
* DO NOT EDIT MANUALLY.
|
|
5
|
+
*/
|
|
6
|
+
import type { FunctionLikeType, RejectedValueType, ResolvedValueType } from '@remotex-labs/xjet-expect';
|
|
7
|
+
import type { ContextType, FunctionType } from '@remotex-labs/xjet-expect';
|
|
8
|
+
import { type ContextType, type FunctionLikeType, type FunctionType } from '@remotex-labs/xjet-expect';
|
|
9
|
+
import type { ContextType } from '@remotex-labs/xjet-expect';
|
|
10
|
+
import type { FunctionLikeType } from '@remotex-labs/xjet-expect';
|
|
11
|
+
import type { FunctionType } from '@remotex-labs/xjet-expect';
|
|
12
|
+
import type { Struct } from '@remotex-labs/xstruct';
|
|
13
|
+
import { Struct } from '@remotex-labs/xstruct';
|
|
14
|
+
import type { ConstructorLikeType, FunctionType } from '@remotex-labs/xjet-expect';
|
|
15
|
+
import type { ConstructorType } from '@remotex-labs/xjet-expect';
|
|
16
|
+
import type { ConstructorLikeType, FunctionLikeType } from '@remotex-labs/xjet-expect';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Imports
|
|
20
|
+
*/
|
|
21
|
+
export {};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Import will remove at compile time
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* A class representing the mock state for tracking and managing the behavior of mocked functions or classes.
|
|
28
|
+
*
|
|
29
|
+
* @template ReturnType - The type of value returned by the mock function.
|
|
30
|
+
* @template Context - The type representing the context (`this` value) used in the mock function.
|
|
31
|
+
* @template Args - The types of arguments for the mocked function.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* This class provides mechanisms to customize and manage the behavior of mocked functions or constructors.
|
|
35
|
+
* It tracks invocation details, allows for behavior customization, and enables resetting or restoring the mock to its original state.
|
|
36
|
+
*
|
|
37
|
+
* @since v1.0.0
|
|
38
|
+
*/
|
|
39
|
+
declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unknown[], Context = unknown> extends Function {
|
|
40
|
+
/**
|
|
41
|
+
* List of all mocks that created
|
|
42
|
+
*/
|
|
43
|
+
static mocks: Array<MockState>;
|
|
44
|
+
/**
|
|
45
|
+
* The `name` property represents the name of the mock function.
|
|
46
|
+
*/
|
|
47
|
+
name: string;
|
|
48
|
+
/**
|
|
49
|
+
* Flag to detect mock functions
|
|
50
|
+
*/
|
|
51
|
+
readonly isMock: boolean;
|
|
52
|
+
readonly xJetMock: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The `state` property holds the detailed state of the mock invocations.
|
|
55
|
+
* It tracks information such as the arguments passed, the results returned, the context (`this` value) used,
|
|
56
|
+
* the instances created, and the order of invocations.
|
|
57
|
+
* This data is automatically updated after each call to the mock.
|
|
58
|
+
*
|
|
59
|
+
* @template ReturnType - The type of value returned by the mock function.
|
|
60
|
+
* @template Context - The type representing the context (`this` value) used in the mock function.
|
|
61
|
+
* @template Args - The types of arguments for the mocked function.
|
|
62
|
+
*
|
|
63
|
+
* @see MocksStateInterface
|
|
64
|
+
*
|
|
65
|
+
* @since v1.0.0
|
|
66
|
+
*/
|
|
67
|
+
private state;
|
|
68
|
+
/**
|
|
69
|
+
* A private function that restores the mocks original implementation.
|
|
70
|
+
* It resets the mock to its initial state, typically used when restoring
|
|
71
|
+
* the mock after a call to `mockReset` or `mockRestore`.
|
|
72
|
+
*
|
|
73
|
+
* This function is invoked internally to ensure that the mock behaves as
|
|
74
|
+
* originally defined before any changes were made to its behavior or implementation.
|
|
75
|
+
*
|
|
76
|
+
* @since v1.0.0
|
|
77
|
+
*/
|
|
78
|
+
private readonly restore;
|
|
79
|
+
/**
|
|
80
|
+
* A private array that stores the implementations queued to be executed
|
|
81
|
+
* for future invocations of the mock.
|
|
82
|
+
* Each function in this array is invoked in sequence when the mock function is called,
|
|
83
|
+
* with the first queued implementation being used on the first call, the second on the second call, and so on.
|
|
84
|
+
*
|
|
85
|
+
* This property is used in conjunction with methods like `mockImplementationOnce`
|
|
86
|
+
* to specify different behaviors for consecutive calls to the mock.
|
|
87
|
+
*
|
|
88
|
+
* @since v1.0.0
|
|
89
|
+
*/
|
|
90
|
+
private queuedImplementations;
|
|
91
|
+
/**
|
|
92
|
+
* A private property that holds the current implementation of the mock function.
|
|
93
|
+
* This function is executed whenever the mock is invoked, and can be changed
|
|
94
|
+
* using methods like `mockImplementation` or `mockImplementationOnce`.
|
|
95
|
+
*
|
|
96
|
+
* The `implementation` allows for customizing the behavior of the mock,
|
|
97
|
+
* such as returning specific values, throwing errors, or performing actions.
|
|
98
|
+
*
|
|
99
|
+
* @since v1.0.0
|
|
100
|
+
*/
|
|
101
|
+
private implementation;
|
|
102
|
+
/**
|
|
103
|
+
* A private property that holds the original implementation of the mock function.
|
|
104
|
+
* This is the initial function passed to the constructor that defines the mock's default behavior
|
|
105
|
+
* before any customization.
|
|
106
|
+
*
|
|
107
|
+
* @template ReturnType - The type of value returned by the original function.
|
|
108
|
+
* @template Args - The types of arguments for the original function.
|
|
109
|
+
* @template Context - The type representing the context (`this` value) used in the original function.
|
|
110
|
+
*
|
|
111
|
+
* @remarks
|
|
112
|
+
* This property stores the initial implementation provided when creating the mock.
|
|
113
|
+
* If no implementation is provided during construction, it defaults to a function
|
|
114
|
+
* that returns `undefined` cast to the appropriate return type.
|
|
115
|
+
* Unlike `implementation` which can be modified, this property maintains a reference
|
|
116
|
+
* to the original function for scenarios where the original behavior needs to be preserved
|
|
117
|
+
* or restored.
|
|
118
|
+
*
|
|
119
|
+
* @see FunctionLikeType
|
|
120
|
+
* @see MockState.original
|
|
121
|
+
*
|
|
122
|
+
* @since 1.0.0
|
|
123
|
+
*/
|
|
124
|
+
private readonly originalImplementation;
|
|
125
|
+
/**
|
|
126
|
+
* Constructs a mock object that allows custom implementation, restore capability,
|
|
127
|
+
* and optional naming functionality. This mock is proxied to handle function invocation
|
|
128
|
+
* and class instantiation.
|
|
129
|
+
*
|
|
130
|
+
* @template ReturnType - The type of the value returned by the mock function.
|
|
131
|
+
* @template Context - The type of the context (`this`) for the mock function.
|
|
132
|
+
* @template Args - The type of arguments accepted by the mock function.
|
|
133
|
+
*
|
|
134
|
+
* @param implementation - Optional implementation for the mock function.
|
|
135
|
+
* @param restore - Optional function to restore the mock to its initial state. Defaults to resetting to the provided implementation.
|
|
136
|
+
* @param name - Optional name for the mock instance. Default to a predefined mock name if not provided.
|
|
137
|
+
*
|
|
138
|
+
* @returns A proxied mock object capable of behaving as a callable function or a constructible class.
|
|
139
|
+
*
|
|
140
|
+
* @remarks
|
|
141
|
+
* The mock object can work as both a function and a class, using JavaScript's Proxy API. The restore functionality
|
|
142
|
+
* allows resetting to the original state or implementation. If no implementation is provided, the mock remains uninitialized
|
|
143
|
+
* but still functional.
|
|
144
|
+
*
|
|
145
|
+
* @see FunctionLikeType
|
|
146
|
+
* @see VoidFunctionType
|
|
147
|
+
*
|
|
148
|
+
* @since 1.0.0
|
|
149
|
+
*/
|
|
150
|
+
constructor(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: FunctionLikeType<void>, name?: string);
|
|
151
|
+
/**
|
|
152
|
+
* todo remove it
|
|
153
|
+
* only for jest expect will support this mock
|
|
154
|
+
*/
|
|
155
|
+
getMockName(): string;
|
|
156
|
+
/**
|
|
157
|
+
* Retrieves the current state of mocks.
|
|
158
|
+
*
|
|
159
|
+
* @return The current state of the mocks.
|
|
160
|
+
* @see MocksStateInterface
|
|
161
|
+
*
|
|
162
|
+
* @since 1.0.0
|
|
163
|
+
*/
|
|
164
|
+
get mock(): Readonly<MocksStateInterface<ReturnType, Args, Context>>;
|
|
165
|
+
/**
|
|
166
|
+
* Returns the original implementation of the function that was instrumented
|
|
167
|
+
*
|
|
168
|
+
* @returns The original function implementation that was wrapped or modified
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* This getter provides access to the unmodified function that was originally passed
|
|
172
|
+
* to the instrumentation system. Useful for debugging, testing, or when you need
|
|
173
|
+
* to bypass the instrumented behavior temporarily.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* const mockFn = jest.fn(x => x * 2);
|
|
178
|
+
* // Later in test
|
|
179
|
+
* const originalImplementation = mockFn.original;
|
|
180
|
+
* expect(originalImplementation(5)).toBe(10);
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* @see FunctionLikeType
|
|
184
|
+
*
|
|
185
|
+
* @since 1.0.0
|
|
186
|
+
*/
|
|
187
|
+
get original(): FunctionLikeType<ReturnType, Args, Context>;
|
|
188
|
+
/**
|
|
189
|
+
* Clears the `mock.calls`, `mock.results`, `mock.contexts`, `mock.instances`, and `mock.invocationCallOrder` properties.
|
|
190
|
+
*
|
|
191
|
+
* @returns The current instance of the mock, allowing for method chaining.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* This method resets the state of the mock function, clearing all invocation data and results,
|
|
195
|
+
* ensuring that previous mock states do not affect the following tests.
|
|
196
|
+
* Equivalent to calling `.mockClear()` on every mocked function.
|
|
197
|
+
*
|
|
198
|
+
* @see MockState.initState
|
|
199
|
+
*
|
|
200
|
+
* @since v1.0.0
|
|
201
|
+
*/
|
|
202
|
+
mockClear(): this;
|
|
203
|
+
/**
|
|
204
|
+
* Resets the mock function to its initial state.
|
|
205
|
+
*
|
|
206
|
+
* @returns The current instance of the mock, allowing for method chaining.
|
|
207
|
+
*
|
|
208
|
+
* @remarks
|
|
209
|
+
* The `mockReset` method clears all invocation data and results by calling `mockClear()`, and also resets
|
|
210
|
+
* the queued implementations,
|
|
211
|
+
* removing any previously queued behavior set by methods like `mockImplementationOnce`.
|
|
212
|
+
* This ensures that the mock is in a clean state and ready for new invocations or configurations.
|
|
213
|
+
*
|
|
214
|
+
* @see MockState.mockClear
|
|
215
|
+
*
|
|
216
|
+
* @since v1.0.0
|
|
217
|
+
*/
|
|
218
|
+
mockReset(): this;
|
|
219
|
+
/**
|
|
220
|
+
* Restores the mock function to its original implementation and resets its state.
|
|
221
|
+
*
|
|
222
|
+
* @returns The current instance of the mock, allowing for method chaining.
|
|
223
|
+
*
|
|
224
|
+
* @remarks
|
|
225
|
+
* The `mockRestore` method does two things:
|
|
226
|
+
* 1. It restores the mock to its initial implementation, which was set during the mocks creation or
|
|
227
|
+
* via the `mockImplementation` method.
|
|
228
|
+
* 2. It clears all tracking data, such as calls, results, contexts, instances, and invocation call order
|
|
229
|
+
* by calling `mockReset()`, ensuring the mock is fully reset and ready for new invocations.
|
|
230
|
+
*
|
|
231
|
+
* This method is useful for ensuring that the mock is completely restored and cleared, making it behave as it did
|
|
232
|
+
* when it was first created or last restored.
|
|
233
|
+
*
|
|
234
|
+
* @see MockState.restore
|
|
235
|
+
* @see MockState.mockReset
|
|
236
|
+
*
|
|
237
|
+
* @since v1.0.0
|
|
238
|
+
*/
|
|
239
|
+
mockRestore(): this;
|
|
240
|
+
/**
|
|
241
|
+
* Retrieves the mock implementation for a function, if available.
|
|
242
|
+
*
|
|
243
|
+
* @template ReturnType The type of the return value of the function.
|
|
244
|
+
* @template Context The type of the `this` context for the function.
|
|
245
|
+
* @template Args The type of the argument(s) of the function.
|
|
246
|
+
*
|
|
247
|
+
* @return A function matching `FunctionLikeType` that represents the mock implementation,
|
|
248
|
+
* or `undefined` if no implementation is set.
|
|
249
|
+
*
|
|
250
|
+
* @remarks
|
|
251
|
+
* This method returns the mock implementation associated with the instance.
|
|
252
|
+
* If no mock implementation exists, it returns `undefined`.
|
|
253
|
+
*
|
|
254
|
+
* @since 1.0.0
|
|
255
|
+
*/
|
|
256
|
+
getMockImplementation(): FunctionLikeType<ReturnType, Args, Context> | undefined;
|
|
257
|
+
/**
|
|
258
|
+
* Retrieves the next implementation from the queued implementations or defaults to the current implementation.
|
|
259
|
+
*
|
|
260
|
+
* @template ReturnType The return type of the function-like implementation.
|
|
261
|
+
* @template Context The context in which the implementation executes.
|
|
262
|
+
* @template Args The argument types expected by the implementation.
|
|
263
|
+
*
|
|
264
|
+
* @return The next implementation from the queue if available, or the current implementation.
|
|
265
|
+
* Returns are `undefined` if no implementation is found.
|
|
266
|
+
*
|
|
267
|
+
* @remarks
|
|
268
|
+
* This method first checks if there are any queued implementations available.
|
|
269
|
+
* If a queued implementation exists, it will be removed from the queue and returned.
|
|
270
|
+
* If the queue is empty, the primary current implementation is returned.
|
|
271
|
+
* Returns are `undefined` if there is no implementation available.
|
|
272
|
+
*
|
|
273
|
+
* @since 1.0.0
|
|
274
|
+
*/
|
|
275
|
+
getNextImplementation(): FunctionLikeType<ReturnType, Args, Context> | undefined;
|
|
276
|
+
/**
|
|
277
|
+
* Replaces the default implementation of a mock function with the provided function.
|
|
278
|
+
*
|
|
279
|
+
* @template ReturnType - The type of the value returned by the implementation function.
|
|
280
|
+
* @template Context - The context (`this`) expected by the implementation function.
|
|
281
|
+
* @template Args - The types of the arguments expected by the implementation function.
|
|
282
|
+
*
|
|
283
|
+
* @param fn - The function to be used as the mock implementation. It defines
|
|
284
|
+
* the behavior of the mock when called.
|
|
285
|
+
*
|
|
286
|
+
* @return Returns the instance of the current object for method chaining.
|
|
287
|
+
*
|
|
288
|
+
* @remarks
|
|
289
|
+
* This method is useful when you need to mock the behavior of a function
|
|
290
|
+
* dynamically during tests or in controlled scenarios.
|
|
291
|
+
*
|
|
292
|
+
* @since 1.0.0
|
|
293
|
+
*/
|
|
294
|
+
mockImplementation(fn: FunctionLikeType<ReturnType, Args, Context>): this;
|
|
295
|
+
/**
|
|
296
|
+
* Sets a mock implementation that will be used once for the next call to the mocked function.
|
|
297
|
+
*
|
|
298
|
+
* @template ReturnType The type of the value that the mock function will return.
|
|
299
|
+
* @template Context The type of the `this` context for the mock function.
|
|
300
|
+
* @template Args The type of arguments that the mock function will receive.
|
|
301
|
+
*
|
|
302
|
+
* @param fn - The function to be used as the mock implementation for the next call.
|
|
303
|
+
* @returns The current instance, allowing for chaining of mock configurations.
|
|
304
|
+
*
|
|
305
|
+
* @remarks
|
|
306
|
+
* The provided mock implementation will only be executed once. Further calls will fall back
|
|
307
|
+
* to a different implementation, if provided, or the default behavior of the mock function.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* const mockFn = new MockState();
|
|
312
|
+
*
|
|
313
|
+
* // Set default implementation
|
|
314
|
+
* mockFn.mockImplementation(() => 'default');
|
|
315
|
+
*
|
|
316
|
+
* // Set one-time behavior for the next call
|
|
317
|
+
* mockFn.mockImplementationOnce(() => 'first call');
|
|
318
|
+
*
|
|
319
|
+
* console.log(mockFn()); // Output: 'first call' (from mockImplementationOnce)
|
|
320
|
+
* console.log(mockFn()); // Output: 'default' (from mockImplementation)
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
323
|
+
* @see FunctionLikeType
|
|
324
|
+
*
|
|
325
|
+
* @since 1.0.0
|
|
326
|
+
*/
|
|
327
|
+
mockImplementationOnce(fn: FunctionLikeType<ReturnType, Args, Context>): this;
|
|
328
|
+
/**
|
|
329
|
+
* Sets a mock implementation to always return a specified value when invoked.
|
|
330
|
+
*
|
|
331
|
+
* @template ReturnType The type of the value returned by the mock implementation.
|
|
332
|
+
*
|
|
333
|
+
* @param value - The value to always return when the mock function is called.
|
|
334
|
+
* @return The current instance for chaining.
|
|
335
|
+
*
|
|
336
|
+
* @remarks
|
|
337
|
+
* This method overrides any previous mock implementation configured for the function.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* const mockFn = new MockState();
|
|
342
|
+
*
|
|
343
|
+
* // Set mock to return 'Hello World' on each call
|
|
344
|
+
* mockFn.mockReturnValue('Hello World');
|
|
345
|
+
*
|
|
346
|
+
* console.log(mockFn()); // Output: 'Hello World'
|
|
347
|
+
* console.log(mockFn()); // Output: 'Hello World'
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
* @since 1.0.0
|
|
351
|
+
*/
|
|
352
|
+
mockReturnValue(value: ReturnType): this;
|
|
353
|
+
/**
|
|
354
|
+
* Sets up a mock function to always resolve a promise with the specified value when called.
|
|
355
|
+
*
|
|
356
|
+
* @template ResolvedValueType - The type of the value to be resolved by the promise.
|
|
357
|
+
* @template ReturnType - The return type of the function, which should include a Promise of the specified resolved value type.
|
|
358
|
+
*
|
|
359
|
+
* @param value - The value to be returned as the resolved value of the promise.
|
|
360
|
+
* @returns The mock function instance, enabling method chaining.
|
|
361
|
+
*
|
|
362
|
+
* @remarks
|
|
363
|
+
* This method is particularly useful for mocking asynchronous functions that return promises.
|
|
364
|
+
* It ensures that the mock function resolves with the provided value every time it is called.
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* const mockFn = new MockState<Promise<string>>();
|
|
369
|
+
*
|
|
370
|
+
* // Set mock to return a resolved promise with the value 'Success'
|
|
371
|
+
* mockFn.mockResolvedValue('Success');
|
|
372
|
+
*
|
|
373
|
+
* mockFn().then((result: string) => {
|
|
374
|
+
* console.log(result); // Output: 'Success'
|
|
375
|
+
* });
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
378
|
+
* @since 1.0.0
|
|
379
|
+
*/
|
|
380
|
+
mockResolvedValue(value: ResolvedValueType<ReturnType>): this;
|
|
381
|
+
/**
|
|
382
|
+
* Sets a mock implementation for a single call that resolves to the specified value.
|
|
383
|
+
*
|
|
384
|
+
* @template ReturnType The type of the resolved value.
|
|
385
|
+
* @template ResolvedValueType The type of the input value to be resolved.
|
|
386
|
+
*
|
|
387
|
+
* @param value - The value that the promise should resolve with when the mock is called once.
|
|
388
|
+
* @return The current mock object instance, enabling method chaining.
|
|
389
|
+
*
|
|
390
|
+
* @remarks
|
|
391
|
+
* This method is useful for defining custom behavior for a specific invocation of a mocked function,
|
|
392
|
+
* returning a resolved promise with the provided value.
|
|
393
|
+
*
|
|
394
|
+
* @example
|
|
395
|
+
* ```ts
|
|
396
|
+
* const mockFn = new MockState(async () => {
|
|
397
|
+
* return 'end';
|
|
398
|
+
* });
|
|
399
|
+
*
|
|
400
|
+
* // Set mock to return a resolved promise with the value 'Success'
|
|
401
|
+
* mockFn.mockResolvedValueOnce('Success');
|
|
402
|
+
*
|
|
403
|
+
* mockFn().then((result: string) => {
|
|
404
|
+
* console.log(result); // Output: 'Success'
|
|
405
|
+
* });
|
|
406
|
+
*
|
|
407
|
+
* mockFn().then((result: string) => {
|
|
408
|
+
* console.log(result); // Output: 'end'
|
|
409
|
+
* });
|
|
410
|
+
* ```
|
|
411
|
+
*
|
|
412
|
+
* @since 1.0.0
|
|
413
|
+
*/
|
|
414
|
+
mockResolvedValueOnce(value: ResolvedValueType<ReturnType>): this;
|
|
415
|
+
/**
|
|
416
|
+
* Sets the return value of the mock function for a single call.
|
|
417
|
+
*
|
|
418
|
+
* @template ReturnType The type of the value to be returned.
|
|
419
|
+
*
|
|
420
|
+
* @param value - The value to be returned by the mock function for the next call.
|
|
421
|
+
* @return The mock function instance, allowing for method chaining.
|
|
422
|
+
*
|
|
423
|
+
* @remarks
|
|
424
|
+
* This method only affects the return value for the next call to the mock function.
|
|
425
|
+
* All further calls will use the usual mock implementation or other specified behaviors.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* ```ts
|
|
429
|
+
* const mockFn = new MockState();
|
|
430
|
+
*
|
|
431
|
+
* // Set default return value
|
|
432
|
+
* mockFn.mockReturnValue('Default Value');
|
|
433
|
+
*
|
|
434
|
+
* // Set one-time return value for the next call
|
|
435
|
+
* mockFn.mockReturnValueOnce('First Call');
|
|
436
|
+
* mockFn.mockReturnValueOnce('Second Call');
|
|
437
|
+
*
|
|
438
|
+
* console.log(mockFn()); // Output: 'First Call' (from mockReturnValueOnce)
|
|
439
|
+
* console.log(mockFn()); // Output: 'Second Call' (from mockReturnValueOnce)
|
|
440
|
+
* console.log(mockFn()); // Output: 'Default Value' (from mockReturnValue)
|
|
441
|
+
* ```
|
|
442
|
+
*
|
|
443
|
+
* @since 1.0.0
|
|
444
|
+
*/
|
|
445
|
+
mockReturnValueOnce(value: ReturnType): this;
|
|
446
|
+
/**
|
|
447
|
+
* Mocks the method to always return a rejected Promise with the specified value.
|
|
448
|
+
*
|
|
449
|
+
* @template ReturnType - The expected type of the return value for the mocked method.
|
|
450
|
+
* @template RejectedValueType - The type of the value used to reject the Promise.
|
|
451
|
+
*
|
|
452
|
+
* @param value - The value with which the mocked Promise will be rejected.
|
|
453
|
+
*
|
|
454
|
+
* @return The current instance of the mock for chaining purposes.
|
|
455
|
+
*
|
|
456
|
+
* @remarks
|
|
457
|
+
* This method is useful for testing scenarios where the function being mocked
|
|
458
|
+
* is expected to reject with a specific value.
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```ts
|
|
462
|
+
* const mockFn = new MockState<Promise<string>>();
|
|
463
|
+
*
|
|
464
|
+
* // Set mock to return a rejected promise with the value 'Error'
|
|
465
|
+
* mockFn.mockRejectedValue('Error');
|
|
466
|
+
*
|
|
467
|
+
* mockFn().catch(error => {
|
|
468
|
+
* console.log(error); // Output: 'Error'
|
|
469
|
+
* });
|
|
470
|
+
* ```
|
|
471
|
+
*
|
|
472
|
+
* @since 1.0.0
|
|
473
|
+
*/
|
|
474
|
+
mockRejectedValue(value: RejectedValueType<ReturnType>): this;
|
|
475
|
+
/**
|
|
476
|
+
* Adds a one-time rejection with the provided value to the mock function.
|
|
477
|
+
*
|
|
478
|
+
* @template ReturnType - The type of the value the mock function would return.
|
|
479
|
+
*
|
|
480
|
+
* @param value - The value to reject the promise with in the mock function.
|
|
481
|
+
* @return The current instance of the mock function, allowing for method chaining.
|
|
482
|
+
*
|
|
483
|
+
* @remarks
|
|
484
|
+
* This method configures a mock function to return a rejected promise with the
|
|
485
|
+
* specified value the next time it is called. After the rejection occurs, the
|
|
486
|
+
* mock function's behavior will revert to the next defined mock behavior, or
|
|
487
|
+
* to the default behavior if no behaviors are defined.
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```ts
|
|
491
|
+
* const mockFn = new MockState<Promise<string>>();
|
|
492
|
+
*
|
|
493
|
+
* // Set default rejected value
|
|
494
|
+
* mockFn.mockRejectedValue('Default Error');
|
|
495
|
+
*
|
|
496
|
+
* // Set one-time rejected value for the next call
|
|
497
|
+
* mockFn.mockRejectedValueOnce('First Call Error');
|
|
498
|
+
*
|
|
499
|
+
* mockFn().catch(error => {
|
|
500
|
+
* console.log(error); // Output: 'First Call Error' (from mockRejectedValueOnce)
|
|
501
|
+
* });
|
|
502
|
+
*
|
|
503
|
+
* mockFn().catch(error => {
|
|
504
|
+
* console.log(error); // Output: 'Default Error' (from mockRejectedValue)
|
|
505
|
+
* });
|
|
506
|
+
* ```
|
|
507
|
+
*
|
|
508
|
+
* @since 1.0.0
|
|
509
|
+
*/
|
|
510
|
+
mockRejectedValueOnce(value: RejectedValueType<ReturnType>): this;
|
|
511
|
+
/**
|
|
512
|
+
* Initializes and returns the state object for mock function tracking.
|
|
513
|
+
*
|
|
514
|
+
* @return An object containing the initialized mock function state.
|
|
515
|
+
*
|
|
516
|
+
* @remarks
|
|
517
|
+
* The state object contains the structure necessary to keep track of
|
|
518
|
+
* calls, results, contexts, instances, and invocation orders of the function.
|
|
519
|
+
*
|
|
520
|
+
* @see MocksStateInterface
|
|
521
|
+
*
|
|
522
|
+
* @since v1.0.0
|
|
523
|
+
*/
|
|
524
|
+
private initState;
|
|
525
|
+
/**
|
|
526
|
+
* Invokes the next implementation of a mock function with the provided context and arguments.
|
|
527
|
+
*
|
|
528
|
+
* @template Context The type of `this` context in which the function is invoked.
|
|
529
|
+
* @template Args The type of the arguments passed to the function.
|
|
530
|
+
* @template ReturnType The type of the return value, of the function being invoked.
|
|
531
|
+
*
|
|
532
|
+
* @param thisArg - The context (`this`) in which the function is executed.
|
|
533
|
+
* @param args - The arguments to be passed to the function.
|
|
534
|
+
*
|
|
535
|
+
* @returns The result of the function invocation, which is either the return value, the thrown error, or undefined.
|
|
536
|
+
*
|
|
537
|
+
* @remarks
|
|
538
|
+
* This method simulates the function call for a mocked implementation. It tracks all invocations,
|
|
539
|
+
* including the call order, the provided arguments, and the context. It handles binding, default
|
|
540
|
+
* arguments, and maintains a record of results (either successful returns or thrown errors).
|
|
541
|
+
*
|
|
542
|
+
* @since 1.0.0
|
|
543
|
+
*/
|
|
544
|
+
private invoke;
|
|
545
|
+
/**
|
|
546
|
+
* Invokes a function within a specific context and with provided arguments.
|
|
547
|
+
*
|
|
548
|
+
* @template Context The type of the context in which the function is invoked.
|
|
549
|
+
* @template Args The type of the arguments passed to the function.
|
|
550
|
+
* @template ReturnType The type of the value that the invoked function returns.
|
|
551
|
+
*
|
|
552
|
+
* @param target - The instance of the class containing the method to be invoked.
|
|
553
|
+
* @param thisArg - The context object that will be bound to the invoked function.
|
|
554
|
+
* @param argumentsList - The list of arguments to pass to the invoked function.
|
|
555
|
+
* @returns The result of the invoked function or `undefined` if no value is returned.
|
|
556
|
+
*
|
|
557
|
+
* @remarks
|
|
558
|
+
* This method modifies the state of the `target` instance by adding the `thisArg`
|
|
559
|
+
* to the `target.state.instances` array before invoking the function.
|
|
560
|
+
*
|
|
561
|
+
* @since 1.0.0
|
|
562
|
+
*/
|
|
563
|
+
private invokeFunction;
|
|
564
|
+
/**
|
|
565
|
+
* Invokes a class method on the provided target with specified arguments and a new target.
|
|
566
|
+
*
|
|
567
|
+
* @template Args - The type of arguments to be passed to the invoked method.
|
|
568
|
+
*
|
|
569
|
+
* @param target - The object on which the class method is invoked.
|
|
570
|
+
* @param argArray - The array of arguments to pass to the invoked method.
|
|
571
|
+
* @param newTarget - The new object used as the invocation context.
|
|
572
|
+
* @returns The result of the invocation, typically an object. If the result is not an object,
|
|
573
|
+
* the `newTarget` is returned instead.
|
|
574
|
+
*
|
|
575
|
+
* @remarks
|
|
576
|
+
* This method ensures that the result is stored in the `instances` array of the target's state
|
|
577
|
+
* if it is detected as a proper class instance. Otherwise, the `newTarget` is registered.
|
|
578
|
+
*
|
|
579
|
+
* @since 1.0.0
|
|
580
|
+
*/
|
|
581
|
+
private invokeClass;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Represents an interface for defining bound context and arguments.
|
|
586
|
+
*
|
|
587
|
+
* @template Context - The type of the bound `this` context, which can be an object, null, or undefined.
|
|
588
|
+
* @template Args - The type of the array representing bound arguments.
|
|
589
|
+
*
|
|
590
|
+
* @remarks
|
|
591
|
+
* This interface is designed to store binding metadata, specifically a reference to the bound `this` context
|
|
592
|
+
* and any arguments that are pre-applied during function binding. It is often used in scenarios involving
|
|
593
|
+
* dynamic contexts or partial application of functions.
|
|
594
|
+
*
|
|
595
|
+
* @see ContextType
|
|
596
|
+
*
|
|
597
|
+
* @since 1.0.0
|
|
598
|
+
*/
|
|
599
|
+
interface BoundInterface<Context = unknown | null | undefined, Args = Array<unknown>> {
|
|
600
|
+
__boundThis?: Context;
|
|
601
|
+
__boundArgs?: Args;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Represents the possible result types of a mock function invocation.
|
|
606
|
+
*
|
|
607
|
+
* @template 'return' | 'throw' | 'incomplete'
|
|
608
|
+
*
|
|
609
|
+
* @since 1.0.0
|
|
610
|
+
*/
|
|
611
|
+
type MockInvocationResultType = 'return' | 'throw' | 'incomplete';
|
|
612
|
+
/**
|
|
613
|
+
* Represents the result of a mock function invocation, providing details regarding the outcome,
|
|
614
|
+
* such as whether the function returned a value, threw an error, or did not complete its execution.
|
|
615
|
+
*
|
|
616
|
+
* @template T - Specifies the expected return type of the mock function when the result is of type `'return'`.
|
|
617
|
+
*
|
|
618
|
+
* @remarks
|
|
619
|
+
* This interface is useful in mock testing frameworks to analyze the behavior of mocked functions
|
|
620
|
+
* and their respective invocation outcomes.
|
|
621
|
+
*
|
|
622
|
+
* @since 1.0.0
|
|
623
|
+
*/
|
|
624
|
+
interface MockInvocationResultInterface<T> {
|
|
625
|
+
/**
|
|
626
|
+
* Indicates the result type:
|
|
627
|
+
* - `'return'`: The mock function successfully returned a value.
|
|
628
|
+
* - `'throw'`: The mock function threw an error or exception.
|
|
629
|
+
* - `'incomplete'`: The mock function invocation has not been completed (rare case).
|
|
630
|
+
*
|
|
631
|
+
* @see MockInvocationResultType
|
|
632
|
+
*
|
|
633
|
+
* @since 1.0.0
|
|
634
|
+
*/
|
|
635
|
+
type: MockInvocationResultType;
|
|
636
|
+
/**
|
|
637
|
+
* The value associated with the invocation result:
|
|
638
|
+
* - If `type` is `'return'`, this is the mocks return value (`T`).
|
|
639
|
+
* - If `type` is `'throw'`, this is the thrown error (`unknown`).
|
|
640
|
+
* - If `type` is `'incomplete'`, this is `undefined`.
|
|
641
|
+
*
|
|
642
|
+
* @since 1.0.0
|
|
643
|
+
*/
|
|
644
|
+
value: T | (unknown & {
|
|
645
|
+
type?: never;
|
|
646
|
+
}) | undefined | unknown;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Interface representing the internal state of a mock function, tracking details of its invocations,
|
|
650
|
+
* such as arguments, contexts, return values, and more.
|
|
651
|
+
*
|
|
652
|
+
* @template ReturnType - The type of value returned by the mock function.
|
|
653
|
+
* @template Context - The type of the `this` context used during the mocks execution. Defaults to `DefaultContextType`.
|
|
654
|
+
* @template Args - The type of arguments passed to the mock function. Default to an array of unknown values (`Array<unknown>`).
|
|
655
|
+
*
|
|
656
|
+
* @remarks
|
|
657
|
+
* This interface is designed to provide detailed tracking of mocks behavior,
|
|
658
|
+
* including call arguments, contexts, instances, invocation order, and results.
|
|
659
|
+
* Useful for testing and debugging in scenarios that require precise information about mock execution.
|
|
660
|
+
*
|
|
661
|
+
* @since 1.0.0
|
|
662
|
+
*/
|
|
663
|
+
interface MocksStateInterface<ReturnType, Args extends Array<unknown> = [], Context = unknown> {
|
|
664
|
+
/**
|
|
665
|
+
* An array that holds the arguments for each invocation made to the mock.
|
|
666
|
+
* Each entry corresponds to the arguments passed during a single call to the mock function.
|
|
667
|
+
*
|
|
668
|
+
* @since 1.0.0
|
|
669
|
+
*/
|
|
670
|
+
calls: Array<Args>;
|
|
671
|
+
/**
|
|
672
|
+
* The arguments passed to the mock during its most recent invocation.
|
|
673
|
+
* Returns `undefined` if the mock has not been called yet.
|
|
674
|
+
*
|
|
675
|
+
* @since 1.0.0
|
|
676
|
+
*/
|
|
677
|
+
lastCall?: Args;
|
|
678
|
+
/**
|
|
679
|
+
* An array of contexts (`this` values) for each invocation made to the mock.
|
|
680
|
+
* Each entry corresponds to the context in which the mock was called.
|
|
681
|
+
*
|
|
682
|
+
* @since 1.0.0
|
|
683
|
+
*/
|
|
684
|
+
contexts: Array<Context>;
|
|
685
|
+
/**
|
|
686
|
+
* An array of all object instances created by the mock.
|
|
687
|
+
* Each entry represents an instance was instantiated during the mocks invocations.
|
|
688
|
+
*
|
|
689
|
+
* @since 1.0.0
|
|
690
|
+
*/
|
|
691
|
+
instances: Array<Context>;
|
|
692
|
+
/**
|
|
693
|
+
* An array of invocation order indices for the mock.
|
|
694
|
+
* xJet assigns an index to each call, starting from 1, to track the order in which mocks are invoked within a test file.
|
|
695
|
+
*
|
|
696
|
+
* @since 1.0.0
|
|
697
|
+
*/
|
|
698
|
+
invocationCallOrder: Array<number>;
|
|
699
|
+
/**
|
|
700
|
+
* An array of results for each invocation made to the mock.
|
|
701
|
+
* Each entry represents the outcome of a single call, including the return value or any error thrown.
|
|
702
|
+
*
|
|
703
|
+
* @since 1.0.0
|
|
704
|
+
*/
|
|
705
|
+
results: Array<MockInvocationResultInterface<ReturnType>>;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Import will remove at compile time
|
|
710
|
+
*/
|
|
711
|
+
/**
|
|
712
|
+
* Manages the state of test suites within the testing framework, tracking the hierarchy of describe blocks and test cases.
|
|
713
|
+
* Implemented as a singleton to ensure a single source of truth for the test suite structure.
|
|
714
|
+
*
|
|
715
|
+
* @template TestModel - The model type representing individual test cases
|
|
716
|
+
* @template FunctionType - The function type for describe block implementations
|
|
717
|
+
* @template DescribeModel - The model type representing test suite describe blocks
|
|
718
|
+
* @template DescribeOptionsInterface - Configuration options for describe blocks
|
|
719
|
+
*
|
|
720
|
+
* @throws Error - If you attempt to instantiate this class directly instead of using getInstance()
|
|
721
|
+
*
|
|
722
|
+
* @remarks
|
|
723
|
+
* The singleton pattern ensures that all test registration operations work with the same
|
|
724
|
+
* state instance, properly maintaining the hierarchical structure of tests.
|
|
725
|
+
* Use the static getInstance() method to access the singleton instance.
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* ```ts
|
|
729
|
+
* // Get the singleton instance
|
|
730
|
+
* const suiteState = SuiteState.getInstance();
|
|
731
|
+
*
|
|
732
|
+
* // Add a describe block
|
|
733
|
+
* suiteState.addDescribe('My test suite', () => {
|
|
734
|
+
* // Test suite implementation
|
|
735
|
+
* }, { only: true });
|
|
736
|
+
*
|
|
737
|
+
* // Add a test case to the current describe block
|
|
738
|
+
* suiteState.addTest(new TestModel('should work', () => {}, { skip: false }));
|
|
739
|
+
* ```
|
|
740
|
+
*
|
|
741
|
+
* @see TestModel
|
|
742
|
+
* @see DescribeModel
|
|
743
|
+
*
|
|
744
|
+
* @since 1.0.0
|
|
745
|
+
*/
|
|
746
|
+
declare class SuiteState {
|
|
747
|
+
/**
|
|
748
|
+
* Tracks whether any test or describe block has the 'only' flag set
|
|
749
|
+
*
|
|
750
|
+
* @since 1.0.0
|
|
751
|
+
*/
|
|
752
|
+
private onlyMode;
|
|
753
|
+
/**
|
|
754
|
+
* Reference to the currently active describe block in the test hierarchy
|
|
755
|
+
*
|
|
756
|
+
* @see DescribeModel
|
|
757
|
+
* @since 1.0.0
|
|
758
|
+
*/
|
|
759
|
+
private currentDescribe;
|
|
760
|
+
/**
|
|
761
|
+
* Reference to the test currently being defined or executed
|
|
762
|
+
*
|
|
763
|
+
* @see TestModel
|
|
764
|
+
* @since 1.0.0
|
|
765
|
+
*/
|
|
766
|
+
private currentTest?;
|
|
767
|
+
/**
|
|
768
|
+
* Suite has at least one test
|
|
769
|
+
* @since 1.0.0
|
|
770
|
+
*/
|
|
771
|
+
private hasTests;
|
|
772
|
+
/**
|
|
773
|
+
* The top-level describe block that contains all tests and nested describe blocks
|
|
774
|
+
*
|
|
775
|
+
* @see DescribeModel
|
|
776
|
+
* @since 1.0.0
|
|
777
|
+
*/
|
|
778
|
+
private readonly rootDescribe;
|
|
779
|
+
/**
|
|
780
|
+
* Array of regular expressions used for filtering test paths
|
|
781
|
+
*
|
|
782
|
+
* @remarks
|
|
783
|
+
* This array stores regular expressions created from filter strings specified in runtime configuration.
|
|
784
|
+
* Used to determine which tests should be executed when filtering is enabled.
|
|
785
|
+
*
|
|
786
|
+
* @see SuiteState.matchesFilter - Method that uses these regex patterns for test filtering
|
|
787
|
+
* @since 1.0.0
|
|
788
|
+
*/
|
|
789
|
+
private readonly filterRegexChain;
|
|
790
|
+
/**
|
|
791
|
+
* Checks if a test path matches the provided regular expression filter patterns
|
|
792
|
+
*
|
|
793
|
+
* This method compares a test's full path (including ancestry and description) against one or more
|
|
794
|
+
* regular expression patterns to determine if the test should be included in execution.
|
|
795
|
+
*
|
|
796
|
+
* @param path - Array of path segments representing test ancestry and description
|
|
797
|
+
* @param filter - Array of RegExp objects to test against the path
|
|
798
|
+
* @returns Boolean indicating whether the path matches the filter patterns
|
|
799
|
+
*
|
|
800
|
+
* @throws Error - When invalid regular expressions are provided
|
|
801
|
+
*
|
|
802
|
+
* @remarks
|
|
803
|
+
* The method aligns the filter from the end of the path, allowing partial matching of nested test structures.
|
|
804
|
+
* This is particularly useful for selecting specific test cases within a larger test hierarchy.
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```ts
|
|
808
|
+
* // Match tests with description containing "login"
|
|
809
|
+
* const matches = SuiteState.matchesFilter(
|
|
810
|
+
* ['Auth', 'User', 'should handle login correctly'],
|
|
811
|
+
* [/login/i]
|
|
812
|
+
* );
|
|
813
|
+
* // Returns: true
|
|
814
|
+
*
|
|
815
|
+
* // Match specific test path structure
|
|
816
|
+
* const matches = SuiteState.matchesFilter(
|
|
817
|
+
* ['API', 'GET', 'should return 200 status'],
|
|
818
|
+
* [/API/, /GET/, /status/]
|
|
819
|
+
* );
|
|
820
|
+
* // Returns: true
|
|
821
|
+
* ```
|
|
822
|
+
*
|
|
823
|
+
* @see filterChain - String-based alternative for exact matching
|
|
824
|
+
* @see addTest - Method that uses matchesFilter to determine which tests to run
|
|
825
|
+
*
|
|
826
|
+
* @since 1.0.0
|
|
827
|
+
*/
|
|
828
|
+
static matchesFilter(path: string[], filter: RegExp[]): boolean;
|
|
829
|
+
/**
|
|
830
|
+
* Indicates whether the test suite is running in "only" mode
|
|
831
|
+
*
|
|
832
|
+
* @returns True if only specifically marked tests should run
|
|
833
|
+
*
|
|
834
|
+
* @since 1.0.0
|
|
835
|
+
*/
|
|
836
|
+
get isOnlyMode(): boolean;
|
|
837
|
+
/**
|
|
838
|
+
* Gets the root describe block of the test suite
|
|
839
|
+
*
|
|
840
|
+
* @returns The top-level describe block
|
|
841
|
+
*
|
|
842
|
+
* @see DescribeModel
|
|
843
|
+
* @since 1.0.0
|
|
844
|
+
*/
|
|
845
|
+
get root(): DescribeModel;
|
|
846
|
+
/**
|
|
847
|
+
* Gets the current describe block being defined
|
|
848
|
+
*
|
|
849
|
+
* @returns The active describe block
|
|
850
|
+
*
|
|
851
|
+
* @see DescribeModel
|
|
852
|
+
* @since 1.0.0
|
|
853
|
+
*/
|
|
854
|
+
get describe(): DescribeModel;
|
|
855
|
+
/**
|
|
856
|
+
* Gets the test currently being defined or executed
|
|
857
|
+
*
|
|
858
|
+
* @returns The current test or null if no test is active
|
|
859
|
+
*
|
|
860
|
+
* @see TestModel
|
|
861
|
+
* @since 1.0.0
|
|
862
|
+
*/
|
|
863
|
+
get test(): TestModel | undefined;
|
|
864
|
+
/**
|
|
865
|
+
* Sets the current test being defined or executed
|
|
866
|
+
*
|
|
867
|
+
* @param test - The test to set as current
|
|
868
|
+
*
|
|
869
|
+
* @see TestModel
|
|
870
|
+
* @since 1.0.0
|
|
871
|
+
*/
|
|
872
|
+
set test(test: TestModel | undefined);
|
|
873
|
+
/**
|
|
874
|
+
* Executes the entire test suite from the root describe block
|
|
875
|
+
*
|
|
876
|
+
* @param context - The execution context containing runtime information
|
|
877
|
+
*
|
|
878
|
+
* @throws Error - When test execution fails, the error is encoded and dispatched
|
|
879
|
+
*
|
|
880
|
+
* @remarks
|
|
881
|
+
* This method initiates the test execution flow by calling run() on the root describe block
|
|
882
|
+
* and emits an END status notification when all tests complete successfully. If an error
|
|
883
|
+
* occurs during execution, it's captured, encoded, and dispatched through the error schema.
|
|
884
|
+
*
|
|
885
|
+
* @example
|
|
886
|
+
* ```ts
|
|
887
|
+
* const suiteState = SuiteState.getInstance();
|
|
888
|
+
* await suiteState.run({
|
|
889
|
+
* timeout: 5000,
|
|
890
|
+
* currentPath: '/tests',
|
|
891
|
+
* skipAfterFailure: true
|
|
892
|
+
* });
|
|
893
|
+
* ```
|
|
894
|
+
*
|
|
895
|
+
* @see emitStatus
|
|
896
|
+
* @see DescribeModel
|
|
897
|
+
*
|
|
898
|
+
* @since 1.0.0
|
|
899
|
+
*/
|
|
900
|
+
run(context: ContextType<ContextInterface>): Promise<void>;
|
|
901
|
+
/**
|
|
902
|
+
* Adds a new describe block to the test suite hierarchy
|
|
903
|
+
*
|
|
904
|
+
* @param description - The textual description of the describe block
|
|
905
|
+
* @param describeFn - The function containing the tests and nested describes
|
|
906
|
+
* @param flags - Configuration options for the describe block
|
|
907
|
+
* @param describeArgs - Arguments to pass to the describe function
|
|
908
|
+
*
|
|
909
|
+
* @throws Error - If the describe function throws any exceptions
|
|
910
|
+
*
|
|
911
|
+
* @remarks
|
|
912
|
+
* This method temporarily changes the current describe context while executing
|
|
913
|
+
* the describeFn, then restores it afterward, ensuring proper nesting of test blocks.
|
|
914
|
+
* If the 'only' flag is set, it enables only-mode for the entire test suite.
|
|
915
|
+
*
|
|
916
|
+
* @example
|
|
917
|
+
* ```ts
|
|
918
|
+
* suiteState.addDescribe(
|
|
919
|
+
* 'my test group',
|
|
920
|
+
* () => {
|
|
921
|
+
* // test definitions here
|
|
922
|
+
* },
|
|
923
|
+
* { only: true }
|
|
924
|
+
* );
|
|
925
|
+
* ```
|
|
926
|
+
*
|
|
927
|
+
* @see FunctionType
|
|
928
|
+
* @see DescribeModel
|
|
929
|
+
* @see DescribeOptionsInterface
|
|
930
|
+
*
|
|
931
|
+
* @since 1.0.0
|
|
932
|
+
*/
|
|
933
|
+
addDescribe(description: string, describeFn: FunctionType, flags?: DescribeOptionsInterface, describeArgs?: Array<unknown>): void;
|
|
934
|
+
/**
|
|
935
|
+
* Adds a test to the current describe block
|
|
936
|
+
*
|
|
937
|
+
* @param test - The test model to add
|
|
938
|
+
*
|
|
939
|
+
* @remarks
|
|
940
|
+
* If the test has the 'only' option set, it enables only-mode for the entire test suite.
|
|
941
|
+
*
|
|
942
|
+
* @see TestModel
|
|
943
|
+
* @since 1.0.0
|
|
944
|
+
*/
|
|
945
|
+
addTest(test: TestModel): void;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* Import will remove at compile time
|
|
950
|
+
*/
|
|
951
|
+
/**
|
|
952
|
+
* Represents a test case within the testing framework that manages execution,
|
|
953
|
+
* timing, and reporting of individual tests.
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* ```ts
|
|
957
|
+
* const test = new TestModel(
|
|
958
|
+
* 'should validate user input correctly',
|
|
959
|
+
* async () => {
|
|
960
|
+
* const result = await validateInput('test@example.com');
|
|
961
|
+
* expect(result.isValid).toBe(true);
|
|
962
|
+
* },
|
|
963
|
+
* 2000
|
|
964
|
+
* );
|
|
965
|
+
*
|
|
966
|
+
* // Set ancestry to show the test's location in the test hierarchy
|
|
967
|
+
* test.ancestry = ['Authentication', 'Input Validation'];
|
|
968
|
+
*
|
|
969
|
+
* // Execute the test
|
|
970
|
+
* await test.execute();
|
|
971
|
+
* ```
|
|
972
|
+
*
|
|
973
|
+
* @remarks
|
|
974
|
+
* The TestModel is responsible for tracking test execution time, handling timeouts,
|
|
975
|
+
* managing test context, and reporting test status through the emit service.
|
|
976
|
+
* It supports both synchronous and promise-based test implementations.
|
|
977
|
+
*
|
|
978
|
+
* @throws FailingError - When a test fails due to assertions or errors
|
|
979
|
+
*
|
|
980
|
+
* @see ContextInterface
|
|
981
|
+
* @see EmitActionEventType
|
|
982
|
+
* @see EmitStatusEventType
|
|
983
|
+
* @see InvocationLocationType
|
|
984
|
+
*
|
|
985
|
+
* @since 1.0.0
|
|
986
|
+
*/
|
|
987
|
+
declare class TestModel {
|
|
988
|
+
readonly description: string;
|
|
989
|
+
private readonly testImplementation;
|
|
990
|
+
private readonly timeoutDuration;
|
|
991
|
+
private readonly testParameters;
|
|
992
|
+
private readonly testOptions;
|
|
993
|
+
/**
|
|
994
|
+
* Stores the hierarchical path of parent test descriptions that contain this test
|
|
995
|
+
*
|
|
996
|
+
* @default []
|
|
997
|
+
* @since 1.0.0
|
|
998
|
+
*/
|
|
999
|
+
readonly ancestry: Array<string>;
|
|
1000
|
+
/**
|
|
1001
|
+
* Stores information about where this test is being executed in the source code
|
|
1002
|
+
*
|
|
1003
|
+
* @see InvocationLocationType
|
|
1004
|
+
* @since 1.0.0
|
|
1005
|
+
*/
|
|
1006
|
+
private executionLocation;
|
|
1007
|
+
/**
|
|
1008
|
+
* Timestamp when test execution started, measured in milliseconds
|
|
1009
|
+
*
|
|
1010
|
+
* @default 0
|
|
1011
|
+
* @since 1.0.0
|
|
1012
|
+
*/
|
|
1013
|
+
private executionStartTime;
|
|
1014
|
+
/**
|
|
1015
|
+
* Creates a new instance of the TestModel to represent an executable test.
|
|
1016
|
+
*
|
|
1017
|
+
* @param description - The test description that explains the purpose of the test
|
|
1018
|
+
* @param testImplementation - The function containing the actual test code to be executed
|
|
1019
|
+
* @param timeoutDuration - The maximum time in milliseconds that the test is allowed to run
|
|
1020
|
+
* @param testParameters - An array of parameters to be passed to the test implementation
|
|
1021
|
+
* @param testOptions - Configuration options that control test execution behavior
|
|
1022
|
+
*
|
|
1023
|
+
* @example
|
|
1024
|
+
* ```ts
|
|
1025
|
+
* const test = new TestModel(
|
|
1026
|
+
* 'should calculate the correct sum',
|
|
1027
|
+
* () => expect(sum(2, 2)).toBe(4),
|
|
1028
|
+
* 5000
|
|
1029
|
+
* );
|
|
1030
|
+
* ```
|
|
1031
|
+
*
|
|
1032
|
+
* @since 1.0.0
|
|
1033
|
+
*/
|
|
1034
|
+
constructor(description: string, testImplementation: FunctionType, timeoutDuration: number, testParameters?: unknown[], testOptions?: TestFlagsType);
|
|
1035
|
+
/**
|
|
1036
|
+
* Provides access to the test configuration options.
|
|
1037
|
+
*
|
|
1038
|
+
* @returns The test configuration options that control test execution behavior
|
|
1039
|
+
*
|
|
1040
|
+
* @since 1.0.0
|
|
1041
|
+
*/
|
|
1042
|
+
get options(): TestFlagsType;
|
|
1043
|
+
/**
|
|
1044
|
+
* Sets the location where the test is being executed.
|
|
1045
|
+
*
|
|
1046
|
+
* @param location - The execution location information containing position details
|
|
1047
|
+
*
|
|
1048
|
+
* @see InvocationLocationType
|
|
1049
|
+
* @since 1.0.0
|
|
1050
|
+
*/
|
|
1051
|
+
setExecutionLocation(location: string): void;
|
|
1052
|
+
/**
|
|
1053
|
+
* Applies execution control options to the test, allowing it to be skipped or run exclusively.
|
|
1054
|
+
*
|
|
1055
|
+
* @param skip - Flag indicating whether the test should be skipped during execution
|
|
1056
|
+
* @param only - Flag indicating whether only this test should be executed
|
|
1057
|
+
*
|
|
1058
|
+
* @remarks
|
|
1059
|
+
* This method uses logical OR assignment (||=) to ensure options are only set to true and
|
|
1060
|
+
* never reset from true to false. Existing flag values take precedence.
|
|
1061
|
+
*
|
|
1062
|
+
* @example
|
|
1063
|
+
* ```ts
|
|
1064
|
+
* testInstance.applyExecutionFlags(true, false); // Will mark test to be skipped
|
|
1065
|
+
* testInstance.applyExecutionFlags(false, true); // Will mark test to run exclusively
|
|
1066
|
+
* ```
|
|
1067
|
+
*
|
|
1068
|
+
* @see TestFlagsType
|
|
1069
|
+
* @since 1.0.0
|
|
1070
|
+
*/
|
|
1071
|
+
applyExecutionFlags(skip?: boolean, only?: boolean): void;
|
|
1072
|
+
/**
|
|
1073
|
+
* Sets the ancestry for this test by adding parent test descriptions to the ancestry chain.
|
|
1074
|
+
*
|
|
1075
|
+
* @param parentTests - Array of parent test description strings representing the test hierarchy
|
|
1076
|
+
*
|
|
1077
|
+
* @example
|
|
1078
|
+
* ```ts
|
|
1079
|
+
* const childTest = new TestModel("should work", () => {}, 5000);
|
|
1080
|
+
* childTest.setAncestry(["describe block A", "describe block B"]);
|
|
1081
|
+
* ```
|
|
1082
|
+
*
|
|
1083
|
+
* @since 1.0.0
|
|
1084
|
+
*/
|
|
1085
|
+
setAncestry(parentTests: string[]): void;
|
|
1086
|
+
/**
|
|
1087
|
+
* Executes the test within the specified context, managing test lifecycle and status reporting.
|
|
1088
|
+
*
|
|
1089
|
+
* @param context - The test execution context containing shared state and utilities
|
|
1090
|
+
* @param runLifecycleHooks - Function that runs before/after hooks at appropriate times
|
|
1091
|
+
* @param isExclusiveMode - Flag indicating if tests are running in exclusive mode (only marked tests)
|
|
1092
|
+
* @returns Promise that resolves when the test execution completes
|
|
1093
|
+
*
|
|
1094
|
+
* @throws Error - If any unhandled exceptions occur during test execution that aren't captured
|
|
1095
|
+
*
|
|
1096
|
+
* @remarks
|
|
1097
|
+
* The method tracks execution time, manages test lifecycle, handles skipping logic, and processes
|
|
1098
|
+
* test results. It also notifies about test status changes through observer pattern implementation.
|
|
1099
|
+
*
|
|
1100
|
+
* @example
|
|
1101
|
+
* ```ts
|
|
1102
|
+
* // Running a test with context and lifecycle hooks
|
|
1103
|
+
* await testInstance.run(
|
|
1104
|
+
* testContext,
|
|
1105
|
+
* async (hookType, ctx) => {
|
|
1106
|
+
* await runHooks(hookType, ctx);
|
|
1107
|
+
* },
|
|
1108
|
+
* false
|
|
1109
|
+
* );
|
|
1110
|
+
* ```
|
|
1111
|
+
*
|
|
1112
|
+
* @see HookType
|
|
1113
|
+
* @see ActionType
|
|
1114
|
+
* @see ContextInterface
|
|
1115
|
+
*
|
|
1116
|
+
* @since 1.0.0
|
|
1117
|
+
*/
|
|
1118
|
+
run(context: ContextType<ContextInterface>, runLifecycleHooks: FunctionLikeType<Promise<void>, [HookType, ContextType<ContextInterface>]>, isExclusiveMode?: boolean): Promise<void>;
|
|
1119
|
+
private determineSkipAction;
|
|
1120
|
+
private getExecutionStrategy;
|
|
1121
|
+
private notifyTestStatus;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* Represents a collection of optional options used for test configuration.
|
|
1126
|
+
*
|
|
1127
|
+
* @property skip - Indicates whether the test should be skipped during execution.
|
|
1128
|
+
* @property only - Ensures that only this specific test or set of tests will be run.
|
|
1129
|
+
* @property todo - Marks the test as a work-in-progress or not yet implemented.
|
|
1130
|
+
* @property failing - Denotes that the test is expected to fail as part of the workflow.
|
|
1131
|
+
*
|
|
1132
|
+
* @remarks
|
|
1133
|
+
* The `TestFlagsType` type is commonly used to specify various states or behaviors for a test case.
|
|
1134
|
+
* Each property is optional, and the combination of these options can influence how a test suite operates.
|
|
1135
|
+
*
|
|
1136
|
+
* @since 1.0.0
|
|
1137
|
+
*/
|
|
1138
|
+
type TestFlagsType = {
|
|
1139
|
+
skip?: boolean;
|
|
1140
|
+
only?: boolean;
|
|
1141
|
+
todo?: boolean;
|
|
1142
|
+
failing?: boolean;
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Import will remove at compile time
|
|
1147
|
+
*/
|
|
1148
|
+
/**
|
|
1149
|
+
* Represents options that control the execution behavior of a `describe` block.
|
|
1150
|
+
*
|
|
1151
|
+
* @remarks
|
|
1152
|
+
* This interface is used to configure specific conditions for a `describe` block,
|
|
1153
|
+
* such as skipping the execution (`skip`) or running it exclusively (`only`).
|
|
1154
|
+
* Both properties are optional and allow flexible test suite configuration.
|
|
1155
|
+
*
|
|
1156
|
+
* @example
|
|
1157
|
+
* ```ts
|
|
1158
|
+
* const options: DescribeOptionsInterface = {
|
|
1159
|
+
* skip: true, // This described block will be skipped
|
|
1160
|
+
* only: false // Not marked as exclusive; can be omitted
|
|
1161
|
+
* };
|
|
1162
|
+
* ```
|
|
1163
|
+
*
|
|
1164
|
+
* @since 1.0.0
|
|
1165
|
+
*/
|
|
1166
|
+
interface DescribeOptionsInterface {
|
|
1167
|
+
/**
|
|
1168
|
+
* If true, the `describe` block will be skipped.
|
|
1169
|
+
* @since 1.0.0
|
|
1170
|
+
*/
|
|
1171
|
+
skip?: boolean;
|
|
1172
|
+
/**
|
|
1173
|
+
* If true, the `describe` block will run exclusively, ignoring other blocks.
|
|
1174
|
+
* @since 1.0.0
|
|
1175
|
+
*/
|
|
1176
|
+
only?: boolean;
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* Represents the collection of hooks for a `describe` block.
|
|
1180
|
+
*
|
|
1181
|
+
* @remarks
|
|
1182
|
+
* Each `describe` block can define multiple lifecycle hooks:
|
|
1183
|
+
* - `beforeAll` → Runs once before all tests in the block.
|
|
1184
|
+
* - `afterAll` → Runs once after all tests in the block.
|
|
1185
|
+
* - `beforeEach` → Runs before each test in the block.
|
|
1186
|
+
* - `afterEach` → Runs after each test in the block.
|
|
1187
|
+
*
|
|
1188
|
+
* These hooks are stored as arrays of {@link HookModel} instances.
|
|
1189
|
+
*
|
|
1190
|
+
* @example
|
|
1191
|
+
* ```ts
|
|
1192
|
+
* const hooks: DescribeHooksInterface = {
|
|
1193
|
+
* beforeAll: [new HookModel(() => {}, 5000)],
|
|
1194
|
+
* afterAll: [new HookModel(() => {}, 5000)],
|
|
1195
|
+
* beforeEach: [new HookModel(() => {}, 5000)],
|
|
1196
|
+
* afterEach: [new HookModel(() => {}, 5000)]
|
|
1197
|
+
* };
|
|
1198
|
+
* ```
|
|
1199
|
+
*
|
|
1200
|
+
* @since 1.0.0
|
|
1201
|
+
*/
|
|
1202
|
+
interface DescribeHooksInterface {
|
|
1203
|
+
/**
|
|
1204
|
+
* Hooks to execute once after all tests in the `describe` block.
|
|
1205
|
+
*
|
|
1206
|
+
* @see HookModel
|
|
1207
|
+
* @since 1.0.0
|
|
1208
|
+
*/
|
|
1209
|
+
afterAll: Array<HookModel>;
|
|
1210
|
+
/**
|
|
1211
|
+
* Hooks to execute once before all tests in the `describe` block.
|
|
1212
|
+
*
|
|
1213
|
+
* @see HookModel
|
|
1214
|
+
* @since 1.0.0
|
|
1215
|
+
*/
|
|
1216
|
+
beforeAll: Array<HookModel>;
|
|
1217
|
+
/**
|
|
1218
|
+
* Hooks to execute before each test in the `describe` block.
|
|
1219
|
+
*
|
|
1220
|
+
* @see HookModel
|
|
1221
|
+
* @since 1.0.0
|
|
1222
|
+
*/
|
|
1223
|
+
beforeEach: Array<HookModel>;
|
|
1224
|
+
/**
|
|
1225
|
+
* Hooks to execute after each test in the `describe` block.
|
|
1226
|
+
*
|
|
1227
|
+
* @see HookModel
|
|
1228
|
+
* @since 1.0.0
|
|
1229
|
+
*/
|
|
1230
|
+
afterEach: Array<HookModel>;
|
|
1231
|
+
}
|
|
1232
|
+
/**
|
|
1233
|
+
* Represents the execution context for a test suite, tracking errors and overall status.
|
|
1234
|
+
*
|
|
1235
|
+
* @remarks
|
|
1236
|
+
* - `hasError` is `true` if any test or suite has failed, indicating that bail mode is active
|
|
1237
|
+
* and the runner should not execute the remaining tests or describe blocks in the current suites.
|
|
1238
|
+
* - `beforeAllErrors` collects errors thrown during `beforeAll` hooks.
|
|
1239
|
+
* - `afterAllErrors` collects errors thrown during `afterAll` hooks.
|
|
1240
|
+
*
|
|
1241
|
+
* @example
|
|
1242
|
+
* ```ts
|
|
1243
|
+
* const context: ContextInterface = {
|
|
1244
|
+
* hasError: false,
|
|
1245
|
+
* beforeAllErrors: [],
|
|
1246
|
+
* afterAllErrors: []
|
|
1247
|
+
* };
|
|
1248
|
+
*
|
|
1249
|
+
* if (context.hasError) {
|
|
1250
|
+
* console.log("Bail is active. Skipping remaining tests.");
|
|
1251
|
+
* }
|
|
1252
|
+
* ```
|
|
1253
|
+
*
|
|
1254
|
+
* @since 1.0.0
|
|
1255
|
+
*/
|
|
1256
|
+
interface ContextInterface {
|
|
1257
|
+
/**
|
|
1258
|
+
* Indicates whether any test or suite has failed. When `true`, bail mode is active,
|
|
1259
|
+
* and the runner should skip the remaining tests or describe blocks.
|
|
1260
|
+
*
|
|
1261
|
+
* @since 1.0.0
|
|
1262
|
+
*/
|
|
1263
|
+
hasError: boolean;
|
|
1264
|
+
/**
|
|
1265
|
+
* Errors thrown during `beforeAll` hooks.
|
|
1266
|
+
*
|
|
1267
|
+
* @since 1.0.0
|
|
1268
|
+
*/
|
|
1269
|
+
beforeAllErrors: Array<unknown>;
|
|
1270
|
+
/**
|
|
1271
|
+
* Errors thrown during `afterAll` hooks.
|
|
1272
|
+
*
|
|
1273
|
+
* @since 1.0.0
|
|
1274
|
+
*/
|
|
1275
|
+
afterAllErrors: Array<unknown>;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
/**
|
|
1279
|
+
* Import will remove at compile time
|
|
1280
|
+
*/
|
|
1281
|
+
/**
|
|
1282
|
+
* Represents a model for managing and executing hook functions with a specified timeout.
|
|
1283
|
+
* Provides functionality for associating invocation locations and handling errors during execution.
|
|
1284
|
+
*
|
|
1285
|
+
* @remarks
|
|
1286
|
+
* This class ensures that hooks are executed with proper timeout enforcement
|
|
1287
|
+
* and supports both synchronous and asynchronous hook functions.
|
|
1288
|
+
* It throws an error if an asynchronous hook
|
|
1289
|
+
* incorrectly uses a "done" callback or if it fails to complete execution within the specified timeout.
|
|
1290
|
+
*
|
|
1291
|
+
* @since 1.0.0
|
|
1292
|
+
*/
|
|
1293
|
+
declare class HookModel {
|
|
1294
|
+
private readonly hookFunction;
|
|
1295
|
+
private readonly timeout;
|
|
1296
|
+
/**
|
|
1297
|
+
* Represents the current location of an invocation within the application or a fallback to undefined.
|
|
1298
|
+
*
|
|
1299
|
+
* @remarks
|
|
1300
|
+
* This variable is designed
|
|
1301
|
+
* to hold the interface representing the invocation's location for tracking.
|
|
1302
|
+
* If no invocation location is available, it defaults to undefined.
|
|
1303
|
+
*
|
|
1304
|
+
* @since 1.0.0
|
|
1305
|
+
*/
|
|
1306
|
+
private location;
|
|
1307
|
+
/**
|
|
1308
|
+
* Constructs an instance of the class.
|
|
1309
|
+
*
|
|
1310
|
+
* @param hookFunction - The callback function that will be executed based on specific conditions.
|
|
1311
|
+
* @param timeout - The time in milliseconds before the callback function is triggered.
|
|
1312
|
+
* @throws Will throw an error if the provided timeout value is negative or invalid.
|
|
1313
|
+
*
|
|
1314
|
+
* @remarks This constructor sets up the necessary callback function and timeout configuration for the instance.
|
|
1315
|
+
*
|
|
1316
|
+
* @since 1.0.0
|
|
1317
|
+
*/
|
|
1318
|
+
constructor(hookFunction: CallbackHandlerType, timeout: number);
|
|
1319
|
+
/**
|
|
1320
|
+
* Sets the location of the invocation based on the provided location object.
|
|
1321
|
+
*
|
|
1322
|
+
* @param location - An object implementing the `InvocationLocationType` or `undefined`
|
|
1323
|
+
* to reset the location.
|
|
1324
|
+
*
|
|
1325
|
+
* @remarks
|
|
1326
|
+
* This method assigns a new location value to the current location of the invocation.
|
|
1327
|
+
* Passing `undefined` will clear the current location.
|
|
1328
|
+
*
|
|
1329
|
+
* @since 1.0.0
|
|
1330
|
+
*/
|
|
1331
|
+
setLocation(location: string): void;
|
|
1332
|
+
/**
|
|
1333
|
+
* Executes a hook function with a timeout mechanism.
|
|
1334
|
+
*
|
|
1335
|
+
* @param context - The context object passed to the hook function, containing execution-specific information.
|
|
1336
|
+
* @return A promise that resolves when the hook function completes its execution successfully.
|
|
1337
|
+
*
|
|
1338
|
+
* @throws TimeoutError - If the hook function does not call `done()` within the specified timeout duration.
|
|
1339
|
+
*
|
|
1340
|
+
* @remarks This method concurrently runs the hook function and a timeout check, ensuring that execution does not
|
|
1341
|
+
* exceed the predefined timeout.
|
|
1342
|
+
* If the timeout is reached without the hook function completing, an ` TimeoutError ` is thrown.
|
|
1343
|
+
*
|
|
1344
|
+
* @since 1.0.0
|
|
1345
|
+
*/
|
|
1346
|
+
run(context: ContextType<unknown>): Promise<void>;
|
|
1347
|
+
/**
|
|
1348
|
+
* Executes a given hook function, handling both synchronous and asynchronous hooks,
|
|
1349
|
+
* with or without a callback mechanism.
|
|
1350
|
+
*
|
|
1351
|
+
* @param hook - The function representing the hook to be executed.
|
|
1352
|
+
* It Can be a synchronous or asynchronous function.
|
|
1353
|
+
* @param context - An object to be used as the `this` context
|
|
1354
|
+
* when invoking the hook function.
|
|
1355
|
+
* @remarks This method ensures correct execution and error handling
|
|
1356
|
+
* for both callback-based and promise-based hooks.
|
|
1357
|
+
*
|
|
1358
|
+
* @since 1.0.0
|
|
1359
|
+
*/
|
|
1360
|
+
private executeHook;
|
|
1361
|
+
/**
|
|
1362
|
+
* Executes a callback-style hook by wrapping it in a Promise
|
|
1363
|
+
*
|
|
1364
|
+
* @param hook - The callback hook function to execute
|
|
1365
|
+
* @param context - Execution context to bind to the hook function
|
|
1366
|
+
* @returns A Promise that resolves when the hook completes or rejects when it fails
|
|
1367
|
+
*
|
|
1368
|
+
* @throws Error - When the hook callback is invoked with an error parameter
|
|
1369
|
+
*
|
|
1370
|
+
* @remarks This method converts traditional Node.js-style callback patterns (error-first callbacks)
|
|
1371
|
+
* into Promise-based async/await compatible functions. The hook function receives a done callback
|
|
1372
|
+
* as its argument and must call it to signal completion.
|
|
1373
|
+
*
|
|
1374
|
+
* @example
|
|
1375
|
+
* ```ts
|
|
1376
|
+
* const callbackHook = (done) => {
|
|
1377
|
+
* setTimeout(() => done(), 100);
|
|
1378
|
+
* };
|
|
1379
|
+
*
|
|
1380
|
+
* await executeCallbackHook(callbackHook, this);
|
|
1381
|
+
* ```
|
|
1382
|
+
*
|
|
1383
|
+
* @see isPromise
|
|
1384
|
+
* @since 1.0.0
|
|
1385
|
+
*/
|
|
1386
|
+
private executeCallbackHook;
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
/**
|
|
1390
|
+
* Represents a callback function type used to signal the completion of an operation.
|
|
1391
|
+
* Typically used in asynchronous functions to convey success or error states.
|
|
1392
|
+
*
|
|
1393
|
+
* @param error - An optional parameter indicating the error details.
|
|
1394
|
+
* Pass a string or an object with a `message` property to specify the error reason.
|
|
1395
|
+
* If no error occurs, this parameter may be omitted or set to `undefined`.
|
|
1396
|
+
*
|
|
1397
|
+
* @remarks
|
|
1398
|
+
* This type is designed for scenarios requiring explicit error signaling.
|
|
1399
|
+
* Consumers of this callback should handle both error and non-error cases appropriately.
|
|
1400
|
+
*
|
|
1401
|
+
* @since 1.0.0
|
|
1402
|
+
*/
|
|
1403
|
+
type DoneCallbackType = (error?: string | {
|
|
1404
|
+
message: string;
|
|
1405
|
+
}) => void;
|
|
1406
|
+
/**
|
|
1407
|
+
* Represents a type definition for a callback handler function.
|
|
1408
|
+
*
|
|
1409
|
+
* @remarks
|
|
1410
|
+
* This type can either be:
|
|
1411
|
+
* - A synchronous function taking a `done` callback of type `DoneCallbackType`
|
|
1412
|
+
* as an argument and optionally returning `undefined`.
|
|
1413
|
+
* - An asynchronous function that returns a `Promise<void>`.
|
|
1414
|
+
* This type is designed to provide flexibility for synchronous functions operating with a `done` callback
|
|
1415
|
+
* or asynchronous functions using `Promise<void>`.
|
|
1416
|
+
*
|
|
1417
|
+
* @since 1.0.0
|
|
1418
|
+
*/
|
|
1419
|
+
type CallbackHandlerType = ((done: DoneCallbackType) => void | undefined) | (() => Promise<void>);
|
|
1420
|
+
|
|
1421
|
+
/**
|
|
1422
|
+
* Import will remove at compile time
|
|
1423
|
+
*/
|
|
1424
|
+
/**
|
|
1425
|
+
* Executes a given asynchronous or synchronous task with an optional timeout constraint.
|
|
1426
|
+
*
|
|
1427
|
+
* @typeParam T - The resolved value type.
|
|
1428
|
+
*
|
|
1429
|
+
* @param task - Either a function that returns a value or promise, or a promise itself.
|
|
1430
|
+
* @param delay - Timeout in milliseconds, or `-1` to disable the timeout.
|
|
1431
|
+
* @param at - A contextual label (e.g., method name or operation name) to aid debugging.
|
|
1432
|
+
* @param stack - Optional stack trace to provide more detailed error context.
|
|
1433
|
+
*
|
|
1434
|
+
* @throws {@link TimeoutError} If the task does not complete within the specified `delay`
|
|
1435
|
+
* (only when `delay` is not `-1`).
|
|
1436
|
+
*
|
|
1437
|
+
* @remarks
|
|
1438
|
+
* The function accepts either:
|
|
1439
|
+
* - a function returning a value or a promise, or
|
|
1440
|
+
* - a promise directly.
|
|
1441
|
+
*
|
|
1442
|
+
* If `delay` is `-1`, no timeout is applied. Otherwise the task is raced against
|
|
1443
|
+
* a timer and a {@link TimeoutError} is thrown on expiry.
|
|
1444
|
+
*
|
|
1445
|
+
* @example
|
|
1446
|
+
* ```ts
|
|
1447
|
+
* // Passing a function
|
|
1448
|
+
* await withTimeout(
|
|
1449
|
+
* () => fetchData(),
|
|
1450
|
+
* 5000,
|
|
1451
|
+
* 'fetchData'
|
|
1452
|
+
* );
|
|
1453
|
+
*
|
|
1454
|
+
* // Passing a promise directly
|
|
1455
|
+
* await withTimeout(
|
|
1456
|
+
* fetchData(),
|
|
1457
|
+
* 5000,
|
|
1458
|
+
* 'fetchDataDirect'
|
|
1459
|
+
* );
|
|
1460
|
+
*
|
|
1461
|
+
* // No timeout
|
|
1462
|
+
* await withTimeout(fetchData(), -1, 'fetchDataNoTimeout');
|
|
1463
|
+
* ```
|
|
1464
|
+
*
|
|
1465
|
+
* @since 1.1.0
|
|
1466
|
+
*/
|
|
1467
|
+
declare function withTimeout<T>(task: FunctionLikeType<T | Promise<T>> | Promise<T> | T, delay: number, at: string, stack?: string): Promise<T>;
|
|
1468
|
+
|
|
1469
|
+
declare class TimeoutError extends Error {
|
|
1470
|
+
constructor(timeout: number, at: string, stack?: string);
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
/**
|
|
1474
|
+
* Import will remove at compile time
|
|
1475
|
+
*/
|
|
1476
|
+
/**
|
|
1477
|
+
* Marks a class as injectable and stores its configuration metadata.
|
|
1478
|
+
*
|
|
1479
|
+
* @template T - The type of the class instance
|
|
1480
|
+
* @template Args - The types of arguments accepted by the constructor, defaults to `unknown[]`
|
|
1481
|
+
*
|
|
1482
|
+
* @param options - Optional configuration for the injectable, including scope and factory
|
|
1483
|
+
*
|
|
1484
|
+
* @example
|
|
1485
|
+
* ```ts
|
|
1486
|
+
* @Injectable({ scope: 'singleton' })
|
|
1487
|
+
* class MyService {}
|
|
1488
|
+
* ```
|
|
1489
|
+
*
|
|
1490
|
+
* @see InjectableOptionsInterface
|
|
1491
|
+
* @since 1.0.0
|
|
1492
|
+
*/
|
|
1493
|
+
declare function Injectable<T = unknown, Args extends Array<unknown> = unknown[]>(options?: InjectableOptionsInterface): (target: new (...args: Args) => T) => void;
|
|
1494
|
+
/**
|
|
1495
|
+
* Resolves and returns an instance of the given injectable token.
|
|
1496
|
+
*
|
|
1497
|
+
* @template T - The type of the instance to return
|
|
1498
|
+
* @template Args - The types of arguments passed to the constructor or factory
|
|
1499
|
+
*
|
|
1500
|
+
* @param token - The injectable class or token to resolve
|
|
1501
|
+
* @param args - Arguments to pass to the constructor or factory
|
|
1502
|
+
*
|
|
1503
|
+
* @returns The resolved instance of type `T`
|
|
1504
|
+
*
|
|
1505
|
+
* @throws Error - If the token is not registered as injectable via `@Injectable`
|
|
1506
|
+
*
|
|
1507
|
+
* @remarks
|
|
1508
|
+
* If the injectable is marked with scope `'singleton'`, the same instance will be returned
|
|
1509
|
+
* on the following calls. Otherwise, a new instance is created for each call.
|
|
1510
|
+
*
|
|
1511
|
+
* @example
|
|
1512
|
+
* ```ts
|
|
1513
|
+
* const service = inject(MyService);
|
|
1514
|
+
* ```
|
|
1515
|
+
*
|
|
1516
|
+
* @see TokenElementType
|
|
1517
|
+
* @since 1.0.0
|
|
1518
|
+
*/
|
|
1519
|
+
declare function inject<T, Args extends Array<unknown>>(token: TokenElementType<T, Args>, ...args: Args): T;
|
|
1520
|
+
|
|
1521
|
+
/**
|
|
1522
|
+
* Import will remove at compile time
|
|
1523
|
+
*/
|
|
1524
|
+
/**
|
|
1525
|
+
* Represents a constructor type for a given element.
|
|
1526
|
+
*
|
|
1527
|
+
* @template T - The type of the instance created by the constructor
|
|
1528
|
+
* @template Args - The types of arguments accepted by the constructor, defaults to `unknown[]`
|
|
1529
|
+
*
|
|
1530
|
+
* @see FunctionType
|
|
1531
|
+
* @since 1.0.0
|
|
1532
|
+
*/
|
|
1533
|
+
type TokenElementType<T, Args extends Array<unknown> = unknown[]> = new (...args: Args) => T;
|
|
1534
|
+
/**
|
|
1535
|
+
* Options for configuring an injectable service.
|
|
1536
|
+
*
|
|
1537
|
+
* @remarks
|
|
1538
|
+
* Used by the {@link Injectable} decorator to specify lifecycle scope
|
|
1539
|
+
* and custom factory function for service instantiation.
|
|
1540
|
+
*
|
|
1541
|
+
* @since 1.0.0
|
|
1542
|
+
*/
|
|
1543
|
+
interface InjectableOptionsInterface {
|
|
1544
|
+
/**
|
|
1545
|
+
* Lifecycle scope of the service.
|
|
1546
|
+
* - `'singleton'` - Only one instance is created and shared
|
|
1547
|
+
* - `'transient'` - A new instance is created each time it is requested
|
|
1548
|
+
*
|
|
1549
|
+
* @default 'singleton'
|
|
1550
|
+
* @since 1.0.0
|
|
1551
|
+
*/
|
|
1552
|
+
scope?: 'singleton' | 'transient';
|
|
1553
|
+
/**
|
|
1554
|
+
* Custom factory function used to create the service instance.
|
|
1555
|
+
* @since 1.0.0
|
|
1556
|
+
*/
|
|
1557
|
+
factory?: FunctionType;
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
/**
|
|
1561
|
+
* Imports
|
|
1562
|
+
*/
|
|
1563
|
+
declare class FailingError extends ExecutionError {
|
|
1564
|
+
constructor(stack: string);
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
/**
|
|
1568
|
+
* A base error class that extends the standard Error class with enhanced JSON serialization and location tracking
|
|
1569
|
+
*
|
|
1570
|
+
* @param message - The error message describing what went wrong
|
|
1571
|
+
* @param location - The precise source code location where the error occurred
|
|
1572
|
+
* @returns The constructed ExecutionError instance
|
|
1573
|
+
*
|
|
1574
|
+
* @remarks
|
|
1575
|
+
* Serves as a foundation for custom error types in the application.
|
|
1576
|
+
* It enhances the native Error object by adding JSON serialization capabilities
|
|
1577
|
+
* and source code location information, allowing for better error reporting,
|
|
1578
|
+
* debugging, and troubleshooting. The location tracking feature helps pinpoint
|
|
1579
|
+
* exactly where in the codebase an error originated.
|
|
1580
|
+
*
|
|
1581
|
+
* @example
|
|
1582
|
+
* ```ts
|
|
1583
|
+
* // Creating a basic execution error with location information
|
|
1584
|
+
* const error = new ExecutionError(
|
|
1585
|
+
* 'Failed to process data',
|
|
1586
|
+
* { line: 42, column: 10 }
|
|
1587
|
+
* );
|
|
1588
|
+
*
|
|
1589
|
+
* // The error can be thrown or used in promise rejection
|
|
1590
|
+
* throw error;
|
|
1591
|
+
* ```
|
|
1592
|
+
*
|
|
1593
|
+
* @see InvocationLocationType
|
|
1594
|
+
*
|
|
1595
|
+
* @since 1.0.0
|
|
1596
|
+
*/
|
|
1597
|
+
declare class ExecutionError extends Error {
|
|
1598
|
+
/**
|
|
1599
|
+
* Creates a new ExecutionError instance with the specified error message and source code location
|
|
1600
|
+
*
|
|
1601
|
+
* @param message - The error message describing what went wrong
|
|
1602
|
+
*
|
|
1603
|
+
* @remarks
|
|
1604
|
+
* This constructor initializes both the standard Error message property and the location
|
|
1605
|
+
* information that helps pinpoint exactly where in the codebase the error originated. The
|
|
1606
|
+
* location parameter follows the InvocationLocationType structure, typically containing
|
|
1607
|
+
* line and column information.
|
|
1608
|
+
*
|
|
1609
|
+
* @example
|
|
1610
|
+
* ```ts
|
|
1611
|
+
* const error = new ExecutionError(
|
|
1612
|
+
* 'Failed to process input data',
|
|
1613
|
+
* { line: 42, column: 8 }
|
|
1614
|
+
* );
|
|
1615
|
+
* ```
|
|
1616
|
+
*
|
|
1617
|
+
* @since 1.0.0
|
|
1618
|
+
*/
|
|
1619
|
+
constructor(message: string);
|
|
1620
|
+
/**
|
|
1621
|
+
* Converts the error instance to a plain JavaScript object for JSON serialization.
|
|
1622
|
+
*
|
|
1623
|
+
* @returns A plain object containing all properties of the error.
|
|
1624
|
+
*
|
|
1625
|
+
* @remarks
|
|
1626
|
+
* This method enhances error serialization by ensuring all properties from the error
|
|
1627
|
+
* instance are properly included in the resulting JSON representation. The standard
|
|
1628
|
+
* `JSON.stringify()` method doesn't properly serialize Error objects by default, as
|
|
1629
|
+
* their properties are typically not enumerable.
|
|
1630
|
+
*
|
|
1631
|
+
* @example
|
|
1632
|
+
* ```ts
|
|
1633
|
+
* const error = new ExecutionError('Something went wrong');
|
|
1634
|
+
* error.code = 'ERR_INVALID_INPUT';
|
|
1635
|
+
*
|
|
1636
|
+
* // Convert to JSON
|
|
1637
|
+
* const serialized = JSON.stringify(error);
|
|
1638
|
+
* // Result includes all properties: message, name, stack, code, etc.
|
|
1639
|
+
* ```
|
|
1640
|
+
*
|
|
1641
|
+
* @since 1.0.0
|
|
1642
|
+
*/
|
|
1643
|
+
toJSON(): Record<string, unknown>;
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
/**
|
|
1647
|
+
* Defines the log verbosity levels for reporters and test execution.
|
|
1648
|
+
*
|
|
1649
|
+
* @remarks
|
|
1650
|
+
* Each level represents the minimum severity of messages that should be
|
|
1651
|
+
* captured or displayed. Higher levels include all messages from lower levels.
|
|
1652
|
+
*
|
|
1653
|
+
* @example
|
|
1654
|
+
* ```ts
|
|
1655
|
+
* if (log.level >= LogLevel.Warn) {
|
|
1656
|
+
* console.warn(log.message);
|
|
1657
|
+
* }
|
|
1658
|
+
* ```
|
|
1659
|
+
*
|
|
1660
|
+
* @since 1.0.0
|
|
1661
|
+
*/
|
|
1662
|
+
declare enum LogLevel {
|
|
1663
|
+
Silent = 0,
|
|
1664
|
+
Error = 1,
|
|
1665
|
+
Warn = 2,
|
|
1666
|
+
Info = 3,
|
|
1667
|
+
Debug = 4
|
|
1668
|
+
}
|
|
1669
|
+
/**
|
|
1670
|
+
* Defines the types of messages emitted during test execution.
|
|
1671
|
+
*
|
|
1672
|
+
* @remarks
|
|
1673
|
+
* These message types are used internally by reporters, runners,
|
|
1674
|
+
* and event emitters to categorize test events. Each type corresponds
|
|
1675
|
+
* to a specific stage or element of the test lifecycle.
|
|
1676
|
+
*
|
|
1677
|
+
* @example
|
|
1678
|
+
* ```ts
|
|
1679
|
+
* if (message.type === MessageType.Test) {
|
|
1680
|
+
* console.log('Test event received');
|
|
1681
|
+
* }
|
|
1682
|
+
* ```
|
|
1683
|
+
*
|
|
1684
|
+
* @since 1.0.0
|
|
1685
|
+
*/
|
|
1686
|
+
declare const enum MessageType {
|
|
1687
|
+
Test = 1,
|
|
1688
|
+
Describe = 2,
|
|
1689
|
+
EndSuite = 3,
|
|
1690
|
+
StartSuite = 4
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
/**
|
|
1694
|
+
* Import will remove at compile time
|
|
1695
|
+
*/
|
|
1696
|
+
/**
|
|
1697
|
+
* Emits a status update packet to the dispatcher.
|
|
1698
|
+
*
|
|
1699
|
+
* @param type - The {@link MessageType} representing the kind of status (e.g., suite start, suite end).
|
|
1700
|
+
* @param notification - The {@link EmitStatusInterface} containing ancestry and description details.
|
|
1701
|
+
*
|
|
1702
|
+
* @remarks
|
|
1703
|
+
* This function encodes the notification into a `PacketKind.Status` packet
|
|
1704
|
+
* and sends it via the global `dispatch` mechanism.
|
|
1705
|
+
*
|
|
1706
|
+
* @see MessageType
|
|
1707
|
+
* @see encodePacket
|
|
1708
|
+
* @see PacketKind.Status
|
|
1709
|
+
* @see EmitStatusInterface
|
|
1710
|
+
*
|
|
1711
|
+
* @since 1.0.0
|
|
1712
|
+
*/
|
|
1713
|
+
declare function emitStatus(type: MessageType, notification?: EmitStatusInterface): void;
|
|
1714
|
+
/**
|
|
1715
|
+
* Emits an action (event) packet to the dispatcher.
|
|
1716
|
+
*
|
|
1717
|
+
* @param type - The {@link MessageType} representing the kind of action (e.g., test end, describe end).
|
|
1718
|
+
* @param notification - The {@link EmitEventInterface} containing ancestry, description,
|
|
1719
|
+
* duration, and possible test errors.
|
|
1720
|
+
*
|
|
1721
|
+
* @remarks
|
|
1722
|
+
* - Errors are serialized with {@link serializeError} before being encoded.
|
|
1723
|
+
* - The function encodes the notification into a `PacketKind.Events` packet
|
|
1724
|
+
* and sends it via the global `dispatch` mechanism.
|
|
1725
|
+
*
|
|
1726
|
+
* @see MessageType
|
|
1727
|
+
* @see encodePacket
|
|
1728
|
+
* @see serializeError
|
|
1729
|
+
* @see PacketKind.Events
|
|
1730
|
+
* @see EmitEventInterface
|
|
1731
|
+
*
|
|
1732
|
+
* @since 1.0.0
|
|
1733
|
+
*/
|
|
1734
|
+
declare function emitEvent(type: MessageType, notification: EmitEventInterface): void;
|
|
1735
|
+
|
|
1736
|
+
/**
|
|
1737
|
+
* Import will remove at compile time
|
|
1738
|
+
*/
|
|
1739
|
+
/**
|
|
1740
|
+
* Represents the payload for emitting an event (e.g., test or describe completion).
|
|
1741
|
+
*
|
|
1742
|
+
* @remarks
|
|
1743
|
+
* Extends {@link PacketEventsInterface} but replaces:
|
|
1744
|
+
* - `errors` with a raw {@link Error} array (to be serialized before dispatch).
|
|
1745
|
+
* - `ancestry` with a string array for hierarchical test path tracking.
|
|
1746
|
+
* - `type` is omitted since it is explicitly provided when emitting.
|
|
1747
|
+
*
|
|
1748
|
+
* @example
|
|
1749
|
+
* ```ts
|
|
1750
|
+
* const event: EmitEventInterface = {
|
|
1751
|
+
* ancestry: ['MySuite', 'MyTest'],
|
|
1752
|
+
* description: 'should add numbers correctly',
|
|
1753
|
+
* duration: 42,
|
|
1754
|
+
* errors: [new Error('Expected 2 but got 3')]
|
|
1755
|
+
* };
|
|
1756
|
+
* emitEvent(MessageType.Test, event);
|
|
1757
|
+
* ```
|
|
1758
|
+
*
|
|
1759
|
+
* @see emitAction
|
|
1760
|
+
* @see PacketEventsInterface
|
|
1761
|
+
*
|
|
1762
|
+
* @since 1.0.0
|
|
1763
|
+
*/
|
|
1764
|
+
interface EmitEventInterface extends Omit<Partial<PacketEventsInterface>, 'errors' | 'ancestry' | 'type'> {
|
|
1765
|
+
errors?: Array<Error>;
|
|
1766
|
+
ancestry: Array<string>;
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Represents the payload for emitting a status update (e.g., suite start or end).
|
|
1770
|
+
*
|
|
1771
|
+
* @remarks
|
|
1772
|
+
* Extends {@link PacketStatusInterface} but replaces:
|
|
1773
|
+
* - `ancestry` with a string array for hierarchical test path tracking.
|
|
1774
|
+
* - `type` is omitted since it is explicitly provided when emitting.
|
|
1775
|
+
*
|
|
1776
|
+
* @example
|
|
1777
|
+
* ```ts
|
|
1778
|
+
* const status: EmitStatusInterface = {
|
|
1779
|
+
* ancestry: ['MySuite'],
|
|
1780
|
+
* description: 'Suite execution started'
|
|
1781
|
+
* };
|
|
1782
|
+
* emitStatus(MessageType.StartSuite, status);
|
|
1783
|
+
* ```
|
|
1784
|
+
*
|
|
1785
|
+
* @see emitStatus
|
|
1786
|
+
* @see PacketStatusInterface
|
|
1787
|
+
*
|
|
1788
|
+
* @since 1.0.0
|
|
1789
|
+
*/
|
|
1790
|
+
interface EmitStatusInterface extends Omit<Partial<PacketStatusInterface>, 'ancestry' | 'type'> {
|
|
1791
|
+
ancestry?: Array<string>;
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
/**
|
|
1795
|
+
* Import will remove at compile time
|
|
1796
|
+
*/
|
|
1797
|
+
/**
|
|
1798
|
+
* Represents the position of an invocation within a bundle file.
|
|
1799
|
+
*
|
|
1800
|
+
* @since 1.0.0
|
|
1801
|
+
*/
|
|
1802
|
+
interface PacketInvocationInterface {
|
|
1803
|
+
/**
|
|
1804
|
+
* The line number where the invocation occurs (0-based).
|
|
1805
|
+
* @since 1.0.0
|
|
1806
|
+
*/
|
|
1807
|
+
line: number;
|
|
1808
|
+
/**
|
|
1809
|
+
* The column number where the invocation occurs (0-based).
|
|
1810
|
+
* @since 1.0.0
|
|
1811
|
+
*/
|
|
1812
|
+
column: number;
|
|
1813
|
+
/**
|
|
1814
|
+
* Source file
|
|
1815
|
+
* @since 1.0.0
|
|
1816
|
+
*/
|
|
1817
|
+
source: string;
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Represents the header information for a packet sent.
|
|
1821
|
+
* @since 1.0.0
|
|
1822
|
+
*/
|
|
1823
|
+
interface PacketHeaderInterface {
|
|
1824
|
+
/**
|
|
1825
|
+
* The type of packet being sent.
|
|
1826
|
+
*
|
|
1827
|
+
* @see PacketKind
|
|
1828
|
+
* @since 1.0.0
|
|
1829
|
+
*/
|
|
1830
|
+
kind: PacketKind;
|
|
1831
|
+
/**
|
|
1832
|
+
* The unique identifier of the test suite.
|
|
1833
|
+
*
|
|
1834
|
+
* @since 1.0.0
|
|
1835
|
+
*/
|
|
1836
|
+
suiteId: string;
|
|
1837
|
+
/**
|
|
1838
|
+
* The unique identifier of the runner sending the packet.
|
|
1839
|
+
*
|
|
1840
|
+
* @since 1.0.0
|
|
1841
|
+
*/
|
|
1842
|
+
runnerId: string;
|
|
1843
|
+
/**
|
|
1844
|
+
* The timestamp when the packet was created, in ISO string format.
|
|
1845
|
+
*
|
|
1846
|
+
* @since 1.0.0
|
|
1847
|
+
*/
|
|
1848
|
+
timestamp: string;
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* Represents a log entry generated during test execution, similar to console output.
|
|
1852
|
+
*
|
|
1853
|
+
* @remarks
|
|
1854
|
+
* Used to capture messages like `console.log`, `console.error`, `console.info`, etc.,
|
|
1855
|
+
* along with metadata about where in the test hierarchy and source code the log originated.
|
|
1856
|
+
*
|
|
1857
|
+
* @since 1.0.0
|
|
1858
|
+
*/
|
|
1859
|
+
interface PacketLogInterface {
|
|
1860
|
+
/**
|
|
1861
|
+
* The severity level of the log entry.
|
|
1862
|
+
* Typically maps to console levels such as info, warn, debug, or error.
|
|
1863
|
+
*
|
|
1864
|
+
* @since 1.0.0
|
|
1865
|
+
*/
|
|
1866
|
+
level: number;
|
|
1867
|
+
/**
|
|
1868
|
+
* The content of the log message.
|
|
1869
|
+
* @since 1.0.0
|
|
1870
|
+
*/
|
|
1871
|
+
message: string;
|
|
1872
|
+
/**
|
|
1873
|
+
* The ancestry path of the test or describe block that generated this log.
|
|
1874
|
+
* @since 1.0.0
|
|
1875
|
+
*/
|
|
1876
|
+
ancestry: string;
|
|
1877
|
+
/**
|
|
1878
|
+
* The location in the source code where the log was generated.
|
|
1879
|
+
*
|
|
1880
|
+
* @see PacketInvocationInterface
|
|
1881
|
+
* @since 1.0.0
|
|
1882
|
+
*/
|
|
1883
|
+
invocation: PacketInvocationInterface;
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Represents a fatal error in a test suite.
|
|
1887
|
+
*
|
|
1888
|
+
* @remarks
|
|
1889
|
+
* This error occurs at the suite level and is **not** associated with any individual test,
|
|
1890
|
+
* describe block, or hook (e.g., before/after hooks). It indicates a failure
|
|
1891
|
+
* that prevents the suite from running normally.
|
|
1892
|
+
*
|
|
1893
|
+
* @since 1.0.0
|
|
1894
|
+
*/
|
|
1895
|
+
interface PacketErrorInterface {
|
|
1896
|
+
/**
|
|
1897
|
+
* The serialized error describing the fatal issue in the suite.
|
|
1898
|
+
* @since 1.0.0
|
|
1899
|
+
*/
|
|
1900
|
+
error: string;
|
|
1901
|
+
}
|
|
1902
|
+
/**
|
|
1903
|
+
* Represents an event emitted when a test or describe block starts or updates during execution.
|
|
1904
|
+
*
|
|
1905
|
+
* @remarks
|
|
1906
|
+
* Used to track the lifecycle of individual tests or describe blocks, including
|
|
1907
|
+
* whether they are skipped, marked TODO, or have errors.
|
|
1908
|
+
* This does **not** include suite-level fatal errors.
|
|
1909
|
+
*
|
|
1910
|
+
* @since 1.0.0
|
|
1911
|
+
*/
|
|
1912
|
+
interface PacketStatusInterface {
|
|
1913
|
+
/**
|
|
1914
|
+
* Indicates whether the status is for a test or a describe block.
|
|
1915
|
+
* @since 1.0.0
|
|
1916
|
+
*/
|
|
1917
|
+
type: number;
|
|
1918
|
+
/**
|
|
1919
|
+
* Indicates if the test or describe block is marked as TODO.
|
|
1920
|
+
* @since 1.0.0
|
|
1921
|
+
*/
|
|
1922
|
+
todo: boolean;
|
|
1923
|
+
/**
|
|
1924
|
+
* Indicates if the test or describe block was skipped.
|
|
1925
|
+
* @since 1.0.0
|
|
1926
|
+
*/
|
|
1927
|
+
skipped: boolean;
|
|
1928
|
+
/**
|
|
1929
|
+
* Duration of the test or describe block in milliseconds, if available.
|
|
1930
|
+
* @since 1.0.0
|
|
1931
|
+
*/
|
|
1932
|
+
duration: number;
|
|
1933
|
+
/**
|
|
1934
|
+
* The ancestry path of the test or describe block.
|
|
1935
|
+
* @since 1.0.0
|
|
1936
|
+
*/
|
|
1937
|
+
ancestry: string;
|
|
1938
|
+
/**
|
|
1939
|
+
* Human-readable description of the test or describe block.
|
|
1940
|
+
* @since 1.0.0
|
|
1941
|
+
*/
|
|
1942
|
+
description: string;
|
|
1943
|
+
}
|
|
1944
|
+
/**
|
|
1945
|
+
* Represents an event emitted for a test or describe block during execution.
|
|
1946
|
+
*
|
|
1947
|
+
* @remarks
|
|
1948
|
+
* Used to track the lifecycle of individual tests or describe blocks, such as finish.
|
|
1949
|
+
* This interface is **not** for suite-level fatal errors.
|
|
1950
|
+
*
|
|
1951
|
+
* @since 1.0.0
|
|
1952
|
+
*/
|
|
1953
|
+
interface PacketEventsInterface {
|
|
1954
|
+
/**
|
|
1955
|
+
* Indicates the type of event and whether it corresponds to a test or a describe block.
|
|
1956
|
+
* @since 1.0.0
|
|
1957
|
+
*/
|
|
1958
|
+
type: number;
|
|
1959
|
+
/**
|
|
1960
|
+
* Error message associated with the test or describe block, if any.
|
|
1961
|
+
* @since 1.0.0
|
|
1962
|
+
*/
|
|
1963
|
+
errors: string;
|
|
1964
|
+
/**
|
|
1965
|
+
* Duration of the test or describe block in milliseconds.
|
|
1966
|
+
* @since 1.0.0
|
|
1967
|
+
*/
|
|
1968
|
+
duration: number;
|
|
1969
|
+
/**
|
|
1970
|
+
* The ancestry path of the test or describe block.
|
|
1971
|
+
* @since 1.0.0
|
|
1972
|
+
*/
|
|
1973
|
+
ancestry: string;
|
|
1974
|
+
/**
|
|
1975
|
+
* Human-readable description of the test or describe block.
|
|
1976
|
+
* @since 1.0.0
|
|
1977
|
+
*/
|
|
1978
|
+
description: string;
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
/**
|
|
1982
|
+
* Import will remove at compile time
|
|
1983
|
+
*/
|
|
1984
|
+
/**
|
|
1985
|
+
* Defines the different kinds of packets that can be transmitted or received.
|
|
1986
|
+
*
|
|
1987
|
+
* @remarks
|
|
1988
|
+
* Each packet kind corresponds to a specific type of event or message in the test framework:
|
|
1989
|
+
* - `Log`: Console or logging messages
|
|
1990
|
+
* - `Error`: Suite-level fatal errors
|
|
1991
|
+
* - `Status`: Test or describe start events, including `skipped` and `todo` flags
|
|
1992
|
+
* - `Events`: Test or describe end events, only for completed tests/describes (no skipped or TODO)
|
|
1993
|
+
*
|
|
1994
|
+
* @since 1.0.0
|
|
1995
|
+
*/
|
|
1996
|
+
declare const enum PacketKind {
|
|
1997
|
+
/**
|
|
1998
|
+
* Represents a log message packet, e.g., `console.log`, `console.error`.
|
|
1999
|
+
* @since 1.0.0
|
|
2000
|
+
*/
|
|
2001
|
+
Log = 1,
|
|
2002
|
+
/**
|
|
2003
|
+
* Represents a fatal suite-level error.
|
|
2004
|
+
* Not associated with any test, describe, or hook.
|
|
2005
|
+
* @since 1.0.0
|
|
2006
|
+
*/
|
|
2007
|
+
Error = 2,
|
|
2008
|
+
/**
|
|
2009
|
+
* Represents a status packet for test or describe start events.
|
|
2010
|
+
*
|
|
2011
|
+
* @remarks
|
|
2012
|
+
* Includes flags for:
|
|
2013
|
+
* - `skipped`: Whether the test or describe was skipped
|
|
2014
|
+
* - `todo`: Whether the test is marked as TODO
|
|
2015
|
+
*
|
|
2016
|
+
* @since 1.0.0
|
|
2017
|
+
*/
|
|
2018
|
+
Status = 3,
|
|
2019
|
+
/**
|
|
2020
|
+
* Represents an event packet for test or describe end events.
|
|
2021
|
+
*
|
|
2022
|
+
* @remarks
|
|
2023
|
+
* Only includes completed tests/describes; skipped or TODO tests are not included.
|
|
2024
|
+
* Contains information such as `passed` and `duration`.
|
|
2025
|
+
*
|
|
2026
|
+
* @since 1.0.0
|
|
2027
|
+
*/
|
|
2028
|
+
Events = 4
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* Maps each {@link PacketKind} to its corresponding {@link Struct} schema for serialization/deserialization.
|
|
2032
|
+
*
|
|
2033
|
+
* @remarks
|
|
2034
|
+
* This object allows encoding and decoding packets of different kinds using
|
|
2035
|
+
* their respective `xStruct` schemas. Use `PacketSchemas[PacketKind.Log]` to
|
|
2036
|
+
* get the schema for log packets, etc.
|
|
2037
|
+
*
|
|
2038
|
+
* @see PacketKind
|
|
2039
|
+
* @see Struct
|
|
2040
|
+
*
|
|
2041
|
+
* @since 1.0.0
|
|
2042
|
+
*/
|
|
2043
|
+
declare const PacketSchemas: Record<PacketKind, Struct>;
|
|
2044
|
+
|
|
2045
|
+
/**
|
|
2046
|
+
* Import will remove at compile time
|
|
2047
|
+
*/
|
|
2048
|
+
/**
|
|
2049
|
+
* Imports
|
|
2050
|
+
*/
|
|
2051
|
+
/**
|
|
2052
|
+
* Schema for serializing and deserializing {@link PacketInvocationInterface} data.
|
|
2053
|
+
*
|
|
2054
|
+
* @remarks
|
|
2055
|
+
* Represents the location in source code where a test, describe, or log originates.
|
|
2056
|
+
*
|
|
2057
|
+
* @since 1.0.0
|
|
2058
|
+
*/
|
|
2059
|
+
declare const invocationSchema: Struct<PacketInvocationInterface>;
|
|
2060
|
+
/**
|
|
2061
|
+
* Schema for serializing and deserializing {@link PacketHeaderInterface} data.
|
|
2062
|
+
*
|
|
2063
|
+
* @remarks
|
|
2064
|
+
* Contains metadata for each packet, including kind, suite and runner identifiers, and timestamp.
|
|
2065
|
+
*
|
|
2066
|
+
* @since 1.0.0
|
|
2067
|
+
*/
|
|
2068
|
+
declare const headerSchema: Struct<PacketHeaderInterface>;
|
|
2069
|
+
/**
|
|
2070
|
+
* Schema for serializing and deserializing {@link PacketLogInterface} data.
|
|
2071
|
+
*
|
|
2072
|
+
* @remarks
|
|
2073
|
+
* Used for console-like logs emitted by tests or describes, including message, level, ancestry, and invocation.
|
|
2074
|
+
*
|
|
2075
|
+
* @since 1.0.0
|
|
2076
|
+
*/
|
|
2077
|
+
declare const logSchema: Struct<PacketLogInterface>;
|
|
2078
|
+
/**
|
|
2079
|
+
* Schema for serializing and deserializing {@link PacketErrorInterface} data.
|
|
2080
|
+
*
|
|
2081
|
+
* @remarks
|
|
2082
|
+
* Represents suite-level fatal errors that are not tied to specific tests or describes.
|
|
2083
|
+
*
|
|
2084
|
+
* @since 1.0.0
|
|
2085
|
+
*/
|
|
2086
|
+
declare const errorSchema: Struct<PacketErrorInterface>;
|
|
2087
|
+
/**
|
|
2088
|
+
* Schema for serializing and deserializing {@link PacketStatusInterface} data.
|
|
2089
|
+
*
|
|
2090
|
+
* @remarks
|
|
2091
|
+
* Represents status updates for individual tests or describe blocks,
|
|
2092
|
+
* including whether it is TODO, skipped, and its ancestry and description.
|
|
2093
|
+
*
|
|
2094
|
+
* @since 1.0.0
|
|
2095
|
+
*/
|
|
2096
|
+
declare const statusSchema: Struct<PacketStatusInterface>;
|
|
2097
|
+
/**
|
|
2098
|
+
* Schema for serializing and deserializing {@link PacketEventsInterface} data.
|
|
2099
|
+
*
|
|
2100
|
+
* @remarks
|
|
2101
|
+
* Represents events emitted during the test or describe execution,
|
|
2102
|
+
* including pass/fail status, duration, error messages, ancestry, and invocation.
|
|
2103
|
+
*
|
|
2104
|
+
* @since 1.0.0
|
|
2105
|
+
*/
|
|
2106
|
+
declare const eventsSchema: Struct<PacketEventsInterface>;
|
|
2107
|
+
|
|
2108
|
+
/**
|
|
2109
|
+
* Import will remove at compile time
|
|
2110
|
+
*/
|
|
2111
|
+
/**
|
|
2112
|
+
* Exports constants
|
|
2113
|
+
*/
|
|
2114
|
+
/**
|
|
2115
|
+
* Exports interfaces
|
|
2116
|
+
*/
|
|
2117
|
+
/**
|
|
2118
|
+
* Encodes a packet of a given kind into a `Buffer`.
|
|
2119
|
+
*
|
|
2120
|
+
* @template T - The packet kind (`PacketKind.Log`, `PacketKind.Error`, `PacketKind.Status`, or `PacketKind.Events`)
|
|
2121
|
+
*
|
|
2122
|
+
* @param kind - The type of packet to encode
|
|
2123
|
+
* @param data - Partial data matching the packet type; will be combined with header information
|
|
2124
|
+
*
|
|
2125
|
+
* @returns A `Buffer` containing the serialized packet
|
|
2126
|
+
*
|
|
2127
|
+
* @throws Error if the provided `kind` does not correspond to a known packet schema
|
|
2128
|
+
*
|
|
2129
|
+
* @remarks
|
|
2130
|
+
* This function combines a header and the payload according to the packet kind.
|
|
2131
|
+
* The header includes the suite ID, runner ID, and a timestamp.
|
|
2132
|
+
*
|
|
2133
|
+
* @since 1.0.0
|
|
2134
|
+
*/
|
|
2135
|
+
declare function encodePacket<T extends PacketKind>(kind: T, data: Partial<PacketPayloadMapType[T]>): Buffer;
|
|
2136
|
+
/**
|
|
2137
|
+
* Decodes a packet from a `Buffer` into its corresponding object representation.
|
|
2138
|
+
*
|
|
2139
|
+
* @template T - The expected packet kind (`PacketKind.Log`, `PacketKind.Error`, `PacketKind.Status`, or `PacketKind.Events`)
|
|
2140
|
+
*
|
|
2141
|
+
* @param buffer - The buffer containing the encoded packet
|
|
2142
|
+
* @returns The decoded packet object, combining the header and payload fields
|
|
2143
|
+
*
|
|
2144
|
+
* @throws Error if the packet kind is unknown or invalid
|
|
2145
|
+
*
|
|
2146
|
+
* @remarks
|
|
2147
|
+
* Decodes both the header and payload based on the packet kind.
|
|
2148
|
+
*
|
|
2149
|
+
* @since 1.0.0
|
|
2150
|
+
*/
|
|
2151
|
+
declare function decodePacket<T extends PacketKind>(buffer: Buffer): DecodedPacketType<T>;
|
|
2152
|
+
/**
|
|
2153
|
+
* Encodes an {@link Error} instance into a binary buffer following the packet schema.
|
|
2154
|
+
*
|
|
2155
|
+
* @param error - The error object to be serialized and encoded.
|
|
2156
|
+
* @param suiteId - Identifier of the suite where the error occurred.
|
|
2157
|
+
* @param runnerId - Identifier of the runner reporting the error.
|
|
2158
|
+
*
|
|
2159
|
+
* @returns A {@link Buffer} containing the encoded error packet, ready for transmission.
|
|
2160
|
+
*
|
|
2161
|
+
* @remarks
|
|
2162
|
+
* The function creates two binary sections:
|
|
2163
|
+
* - A **header**, describing the packet kind (`Error`), suite ID, runner ID, and timestamp.
|
|
2164
|
+
* - A **data buffer**, holding the serialized error details in JSON format.
|
|
2165
|
+
*
|
|
2166
|
+
* These two sections are concatenated into a single buffer.
|
|
2167
|
+
*
|
|
2168
|
+
* @example
|
|
2169
|
+
* ```ts
|
|
2170
|
+
* try {
|
|
2171
|
+
* throw new Error("Test failed");
|
|
2172
|
+
* } catch (err) {
|
|
2173
|
+
* const buffer = encodeErrorSchema(err, "suite-123", "runner-456");
|
|
2174
|
+
* socket.send(buffer); // transmit over a transport channel
|
|
2175
|
+
* }
|
|
2176
|
+
* ```
|
|
2177
|
+
*
|
|
2178
|
+
* @see serializeError
|
|
2179
|
+
* @see PacketKind.Error
|
|
2180
|
+
*
|
|
2181
|
+
* @since 1.0.0
|
|
2182
|
+
*/
|
|
2183
|
+
declare function encodeErrorSchema(error: Error, suiteId: string, runnerId: string): Buffer;
|
|
2184
|
+
|
|
2185
|
+
/**
|
|
2186
|
+
* Import will remove at compile time
|
|
2187
|
+
*/
|
|
2188
|
+
/**
|
|
2189
|
+
* Maps each {@link PacketKind} to its corresponding packet payload interface.
|
|
2190
|
+
*
|
|
2191
|
+
* @remarks
|
|
2192
|
+
* This type is used internally to associate packet kinds with their structured payloads.
|
|
2193
|
+
* For example, a `PacketKind.Log` corresponds to a {@link PacketLogInterface} payload.
|
|
2194
|
+
*
|
|
2195
|
+
* @since 1.0.0
|
|
2196
|
+
*/
|
|
2197
|
+
type PacketPayloadMapType = {
|
|
2198
|
+
[PacketKind.Log]: PacketLogInterface;
|
|
2199
|
+
[PacketKind.Error]: PacketErrorInterface;
|
|
2200
|
+
[PacketKind.Status]: PacketStatusInterface;
|
|
2201
|
+
[PacketKind.Events]: PacketEventsInterface;
|
|
2202
|
+
};
|
|
2203
|
+
/**
|
|
2204
|
+
* Represents a fully decoded packet, combining its payload and the standard packet header.
|
|
2205
|
+
*
|
|
2206
|
+
* @template T - The {@link PacketKind} indicating the type of payload
|
|
2207
|
+
*
|
|
2208
|
+
* @remarks
|
|
2209
|
+
* This type merges the payload interface corresponding to `T` from {@link PacketPayloadMapType}
|
|
2210
|
+
* with {@link PacketHeaderInterface}, so every decoded packet contains both its header and payload.
|
|
2211
|
+
*
|
|
2212
|
+
* @example
|
|
2213
|
+
* ```ts
|
|
2214
|
+
* const decodedLog: DecodedPacketType<PacketKind.Log> = {
|
|
2215
|
+
* kind: PacketKind.Log,
|
|
2216
|
+
* suiteId: 'suite1',
|
|
2217
|
+
* runnerId: 'runner1',
|
|
2218
|
+
* timestamp: '2025-09-02T08:00:00Z',
|
|
2219
|
+
* level: 1,
|
|
2220
|
+
* message: 'Test log message',
|
|
2221
|
+
* ancestry: 'root.describe.test',
|
|
2222
|
+
* invocation: { line: 12, column: 5 }
|
|
2223
|
+
* };
|
|
2224
|
+
* ```
|
|
2225
|
+
*
|
|
2226
|
+
* @since 1.0.0
|
|
2227
|
+
*/
|
|
2228
|
+
type DecodedPacketType<T extends PacketKind> = PacketPayloadMapType[T] & PacketHeaderInterface;
|
|
2229
|
+
|
|
2230
|
+
/**
|
|
2231
|
+
* Represents the types of lifecycle hooks that can be used in testing frameworks or similar systems.
|
|
2232
|
+
*
|
|
2233
|
+
* @remarks
|
|
2234
|
+
* The `HookType` enum defines constants for various lifecycle stages
|
|
2235
|
+
* that are commonly used to implement setup and teardown logic
|
|
2236
|
+
* in testing or other procedural workflows.
|
|
2237
|
+
*
|
|
2238
|
+
* @since 1.0.0
|
|
2239
|
+
*/
|
|
2240
|
+
declare const enum HookType {
|
|
2241
|
+
AFTER_ALL = "afterAll",
|
|
2242
|
+
BEFORE_ALL = "beforeAll",
|
|
2243
|
+
AFTER_EACH = "afterEach",
|
|
2244
|
+
BEFORE_EACH = "beforeEach"
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
/**
|
|
2248
|
+
* Enum representing the types of test execution methods available.
|
|
2249
|
+
*
|
|
2250
|
+
* @remarks
|
|
2251
|
+
* This enum defines three different modes of executing tests:
|
|
2252
|
+
* - `SYNC`: Synchronous execution, where tests are executed in sequence.
|
|
2253
|
+
* - `ASYNC`: Asynchronous execution, where tests are executed using asynchronous logic.
|
|
2254
|
+
* - `CALLBACK`: Callback-based execution, where a callback function is used to signal completion.
|
|
2255
|
+
*
|
|
2256
|
+
* @since 1.0.0
|
|
2257
|
+
*/
|
|
2258
|
+
declare const enum TestExecutionType {
|
|
2259
|
+
SYNC = "SYNC",
|
|
2260
|
+
ASYNC = "ASYNC",
|
|
2261
|
+
CALLBACK = "CALLBACK"
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
/**
|
|
2265
|
+
* Exports
|
|
2266
|
+
*/
|
|
2267
|
+
|
|
2268
|
+
/**
|
|
2269
|
+
* Import will remove at compile time
|
|
2270
|
+
*/
|
|
2271
|
+
/**
|
|
2272
|
+
* Represents a test suite container that manages test execution flow and hierarchy
|
|
2273
|
+
*
|
|
2274
|
+
* @template T - Context type for test execution
|
|
2275
|
+
* @param description - The description of the test suite
|
|
2276
|
+
* @param describeFlags - Configuration options for the test suite
|
|
2277
|
+
* @returns A new DescribeModel instance that can contain tests and nested suites
|
|
2278
|
+
*
|
|
2279
|
+
* @remarks
|
|
2280
|
+
* The DescribeModel manages the execution lifecycle of tests, including
|
|
2281
|
+
* - Maintaining parent-child relationships between test suites
|
|
2282
|
+
* - Executing hooks in the correct order (beforeAll, beforeEach, afterEach, afterAll)
|
|
2283
|
+
* - Tracking test execution status and reporting results
|
|
2284
|
+
* - Supporting test filtering with skip/only options
|
|
2285
|
+
*
|
|
2286
|
+
* @example
|
|
2287
|
+
* ```ts
|
|
2288
|
+
* const rootSuite = new DescribeModel('Root suite');
|
|
2289
|
+
* const testCase = new TestModel('should work', () => {
|
|
2290
|
+
* expect(true).toBe(true);
|
|
2291
|
+
* });
|
|
2292
|
+
* rootSuite.addTest(testCase);
|
|
2293
|
+
* await rootSuite.run({});
|
|
2294
|
+
* ```
|
|
2295
|
+
*
|
|
2296
|
+
* @see TestModel
|
|
2297
|
+
* @see HookModel
|
|
2298
|
+
* @see DescribeHooksInterface
|
|
2299
|
+
*
|
|
2300
|
+
* @since 1.0.0
|
|
2301
|
+
*/
|
|
2302
|
+
declare class DescribeModel {
|
|
2303
|
+
readonly description: string;
|
|
2304
|
+
private readonly describeOptions;
|
|
2305
|
+
/**
|
|
2306
|
+
* Array containing the hierarchical path of parent describe blocks for this test suite
|
|
2307
|
+
*
|
|
2308
|
+
* @since 1.0.0
|
|
2309
|
+
*/
|
|
2310
|
+
readonly ancestry: string[];
|
|
2311
|
+
/**
|
|
2312
|
+
* Creates a new test suite with the specified description and options
|
|
2313
|
+
*
|
|
2314
|
+
* @param description - Human-readable description of the test suite
|
|
2315
|
+
* @param describeOptions - Configuration options to control test suite execution
|
|
2316
|
+
*
|
|
2317
|
+
* @since 1.0.0
|
|
2318
|
+
*/
|
|
2319
|
+
constructor(description?: string, describeOptions?: DescribeOptionsInterface);
|
|
2320
|
+
/**
|
|
2321
|
+
* Retrieves the execution configuration options for this test suite
|
|
2322
|
+
*
|
|
2323
|
+
* @returns Test suite configuration options
|
|
2324
|
+
*
|
|
2325
|
+
* @since 1.0.0
|
|
2326
|
+
*/
|
|
2327
|
+
get options(): DescribeOptionsInterface;
|
|
2328
|
+
/**
|
|
2329
|
+
* Gets all tests in this suite, including tests from nested suites
|
|
2330
|
+
*
|
|
2331
|
+
* @returns Flattened array of all tests in this suite and its children
|
|
2332
|
+
*
|
|
2333
|
+
* @remarks
|
|
2334
|
+
* Recursively collects and flattens all test cases from this suite and any nested suites
|
|
2335
|
+
*
|
|
2336
|
+
* @since 1.0.0
|
|
2337
|
+
*/
|
|
2338
|
+
get tests(): TestModel[];
|
|
2339
|
+
/**
|
|
2340
|
+
* Registers a hook function to be executed at a specific point in the test lifecycle
|
|
2341
|
+
*
|
|
2342
|
+
* @param type - The lifecycle point at which to execute the hook
|
|
2343
|
+
* @param hook - The hook function model to register
|
|
2344
|
+
*
|
|
2345
|
+
* @throws Error - If an invalid hook type is provided
|
|
2346
|
+
*
|
|
2347
|
+
* @since 1.0.0
|
|
2348
|
+
*/
|
|
2349
|
+
addHook(type: HookType, hook: HookModel): void;
|
|
2350
|
+
/**
|
|
2351
|
+
* Adds a test case to this test suite
|
|
2352
|
+
*
|
|
2353
|
+
* @param test - The test model to add to the suite
|
|
2354
|
+
*
|
|
2355
|
+
* @remarks
|
|
2356
|
+
* When adding a test, this method automatically:
|
|
2357
|
+
* - Updates the test's ancestry to include this suite
|
|
2358
|
+
* - Propagates execution options (skip/only) from the suite to the test
|
|
2359
|
+
*
|
|
2360
|
+
* @since 1.0.0
|
|
2361
|
+
*/
|
|
2362
|
+
addTest(test: TestModel): void;
|
|
2363
|
+
/**
|
|
2364
|
+
* Adds a nested test describe to this test describe
|
|
2365
|
+
*
|
|
2366
|
+
* @param describe - The child test describe to add
|
|
2367
|
+
*
|
|
2368
|
+
* @remarks
|
|
2369
|
+
* When adding nested describe, this method automatically:
|
|
2370
|
+
* - Sets up the parent-child relationship
|
|
2371
|
+
* - Propagates execution options and ancestry information from parent to child
|
|
2372
|
+
*
|
|
2373
|
+
* @since 1.0.0
|
|
2374
|
+
*/
|
|
2375
|
+
addDescribe(describe: DescribeModel): void;
|
|
2376
|
+
/**
|
|
2377
|
+
* Executes the test suite and all contained tests
|
|
2378
|
+
*
|
|
2379
|
+
* @param context - The test execution context containing shared state
|
|
2380
|
+
* @returns Promise that resolves when all tests in the suite complete
|
|
2381
|
+
*
|
|
2382
|
+
* @remarks
|
|
2383
|
+
* Execution follows this order:
|
|
2384
|
+
* 1. Check if the suite is marked as skipped
|
|
2385
|
+
* 2. Execute all beforeAll hooks
|
|
2386
|
+
* 3. Run all tests in this suite
|
|
2387
|
+
* 4. Run all nested test suites
|
|
2388
|
+
* 5. Execute all afterAll hooks
|
|
2389
|
+
* If any step fails, the entire suite is marked as failed
|
|
2390
|
+
*
|
|
2391
|
+
* @example
|
|
2392
|
+
* ```ts
|
|
2393
|
+
* const suite = new DescribeModel('My test suite');
|
|
2394
|
+
* const context = createTestContext();
|
|
2395
|
+
* await suite.run(context);
|
|
2396
|
+
* ```
|
|
2397
|
+
*
|
|
2398
|
+
* @since 1.0.0
|
|
2399
|
+
*/
|
|
2400
|
+
run(context: ContextType<ContextInterface>): Promise<void>;
|
|
2401
|
+
/**
|
|
2402
|
+
* Inherits properties from a parent test suite
|
|
2403
|
+
*
|
|
2404
|
+
* @param parent - The parent test describe to inherit from
|
|
2405
|
+
*
|
|
2406
|
+
* @remarks
|
|
2407
|
+
* This method performs the following inheritance operations:
|
|
2408
|
+
* - Adds parent's ancestry chain to this suite's ancestry
|
|
2409
|
+
* - Propagates the 'skip' flag (if parent is skipped, child is skipped)
|
|
2410
|
+
* - Propagates the 'only' flag if the parent has it set
|
|
2411
|
+
*
|
|
2412
|
+
* @since 1.0.0
|
|
2413
|
+
*/
|
|
2414
|
+
inheritFromParentDescribe(parent: DescribeModel): void;
|
|
2415
|
+
private notifyDescribeStatus;
|
|
2416
|
+
private notifyDescribeAction;
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
/**
|
|
2420
|
+
* Import will remove at compile time
|
|
2421
|
+
*/
|
|
2422
|
+
/**
|
|
2423
|
+
* Creates a logging function that formats and dispatches log messages with timestamps
|
|
2424
|
+
*
|
|
2425
|
+
* @template T - Console method type that determines the log level
|
|
2426
|
+
* @param type - The type of console method (log, info, warn, error) to create a handler for
|
|
2427
|
+
* @returns A function that accepts any number of arguments, formats them, and dispatches a log event
|
|
2428
|
+
*
|
|
2429
|
+
* @throws TypeError - When invalid arguments are provided to the returned function
|
|
2430
|
+
*
|
|
2431
|
+
* @remarks
|
|
2432
|
+
* This function acts as a factory for creating specialized logging handlers. Each handler:
|
|
2433
|
+
* - Timestamps the log entry with ISO format
|
|
2434
|
+
* - Formats all arguments into string representations
|
|
2435
|
+
* - Dispatches the log event with the appropriate log level
|
|
2436
|
+
* - Maintains a consistent format for all log messages
|
|
2437
|
+
*
|
|
2438
|
+
* @example
|
|
2439
|
+
* ```ts
|
|
2440
|
+
* const logInfo = createLogHandler('info');
|
|
2441
|
+
* logInfo('User', user.id, 'logged in successfully');
|
|
2442
|
+
* // Dispatches a log event with level: 'INFO', formatted arguments, and timestamp
|
|
2443
|
+
* ```
|
|
2444
|
+
*
|
|
2445
|
+
* @see formatValue
|
|
2446
|
+
* @see dispatch
|
|
2447
|
+
* @see SchemaType
|
|
2448
|
+
* @see LogLevel
|
|
2449
|
+
*
|
|
2450
|
+
* @since 1.0.0
|
|
2451
|
+
*/
|
|
2452
|
+
declare function createLogHandler(type: keyof typeof LogLevel): (...args: unknown[]) => void;
|
|
2453
|
+
/**
|
|
2454
|
+
* Standard log level function for general purpose logging
|
|
2455
|
+
*
|
|
2456
|
+
* @see createLogHandler
|
|
2457
|
+
* @see LogLevel
|
|
2458
|
+
*
|
|
2459
|
+
* @since 1.0.0
|
|
2460
|
+
*/
|
|
2461
|
+
declare const log: (...args: unknown[]) => void;
|
|
2462
|
+
/**
|
|
2463
|
+
* Informational log function for highlighting noteworthy application events
|
|
2464
|
+
*
|
|
2465
|
+
* @see createLogHandler
|
|
2466
|
+
* @see LogLevel
|
|
2467
|
+
*
|
|
2468
|
+
* @since 1.0.0
|
|
2469
|
+
*/
|
|
2470
|
+
declare const info: (...args: unknown[]) => void;
|
|
2471
|
+
/**
|
|
2472
|
+
* Warning log function for potential issues that aren't errors
|
|
2473
|
+
*
|
|
2474
|
+
* @see createLogHandler
|
|
2475
|
+
* @see LogLevel
|
|
2476
|
+
*
|
|
2477
|
+
* @since 1.0.0
|
|
2478
|
+
*/
|
|
2479
|
+
declare const warn: (...args: unknown[]) => void;
|
|
2480
|
+
/**
|
|
2481
|
+
* Error log function for reporting application errors and exceptions
|
|
2482
|
+
*
|
|
2483
|
+
* @see createLogHandler
|
|
2484
|
+
* @see LogLevel
|
|
2485
|
+
*
|
|
2486
|
+
* @since 1.0.0
|
|
2487
|
+
*/
|
|
2488
|
+
declare const error: (...args: unknown[]) => void;
|
|
2489
|
+
/**
|
|
2490
|
+
* Debug log function for detailed diagnostic information
|
|
2491
|
+
*
|
|
2492
|
+
* @see createLogHandler
|
|
2493
|
+
* @see LogLevel
|
|
2494
|
+
*
|
|
2495
|
+
* @since 1.0.0
|
|
2496
|
+
*/
|
|
2497
|
+
declare const debug: (...args: unknown[]) => void;
|
|
2498
|
+
|
|
2499
|
+
/**
|
|
2500
|
+
* Import will remove at compile time
|
|
2501
|
+
*/
|
|
2502
|
+
/**
|
|
2503
|
+
* Retrieves the location in the source code where this function was invoked.
|
|
2504
|
+
*
|
|
2505
|
+
* @param position - The stack frame index to inspect (default is 3). This allows skipping
|
|
2506
|
+
* internal frames to reach the actual caller.
|
|
2507
|
+
* @returns A {@link PacketInvocationInterface} containing `line`, `column`, and `source` file
|
|
2508
|
+
* of the invocation, or `undefined` if the location could not be determined.
|
|
2509
|
+
*
|
|
2510
|
+
* @remarks
|
|
2511
|
+
* This function generates a new `Error` to capture the stack trace, parses it using
|
|
2512
|
+
* {@link parseErrorStack}, and returns the requested frame as a structured object.
|
|
2513
|
+
* It is useful for logging or reporting the exact location of a call within test suites
|
|
2514
|
+
* or framework code.
|
|
2515
|
+
*
|
|
2516
|
+
* @see parseErrorStack
|
|
2517
|
+
* @see PacketInvocationInterface
|
|
2518
|
+
*
|
|
2519
|
+
* @since 1.0.0
|
|
2520
|
+
*/
|
|
2521
|
+
declare function getInvocationLocation(position?: number): PacketInvocationInterface | undefined;
|
|
2522
|
+
/**
|
|
2523
|
+
* Returns a native-style stack trace string,
|
|
2524
|
+
* trimmed to start at the specified frame index.
|
|
2525
|
+
*
|
|
2526
|
+
* @remarks
|
|
2527
|
+
* The first line (error name/message) is preserved,
|
|
2528
|
+
* while the following lines are sliced starting from `position`.
|
|
2529
|
+
*
|
|
2530
|
+
* @param position - Index of the first stack frame to include.
|
|
2531
|
+
* Defaults to `2` to skip this helper and its caller.
|
|
2532
|
+
*
|
|
2533
|
+
* @returns A string identical in format to `Error.stack`,
|
|
2534
|
+
* or an empty string if the stack is unavailable.
|
|
2535
|
+
*
|
|
2536
|
+
* @example
|
|
2537
|
+
* ```ts
|
|
2538
|
+
* console.log(getTrimmedStackString(2));
|
|
2539
|
+
* ```
|
|
2540
|
+
*
|
|
2541
|
+
* @since 3.1.0
|
|
2542
|
+
*/
|
|
2543
|
+
declare function getTrimmedStackString(position?: number): string;
|
|
2544
|
+
|
|
2545
|
+
/**
|
|
2546
|
+
* Import will remove at compile time
|
|
2547
|
+
*/
|
|
2548
|
+
/**
|
|
2549
|
+
* Imports
|
|
2550
|
+
*/
|
|
2551
|
+
/**
|
|
2552
|
+
* Intercepts and mocks the property descriptor of a specified property on a target object.
|
|
2553
|
+
*
|
|
2554
|
+
* @template Target - The target object type.
|
|
2555
|
+
* @template Key - The key of the property to be mocked.
|
|
2556
|
+
*
|
|
2557
|
+
* @param target - The object whose property descriptor is being intercepted and mocked.
|
|
2558
|
+
* @param key - The property key on the target object to spy on.
|
|
2559
|
+
* @return A `MockState` instance that provides control over the mocked property and its interactions.
|
|
2560
|
+
*
|
|
2561
|
+
* @remarks
|
|
2562
|
+
* This function replaces the property's getter and setter to allow interception and testing of their behavior.
|
|
2563
|
+
* A `MockState` instance is returned to control and observes mocked behavior.
|
|
2564
|
+
*
|
|
2565
|
+
* @since 1.0.0
|
|
2566
|
+
*/
|
|
2567
|
+
declare function spyOnDescriptorProperty<Target, Key extends keyof Target>(target: Target, key: Key): MockState<Target[Key], []>;
|
|
2568
|
+
/**
|
|
2569
|
+
* Creates a spy on a specified static method or static property of a target class (not a class instance).
|
|
2570
|
+
* Useful for mocking behavior during testing.
|
|
2571
|
+
*
|
|
2572
|
+
* @template Target - The type of the target class.
|
|
2573
|
+
* @template Key - The static method or static property key on the target object to spy on.
|
|
2574
|
+
*
|
|
2575
|
+
* @param target - The object on which to spy.
|
|
2576
|
+
* @param key - The key of the method or property of the target to spy on.
|
|
2577
|
+
* @returns If the spied-on property is a function, returns a `MockState` object for the function,
|
|
2578
|
+
* allowing tracking of calls and modifying the return value or behavior.
|
|
2579
|
+
* Otherwise, returns a `MockState` object for the property, enabling tracking and manipulation of its value.
|
|
2580
|
+
*
|
|
2581
|
+
* @throws Error Throws an error if the `target` is a primitive value.
|
|
2582
|
+
* @throws Error Throws an error if `key` is null or undefined.
|
|
2583
|
+
* @throws Error Throws an error if the specified property does not exist on the target object.
|
|
2584
|
+
*
|
|
2585
|
+
* @remarks
|
|
2586
|
+
* This function is commonly used in testing environments to replace or monitor functionality without
|
|
2587
|
+
* altering the actual logic in the source code. It provides fine-grained control over target behavior.
|
|
2588
|
+
*
|
|
2589
|
+
* @example
|
|
2590
|
+
* ```ts
|
|
2591
|
+
* class ClassTest {
|
|
2592
|
+
* static name: string = 'ClassTest';
|
|
2593
|
+
*
|
|
2594
|
+
* static x(param: string) {
|
|
2595
|
+
* console.log(`original static x ${ param }`);
|
|
2596
|
+
* }
|
|
2597
|
+
* }
|
|
2598
|
+
*
|
|
2599
|
+
* const spy1 = xJet.spyOn(ClassTest, 'name');
|
|
2600
|
+
* const spy2 = xJet.spyOn(ClassTest, 'x');
|
|
2601
|
+
*
|
|
2602
|
+
* spy1.mockReturnValueOnce('Mock name');
|
|
2603
|
+
* spy2.mockImplementationOnce((param: string) => {
|
|
2604
|
+
* console.log(`Mock x ${ param }`);
|
|
2605
|
+
* });
|
|
2606
|
+
*
|
|
2607
|
+
* console.log(ClassTest.name); // Mock name
|
|
2608
|
+
* console.log(ClassTest.name); // ClassTest
|
|
2609
|
+
*
|
|
2610
|
+
* ClassTest.x('test1'); // Mock x test1
|
|
2611
|
+
* ClassTest.x('test2'); // original static x test2
|
|
2612
|
+
* ```
|
|
2613
|
+
*
|
|
2614
|
+
* @see FunctionType
|
|
2615
|
+
* @see KeysExtendingConstructorType
|
|
2616
|
+
*
|
|
2617
|
+
* @since 1.0.0
|
|
2618
|
+
*/
|
|
2619
|
+
declare function spyOnImplementation<Target, Key extends KeysExtendingConstructorType<Target>>(target: Target, key: Key): Target[Key] extends FunctionType ? MockState<ReturnType<Target[Key]>, Parameters<Target[Key]>, Target> : MockState<Target[Key], [], Target>;
|
|
2620
|
+
/**
|
|
2621
|
+
* Creates a mock spy on the specified method or constructor of the target object.
|
|
2622
|
+
*
|
|
2623
|
+
* @template Target The type of the target object.
|
|
2624
|
+
* @template Key The type of the method or constructor key on the target object.
|
|
2625
|
+
*
|
|
2626
|
+
* @param target - The object whose method or constructor needs to be spied on.
|
|
2627
|
+
* @param key - The property key of the method or constructor to spy on.
|
|
2628
|
+
* @return A mock state representing the spied method or constructor if the key corresponds to a constructor type;
|
|
2629
|
+
* otherwise, throws a type error.
|
|
2630
|
+
*
|
|
2631
|
+
* @throws Error Throws an error if the `target` is a primitive value.
|
|
2632
|
+
* @throws Error Throws an error if `key` is null or undefined.
|
|
2633
|
+
* @throws Error Throws an error if the specified property does not exist on the target object.
|
|
2634
|
+
*
|
|
2635
|
+
* @remarks
|
|
2636
|
+
* This method is typically used for testing purposes to observe or manipulate calls to the method or constructor of an object.
|
|
2637
|
+
* The returned mock state may allow additional configuration, such as altering its behavior or tracking calls.
|
|
2638
|
+
*
|
|
2639
|
+
* @example
|
|
2640
|
+
* ```ts
|
|
2641
|
+
* const coolObject = {
|
|
2642
|
+
* ClassTest: class {
|
|
2643
|
+
* constructor(param: number) {
|
|
2644
|
+
* console.log('original Constructor');
|
|
2645
|
+
* }
|
|
2646
|
+
*
|
|
2647
|
+
* justAnFunction() {
|
|
2648
|
+
* console.log('original justAnFunction');
|
|
2649
|
+
* }
|
|
2650
|
+
* }
|
|
2651
|
+
* };
|
|
2652
|
+
*
|
|
2653
|
+
* const spy = xJet.spyOn(coolObject, 'ClassTest');
|
|
2654
|
+
* spy.mockImplementationOnce((param: number) => {
|
|
2655
|
+
* console.log(`mock Constructor with param: ${ param }`);
|
|
2656
|
+
*
|
|
2657
|
+
* return <any> {
|
|
2658
|
+
* justAnFunction() {
|
|
2659
|
+
* console.log('mock justAnFunction');
|
|
2660
|
+
* }
|
|
2661
|
+
* };
|
|
2662
|
+
* });
|
|
2663
|
+
*
|
|
2664
|
+
* const instance = new coolObject.ClassTest(1); // mock Constructor with param: 1
|
|
2665
|
+
* instance.justAnFunction(); // mock justAnFunction
|
|
2666
|
+
*
|
|
2667
|
+
* const instance2 = new coolObject.ClassTest(2); // original Constructor
|
|
2668
|
+
* instance2.justAnFunction(); // original justAnFunction
|
|
2669
|
+
* ```
|
|
2670
|
+
*
|
|
2671
|
+
* @see ConstructorType
|
|
2672
|
+
* @see ConstructorKeysType
|
|
2673
|
+
*
|
|
2674
|
+
* @since 1.0.0
|
|
2675
|
+
*/
|
|
2676
|
+
declare function spyOnImplementation<Target, Key extends ConstructorKeysType<Target>>(target: Target, key: Key): Target[Key] extends ConstructorLikeType ? MockState<InstanceType<Target[Key]>, ConstructorParameters<Target[Key]>, Target> : never;
|
|
2677
|
+
/**
|
|
2678
|
+
* Creates a spy on a specific method or property of the given target object.
|
|
2679
|
+
*
|
|
2680
|
+
* @template Target - The type of the target object to spy on.
|
|
2681
|
+
* @template Key - The key of the property or method to spy on within the target object.
|
|
2682
|
+
*
|
|
2683
|
+
* @param target - The target object containing the property or method to be spied upon.
|
|
2684
|
+
* @param key - The name of the property or method on the target object to spy on.
|
|
2685
|
+
* @returns If the spied target is a function, it returns a `MockState` object to observe calls and arguments of the function.
|
|
2686
|
+
* Otherwise, it returns a `MockState` object to observe the value or state of the property.
|
|
2687
|
+
*
|
|
2688
|
+
* @throws Error Throws an error if the `target` is a primitive value.
|
|
2689
|
+
* @throws Error Throws an error if `key` is null or undefined.
|
|
2690
|
+
* @throws Error Throws an error if the specified property does not exist on the target object.
|
|
2691
|
+
*
|
|
2692
|
+
* @remarks This method is commonly used in test environments to monitor and assert interactions with a specific property
|
|
2693
|
+
* or method on an object. The returned `MockState` can be used to retrieve call history or observe mutations.
|
|
2694
|
+
*
|
|
2695
|
+
* @example
|
|
2696
|
+
* ```ts
|
|
2697
|
+
* const coolObject = {
|
|
2698
|
+
* myMethod() {
|
|
2699
|
+
* return 'Original myMethod';
|
|
2700
|
+
* },
|
|
2701
|
+
* coolString: 'Original coolString'
|
|
2702
|
+
* };
|
|
2703
|
+
*
|
|
2704
|
+
* const spy = xJet.spyOn(coolObject, 'coolString');
|
|
2705
|
+
* const spy2 = xJet.spyOn(coolObject, 'myMethod');
|
|
2706
|
+
*
|
|
2707
|
+
* spy.mockImplementationOnce(() => 'mock coolString');
|
|
2708
|
+
* spy2.mockImplementationOnce(() => 'mock myMethod string');
|
|
2709
|
+
*
|
|
2710
|
+
* console.log(coolObject.coolString); // mock coolString
|
|
2711
|
+
* console.log(coolObject.coolString); // Original coolString
|
|
2712
|
+
* console.log(coolObject.myMethod()); // mock myMethod string
|
|
2713
|
+
* console.log(coolObject.myMethod()); // Original myMethod
|
|
2714
|
+
* ```
|
|
2715
|
+
*
|
|
2716
|
+
* @see FunctionType
|
|
2717
|
+
*
|
|
2718
|
+
* @since 1.0.0
|
|
2719
|
+
*/
|
|
2720
|
+
declare function spyOnImplementation<Target, Key extends keyof Target>(target: Target, key: Key): Target[Key] extends FunctionType ? MockState<ReturnType<Target[Key]>, Parameters<Target[Key]>, ThisParameterType<Target[Key]>> : MockState<Target[Key] | void, [Target[Key]], ThisParameterType<Target[Key]>>;
|
|
2721
|
+
|
|
2722
|
+
/**
|
|
2723
|
+
* Import will remove at compile time
|
|
2724
|
+
*/
|
|
2725
|
+
/**
|
|
2726
|
+
* A utility type that removes all index signatures (string and number keys) from the given type `T`.
|
|
2727
|
+
* This results in a new type that retains only explicitly defined properties.
|
|
2728
|
+
*
|
|
2729
|
+
* @template T - The type from which index signatures are to be removed.
|
|
2730
|
+
*
|
|
2731
|
+
* @remarks
|
|
2732
|
+
* This type is useful for filtering out index signatures from types, especially when working
|
|
2733
|
+
* with mapped or dynamic types where indexable properties are not desired.
|
|
2734
|
+
* When an object has an index signature (e.g., `[key: string]: any`), TypeScript assumes that all
|
|
2735
|
+
* possible string or number keys exist on the object. This utility type filters out such keys,
|
|
2736
|
+
* leaving only explicitly defined properties.
|
|
2737
|
+
*
|
|
2738
|
+
* @see https://stackoverflow.com/a/66252656/4536543
|
|
2739
|
+
*
|
|
2740
|
+
* @since 1.0.0
|
|
2741
|
+
*/
|
|
2742
|
+
type RemoveIndexType<T> = {
|
|
2743
|
+
[P in keyof T as string extends P ? never : number extends P ? never : P]: T[P];
|
|
2744
|
+
};
|
|
2745
|
+
/**
|
|
2746
|
+
* A utility type that maps the keys of a given type `T` whose properties are constructor types.
|
|
2747
|
+
*
|
|
2748
|
+
* This type iterates over the properties of `RemoveIndexType<T>`,
|
|
2749
|
+
* retaining only those keys where the value matches a constructor type.
|
|
2750
|
+
*
|
|
2751
|
+
* @template T - The target type from which constructor-like properties are to be extracted.
|
|
2752
|
+
*
|
|
2753
|
+
* @remarks
|
|
2754
|
+
* This type is designed to filter out keys of a given type `T` to include only those that are associated with constructor functions.
|
|
2755
|
+
* It leverages mapped types along with conditional type checks to achieve this functionality.
|
|
2756
|
+
*
|
|
2757
|
+
* @since 1.0.0
|
|
2758
|
+
*/
|
|
2759
|
+
type PropertiesWithConstructorsType<T> = {
|
|
2760
|
+
[Key in keyof RemoveIndexType<T> as RemoveIndexType<T>[Key] extends ConstructorType ? Key : never]: RemoveIndexType<T>[Key];
|
|
2761
|
+
};
|
|
2762
|
+
/**
|
|
2763
|
+
* A utility type that extracts the keys of the provided type `T` that correspond
|
|
2764
|
+
* to properties with constructor types, removing any index signatures.
|
|
2765
|
+
*
|
|
2766
|
+
* @template T - The object type from which constructor property keys are extracted.
|
|
2767
|
+
*
|
|
2768
|
+
* @remarks
|
|
2769
|
+
* This type is useful for narrowing down a type to only the keys representing
|
|
2770
|
+
* properties with constructor functions (e.g., classes) while excluding index signatures.
|
|
2771
|
+
*
|
|
2772
|
+
* @since 1.0.0
|
|
2773
|
+
*/
|
|
2774
|
+
type ConstructorKeysType<T> = RemoveIndexType<keyof PropertiesWithConstructorsType<T>>;
|
|
2775
|
+
/**
|
|
2776
|
+
* A utility type that extracts the keys of a type `T` if `T` extends a constructor type.
|
|
2777
|
+
* If `T` does not extend a constructor type, it resolves to `never`.
|
|
2778
|
+
*
|
|
2779
|
+
* @template T - The type to check and extract keys from.
|
|
2780
|
+
*
|
|
2781
|
+
* @param T - The generic type parameter which is evaluated against a constructor type.
|
|
2782
|
+
*
|
|
2783
|
+
* @remarks
|
|
2784
|
+
* This type is particularly useful when working with class-like or constructor-based types.
|
|
2785
|
+
* It further applies `RemoveIndexType` to exclude index signatures from the resulting keys.
|
|
2786
|
+
*
|
|
2787
|
+
* @since 1.0.0
|
|
2788
|
+
*/
|
|
2789
|
+
type KeysExtendingConstructorType<T> = T extends ConstructorType ? keyof RemoveIndexType<T> : never;
|
|
2790
|
+
|
|
2791
|
+
/**
|
|
2792
|
+
* Import will remove at compile time
|
|
2793
|
+
*/
|
|
2794
|
+
/**
|
|
2795
|
+
* Implementation of the test directive that allows for test definition with chainable modifiers.
|
|
2796
|
+
* Acts as a function that can be invoked directly while also providing chainable property access.
|
|
2797
|
+
*
|
|
2798
|
+
* @template T - Type parameter for the test callback return type
|
|
2799
|
+
*
|
|
2800
|
+
* @param description - The test description
|
|
2801
|
+
* @param block - The test implementation function
|
|
2802
|
+
* @param timeout - Optional timeout in milliseconds
|
|
2803
|
+
*
|
|
2804
|
+
* @returns void
|
|
2805
|
+
*
|
|
2806
|
+
* @throws Error - When attempting to nest tests inside other tests
|
|
2807
|
+
* @throws Error - When using incompatible flag combinations (e.g., skip with only)
|
|
2808
|
+
*
|
|
2809
|
+
* @remarks
|
|
2810
|
+
* This class extends Function to allow it to be invoked as a function while also
|
|
2811
|
+
* providing methods and properties. It uses a Proxy to capture function invocations
|
|
2812
|
+
* and redirects them to the invoke method.
|
|
2813
|
+
*
|
|
2814
|
+
* @example
|
|
2815
|
+
* ```ts
|
|
2816
|
+
* // Basic test
|
|
2817
|
+
* test('should work', () => {
|
|
2818
|
+
* expect(2 + 2).toBe(4);
|
|
2819
|
+
* });
|
|
2820
|
+
*
|
|
2821
|
+
* // With modifiers
|
|
2822
|
+
* test.only('runs exclusively', () => {
|
|
2823
|
+
* expect(true).toBe(true);
|
|
2824
|
+
* });
|
|
2825
|
+
*
|
|
2826
|
+
* // Parameterized tests
|
|
2827
|
+
* test.each([1, 2, 3])('test with value %s', (value) => {
|
|
2828
|
+
* expect(typeof value).toBe('number');
|
|
2829
|
+
* });
|
|
2830
|
+
* ```
|
|
2831
|
+
*
|
|
2832
|
+
* @see SuiteState
|
|
2833
|
+
* @see TestDirectiveInterface
|
|
2834
|
+
*
|
|
2835
|
+
* @since 1.0.0
|
|
2836
|
+
*/
|
|
2837
|
+
declare class TestDirective extends Function {
|
|
2838
|
+
/**
|
|
2839
|
+
* Singleton instance of the TestDirective class.
|
|
2840
|
+
*
|
|
2841
|
+
* @see TestDirectiveInterface
|
|
2842
|
+
* @since 1.0.0
|
|
2843
|
+
*/
|
|
2844
|
+
private static instance?;
|
|
2845
|
+
/**
|
|
2846
|
+
* Error messages for invalid flag combinations in test configuration.
|
|
2847
|
+
*
|
|
2848
|
+
* @since 1.0.0
|
|
2849
|
+
*/
|
|
2850
|
+
private static readonly ERROR_MESSAGES;
|
|
2851
|
+
/**
|
|
2852
|
+
* Default timeout in milliseconds for test execution
|
|
2853
|
+
* @since 1.0.0
|
|
2854
|
+
*/
|
|
2855
|
+
private static readonly DEFAULT_TIMEOUT;
|
|
2856
|
+
/**
|
|
2857
|
+
* Configuration options controlling test behavior
|
|
2858
|
+
* @see TestFlagsType
|
|
2859
|
+
* @since 1.0.0
|
|
2860
|
+
*/
|
|
2861
|
+
private options;
|
|
2862
|
+
/**
|
|
2863
|
+
* @param invokationStack - The precise source code invocationStack where the error occurred
|
|
2864
|
+
*/
|
|
2865
|
+
private invocationStack;
|
|
2866
|
+
/**
|
|
2867
|
+
* Returns the singleton instance of TestDirective, creating it if it doesn't exist
|
|
2868
|
+
*
|
|
2869
|
+
* @returns The singleton instance of the test directive implementation
|
|
2870
|
+
*
|
|
2871
|
+
* @remarks
|
|
2872
|
+
* This method implements the singleton pattern, ensuring only one instance of the test directive exists
|
|
2873
|
+
* throughout the application lifecycle
|
|
2874
|
+
*
|
|
2875
|
+
* @example
|
|
2876
|
+
* ```ts
|
|
2877
|
+
* const test = TestDirective.getInstance();
|
|
2878
|
+
* test('my test', () => {
|
|
2879
|
+
* // test implementation
|
|
2880
|
+
* });
|
|
2881
|
+
* ```
|
|
2882
|
+
*
|
|
2883
|
+
* @see TestDirectiveInterface
|
|
2884
|
+
*
|
|
2885
|
+
* @since 1.0.0
|
|
2886
|
+
*/
|
|
2887
|
+
static getInstance(): TestDirectiveInterface;
|
|
2888
|
+
/**
|
|
2889
|
+
* Returns the singleton instance of TestDirective, creating it if it doesn't exist.
|
|
2890
|
+
*
|
|
2891
|
+
* @returns The singleton instance of TestDirective cast to TestDirectiveInterface
|
|
2892
|
+
*
|
|
2893
|
+
* @remarks
|
|
2894
|
+
* This method implements the Singleton pattern to ensure only one instance of
|
|
2895
|
+
* TestDirective exists in the application. It creates the instance on the first call
|
|
2896
|
+
* and returns the existing instance on subsequent calls.
|
|
2897
|
+
*
|
|
2898
|
+
* @example
|
|
2899
|
+
* ```ts
|
|
2900
|
+
* const test = TestDirective.getInstance();
|
|
2901
|
+
* test('example test', () => {
|
|
2902
|
+
* expect(true).toBe(true);
|
|
2903
|
+
* });
|
|
2904
|
+
* ```
|
|
2905
|
+
*
|
|
2906
|
+
* @since 1.0.0
|
|
2907
|
+
*/
|
|
2908
|
+
get skip(): this;
|
|
2909
|
+
/**
|
|
2910
|
+
* Sets the 'only' flag for this test, making it the only test to run in its suite.
|
|
2911
|
+
*
|
|
2912
|
+
* @returns This instance with the 'only' flag set
|
|
2913
|
+
*
|
|
2914
|
+
* @throws Error - When attempting to set 'only' flag on a skipped test
|
|
2915
|
+
*
|
|
2916
|
+
* @example
|
|
2917
|
+
* ```ts
|
|
2918
|
+
* test.only('this is the only test that will run', () => {
|
|
2919
|
+
* expect(true).toBe(true);
|
|
2920
|
+
* });
|
|
2921
|
+
* ```
|
|
2922
|
+
*
|
|
2923
|
+
* @see skip
|
|
2924
|
+
* @since 1.0.0
|
|
2925
|
+
*/
|
|
2926
|
+
get only(): this;
|
|
2927
|
+
/**
|
|
2928
|
+
* Marks a test as a todo item, indicating it's planned but not yet implemented
|
|
2929
|
+
*
|
|
2930
|
+
* @throws Error - When attempting to use todo flag on a skipped test
|
|
2931
|
+
*
|
|
2932
|
+
* @remarks
|
|
2933
|
+
* Todo tests appear in test reports to remind developers of planned work,
|
|
2934
|
+
* but don't execute any test code
|
|
2935
|
+
*
|
|
2936
|
+
* @example
|
|
2937
|
+
* ```ts
|
|
2938
|
+
* // Define a todo test
|
|
2939
|
+
* test.todo('feature that needs implementation');
|
|
2940
|
+
* ```
|
|
2941
|
+
*
|
|
2942
|
+
* @see TestFlagsType
|
|
2943
|
+
*
|
|
2944
|
+
* @since 1.0.0
|
|
2945
|
+
*/
|
|
2946
|
+
get todo(): this;
|
|
2947
|
+
/**
|
|
2948
|
+
* Marks the test as expected to fail.
|
|
2949
|
+
*
|
|
2950
|
+
* @returns This instance with the 'failing' flag set
|
|
2951
|
+
*
|
|
2952
|
+
* @throws Error - When attempting to mark a skipped test as failing
|
|
2953
|
+
*
|
|
2954
|
+
* @example
|
|
2955
|
+
* ```ts
|
|
2956
|
+
* test.failing('this test is expected to fail', () => {
|
|
2957
|
+
* throw new Error('This is an expected failure');
|
|
2958
|
+
* });
|
|
2959
|
+
* ```
|
|
2960
|
+
*
|
|
2961
|
+
* @see skip
|
|
2962
|
+
* @since 1.0.0
|
|
2963
|
+
*/
|
|
2964
|
+
get failing(): this;
|
|
2965
|
+
/**
|
|
2966
|
+
* Creates a parameterized test using template strings.
|
|
2967
|
+
*
|
|
2968
|
+
* @template T - Array type containing the values to be used in the test
|
|
2969
|
+
*
|
|
2970
|
+
* @param string - Template string array used to create the test title
|
|
2971
|
+
* @param placeholders - Values to be inserted into the template string
|
|
2972
|
+
* @returns A callback function for creating the parameterized test
|
|
2973
|
+
*
|
|
2974
|
+
* @remarks
|
|
2975
|
+
* The description supports parameter formatting with the following placeholders:
|
|
2976
|
+
* - %p - pretty-format output
|
|
2977
|
+
* - %s - String value
|
|
2978
|
+
* - %d, %i - Number as integer
|
|
2979
|
+
* - %f - Floating point value
|
|
2980
|
+
* - %j - JSON string
|
|
2981
|
+
* - %o - Object representation
|
|
2982
|
+
* - %# - Index of the test case
|
|
2983
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
2984
|
+
*
|
|
2985
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
2986
|
+
* - $variable - Injects the property value
|
|
2987
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
2988
|
+
* - $# - Injects the index of the test case
|
|
2989
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
2990
|
+
*
|
|
2991
|
+
* @example
|
|
2992
|
+
* ```ts
|
|
2993
|
+
* test.each`
|
|
2994
|
+
* a | b | expected
|
|
2995
|
+
* ${1} | ${1} | ${2}
|
|
2996
|
+
* ${2} | ${2} | ${4}
|
|
2997
|
+
* `('returns $expected when $a is added to $b', ({a, b, expected}) => {
|
|
2998
|
+
* expect(a + b).toBe(expected);
|
|
2999
|
+
* });
|
|
3000
|
+
* ```
|
|
3001
|
+
*
|
|
3002
|
+
* @see each.array
|
|
3003
|
+
* @since 1.0.0
|
|
3004
|
+
*/
|
|
3005
|
+
each<T extends ReadonlyArray<unknown>>(string: TemplateStringsArray, ...placeholders: T): TestCallbackType<Record<string, T[number]>>;
|
|
3006
|
+
/**
|
|
3007
|
+
* Creates a parameterized test using arrays of test cases.
|
|
3008
|
+
*
|
|
3009
|
+
* @template T - Type representing the test case data structure
|
|
3010
|
+
*
|
|
3011
|
+
* @param cases - Arrays containing test case values
|
|
3012
|
+
* @returns A callback function for creating the parameterized test
|
|
3013
|
+
*
|
|
3014
|
+
* @remarks
|
|
3015
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3016
|
+
* - %p - pretty-format output
|
|
3017
|
+
* - %s - String value
|
|
3018
|
+
* - %d, %i - Number as integer
|
|
3019
|
+
* - %f - Floating point value
|
|
3020
|
+
* - %j - JSON string
|
|
3021
|
+
* - %o - Object representation
|
|
3022
|
+
* - %# - Index of the test case
|
|
3023
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3024
|
+
*
|
|
3025
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3026
|
+
* - $variable - Injects the property value
|
|
3027
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3028
|
+
* - $# - Injects the index of the test case
|
|
3029
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3030
|
+
*
|
|
3031
|
+
* @example
|
|
3032
|
+
* ```ts
|
|
3033
|
+
* test.each(
|
|
3034
|
+
* [1, 1, 2],
|
|
3035
|
+
* [1, 2, 3],
|
|
3036
|
+
* [2, 2, 4]
|
|
3037
|
+
* )('adds %i + %i to equal %i', (a, b, expected) => {
|
|
3038
|
+
* expect(a + b).toBe(expected);
|
|
3039
|
+
* });
|
|
3040
|
+
* ```
|
|
3041
|
+
*
|
|
3042
|
+
* @see each
|
|
3043
|
+
* @since 1.0.0
|
|
3044
|
+
*/
|
|
3045
|
+
each<T extends Array<unknown> | [unknown]>(...cases: T[]): TestCallbackType<T>;
|
|
3046
|
+
/**
|
|
3047
|
+
* Creates a parameterized test using individual test case objects or primitive values.
|
|
3048
|
+
*
|
|
3049
|
+
* @template T - Type representing the test case data
|
|
3050
|
+
*
|
|
3051
|
+
* @param args - Test case objects or primitive values
|
|
3052
|
+
* @returns A callback function for creating the parameterized test
|
|
3053
|
+
*
|
|
3054
|
+
* @remarks
|
|
3055
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3056
|
+
* - %p - pretty-format output
|
|
3057
|
+
* - %s - String value
|
|
3058
|
+
* - %d, %i - Number as integer
|
|
3059
|
+
* - %f - Floating point value
|
|
3060
|
+
* - %j - JSON string
|
|
3061
|
+
* - %o - Object representation
|
|
3062
|
+
* - %# - Index of the test case
|
|
3063
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3064
|
+
*
|
|
3065
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3066
|
+
* - $variable - Injects the property value
|
|
3067
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3068
|
+
* - $# - Injects the index of the test case
|
|
3069
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3070
|
+
*
|
|
3071
|
+
* @example
|
|
3072
|
+
* ```ts
|
|
3073
|
+
* test.each(
|
|
3074
|
+
* { a: 1, b: 1, expected: 2 },
|
|
3075
|
+
* { a: 1, b: 2, expected: 3 },
|
|
3076
|
+
* { a: 2, b: 2, expected: 4 }
|
|
3077
|
+
* )('adds $a + $b to equal $expected', ({ a, b, expected }) => {
|
|
3078
|
+
* expect(a + b).toBe(expected);
|
|
3079
|
+
* });
|
|
3080
|
+
* ```
|
|
3081
|
+
*
|
|
3082
|
+
* @see each
|
|
3083
|
+
* @since 1.0.0
|
|
3084
|
+
*/
|
|
3085
|
+
each<T>(...args: readonly T[]): TestCallbackType<T>;
|
|
3086
|
+
/**
|
|
3087
|
+
* Creates a parameterized test using arrays of primitive values as test cases.
|
|
3088
|
+
*
|
|
3089
|
+
* @template T - Array type containing the primitive test values
|
|
3090
|
+
*
|
|
3091
|
+
* @param args - Arrays of primitive values to be used as test cases
|
|
3092
|
+
* @returns A callback function for creating the parameterized test
|
|
3093
|
+
*
|
|
3094
|
+
* @remarks
|
|
3095
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3096
|
+
* - %p - pretty-format output
|
|
3097
|
+
* - %s - String value
|
|
3098
|
+
* - %d, %i - Number as integer
|
|
3099
|
+
* - %f - Floating point value
|
|
3100
|
+
* - %j - JSON string
|
|
3101
|
+
* - %o - Object representation
|
|
3102
|
+
* - %# - Index of the test case
|
|
3103
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3104
|
+
*
|
|
3105
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3106
|
+
* - $variable - Injects the property value
|
|
3107
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3108
|
+
* - $# - Injects the index of the test case
|
|
3109
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3110
|
+
*
|
|
3111
|
+
* @example
|
|
3112
|
+
* ```ts
|
|
3113
|
+
* test.each(
|
|
3114
|
+
* [1, 2, 3],
|
|
3115
|
+
* [4, 5, 9],
|
|
3116
|
+
* [6, 7, 13]
|
|
3117
|
+
* )('adds %i + %i to equal %i', (a, b, expected) => {
|
|
3118
|
+
* expect(a + b).toBe(expected);
|
|
3119
|
+
* });
|
|
3120
|
+
* ```
|
|
3121
|
+
*
|
|
3122
|
+
* @see each
|
|
3123
|
+
* @since 1.0.0
|
|
3124
|
+
*/
|
|
3125
|
+
each<T extends Array<unknown>>(...args: T): TestCallbackType<T[number]>;
|
|
3126
|
+
/**
|
|
3127
|
+
* Creates a new TestModel with the current configuration
|
|
3128
|
+
*/
|
|
3129
|
+
private createTest;
|
|
3130
|
+
}
|
|
3131
|
+
|
|
3132
|
+
/**
|
|
3133
|
+
* Import will remove at compile time
|
|
3134
|
+
*/
|
|
3135
|
+
/**
|
|
3136
|
+
* Represents a test callback function that registers a named test with arguments and optional done callback
|
|
3137
|
+
*
|
|
3138
|
+
* @template T - The type of arguments passed to the test function
|
|
3139
|
+
*
|
|
3140
|
+
* @param name - The descriptive name of the test
|
|
3141
|
+
* @param fn - The test implementation function that either accepts arguments and a done callback, or returns a Promise
|
|
3142
|
+
* @returns A void function that registers the test in the testing framework
|
|
3143
|
+
*
|
|
3144
|
+
* @example
|
|
3145
|
+
* ```ts
|
|
3146
|
+
* const testEach: TestCallbackType<{value: number}> = (name, fn) => {
|
|
3147
|
+
* test(`Test ${name}`, (done) => {
|
|
3148
|
+
* fn({value: 42}, done);
|
|
3149
|
+
* });
|
|
3150
|
+
* };
|
|
3151
|
+
* ```
|
|
3152
|
+
*
|
|
3153
|
+
* @see TestDirectiveInterface - The interface that utilizes this callback type
|
|
3154
|
+
* @since 1.0.0
|
|
3155
|
+
*/
|
|
3156
|
+
type TestCallbackType<T> = (name: string, fn: (args: T, done: DoneCallbackType) => void | ((args: T) => Promise<void>)) => void;
|
|
3157
|
+
interface TestDirectiveInterface {
|
|
3158
|
+
/**
|
|
3159
|
+
* Registers a test case with the given name and optional callback function
|
|
3160
|
+
*
|
|
3161
|
+
* @param name - The descriptive name of the test case
|
|
3162
|
+
* @param fn - The callback function containing the test implementation
|
|
3163
|
+
* @param timeout - The maximum time in milliseconds the test is allowed to run before timing out
|
|
3164
|
+
* @returns Nothing
|
|
3165
|
+
*
|
|
3166
|
+
* @example
|
|
3167
|
+
* ```ts
|
|
3168
|
+
* test('should validate user input', () => {
|
|
3169
|
+
* const result = validateInput('test@example.com');
|
|
3170
|
+
* expect(result).toBe(true);
|
|
3171
|
+
* });
|
|
3172
|
+
*
|
|
3173
|
+
* test('should handle async operations', async () => {
|
|
3174
|
+
* const data = await fetchUserData(1);
|
|
3175
|
+
* expect(data.name).toBe('John');
|
|
3176
|
+
* }, 5000);
|
|
3177
|
+
* ```
|
|
3178
|
+
*
|
|
3179
|
+
* @see CallbackHandlerType - The type definition for the test callback function
|
|
3180
|
+
* @since 1.0.0
|
|
3181
|
+
*/
|
|
3182
|
+
(name: string, fn?: CallbackHandlerType, timeout?: number): void;
|
|
3183
|
+
/**
|
|
3184
|
+
* Creates a skipped test that will be recognized but not executed during test runs
|
|
3185
|
+
*
|
|
3186
|
+
* @override
|
|
3187
|
+
* @returns The same TestDirectiveInterface instance with skip flag enabled, allowing for method chaining
|
|
3188
|
+
* @throws Error - When attempting to combine with 'only' flag which would create conflicting test behavior
|
|
3189
|
+
*
|
|
3190
|
+
* @remarks
|
|
3191
|
+
* When applied, the test will be marked as skipped in test reports but won't be executed.
|
|
3192
|
+
* Cannot be combined with the 'only' modifier due to conflicting behavior.
|
|
3193
|
+
*
|
|
3194
|
+
* @example
|
|
3195
|
+
* ```ts
|
|
3196
|
+
* test.skip('temporarily disabled test', () => {
|
|
3197
|
+
* expect(result).toBe(true);
|
|
3198
|
+
* });
|
|
3199
|
+
* ```
|
|
3200
|
+
*
|
|
3201
|
+
* @see TestDirectiveInterface
|
|
3202
|
+
* @since 1.0.0
|
|
3203
|
+
*/
|
|
3204
|
+
get skip(): TestDirectiveInterface;
|
|
3205
|
+
/**
|
|
3206
|
+
* Creates an exclusive test that will run while other non-exclusive tests are skipped
|
|
3207
|
+
*
|
|
3208
|
+
* @override
|
|
3209
|
+
* @returns The same TestDirectiveInterface instance with only flag enabled, allowing for method chaining
|
|
3210
|
+
* @throws Error - When attempting to combine with 'skip' flag which would create conflicting test behavior
|
|
3211
|
+
*
|
|
3212
|
+
* @remarks
|
|
3213
|
+
* When applied, only this test and other tests marked with 'only' will be executed.
|
|
3214
|
+
* This is useful for focusing on specific tests during development or debugging.
|
|
3215
|
+
* Cannot be combined with the 'skip' modifier due to conflicting behavior.
|
|
3216
|
+
*
|
|
3217
|
+
* @example
|
|
3218
|
+
* ```ts
|
|
3219
|
+
* test.only('focus on this test', () => {
|
|
3220
|
+
* const result = performOperation();
|
|
3221
|
+
* expect(result).toBe(expectedValue);
|
|
3222
|
+
* });
|
|
3223
|
+
* ```
|
|
3224
|
+
*
|
|
3225
|
+
* @see TestDirectiveInterface
|
|
3226
|
+
* @since 1.0.0
|
|
3227
|
+
*/
|
|
3228
|
+
get only(): TestDirectiveInterface;
|
|
3229
|
+
/**
|
|
3230
|
+
* Marks a test as planned but not yet implemented or currently incomplete
|
|
3231
|
+
*
|
|
3232
|
+
* @override
|
|
3233
|
+
* @returns The same TestDirectiveInterface instance with todo flag enabled, allowing for method chaining
|
|
3234
|
+
* @throws Error - When attempting to combine with 'skip' flag which would create redundant test configuration
|
|
3235
|
+
*
|
|
3236
|
+
* @remarks
|
|
3237
|
+
* Tests marked as todo will be reported in test results as pending or incomplete.
|
|
3238
|
+
* This is useful for planning test cases before implementing them or for documenting
|
|
3239
|
+
* tests that need to be written in the future.
|
|
3240
|
+
*
|
|
3241
|
+
* @example
|
|
3242
|
+
* ```ts
|
|
3243
|
+
* test.todo('implement validation test for email addresses');
|
|
3244
|
+
*
|
|
3245
|
+
* // Or with empty implementation
|
|
3246
|
+
* test.todo('handle error cases', () => {
|
|
3247
|
+
* // To be implemented
|
|
3248
|
+
* });
|
|
3249
|
+
* ```
|
|
3250
|
+
*
|
|
3251
|
+
* @see TestDirectiveInterface
|
|
3252
|
+
* @since 1.0.0
|
|
3253
|
+
*/
|
|
3254
|
+
get todo(): TestDirectiveInterface;
|
|
3255
|
+
/**
|
|
3256
|
+
* Marks a test that is expected to fail, allowing tests to be committed even with known failures
|
|
3257
|
+
*
|
|
3258
|
+
* @override
|
|
3259
|
+
* @returns The same TestDirectiveInterface instance with failing flag enabled, allowing for method chaining
|
|
3260
|
+
* @throws Error - When attempting to combine with incompatible directives that would create ambiguous test behavior
|
|
3261
|
+
*
|
|
3262
|
+
* @remarks
|
|
3263
|
+
* The failing directive is useful when you want to document a bug that currently makes a test fail.
|
|
3264
|
+
* Tests marked as failing will be reported as passed when they fail, and failed when they pass.
|
|
3265
|
+
* This helps maintain test suites that contain tests for known issues or bugs that haven't been fixed yet.
|
|
3266
|
+
*
|
|
3267
|
+
* @example
|
|
3268
|
+
* ```ts
|
|
3269
|
+
* test.failing('this bug is not fixed yet', () => {
|
|
3270
|
+
* const result = buggyFunction();
|
|
3271
|
+
* expect(result).toBe(expectedValue); // This will fail as expected
|
|
3272
|
+
* });
|
|
3273
|
+
* ```
|
|
3274
|
+
*
|
|
3275
|
+
* @see TestDirectiveInterface
|
|
3276
|
+
* @since 1.0.0
|
|
3277
|
+
*/
|
|
3278
|
+
get failing(): TestDirectiveInterface;
|
|
3279
|
+
/**
|
|
3280
|
+
* Creates a parameterized test suite with table data defined using template literals
|
|
3281
|
+
*
|
|
3282
|
+
* @template T - Array type containing the values for parameterized tests
|
|
3283
|
+
* @param string - Template strings array containing the table header and row structure
|
|
3284
|
+
* @param placeholders - Values to be inserted into the template strings to form the test data table
|
|
3285
|
+
* @returns A function that accepts a title template and callback to define the test suite
|
|
3286
|
+
* @throws Error - When the template format is invalid or doesn't match the provided values
|
|
3287
|
+
*
|
|
3288
|
+
* @remarks
|
|
3289
|
+
* This method allows creating data-driven tests with a table-like syntax using template literals.
|
|
3290
|
+
* Each row in the provided table represents a separate test case with different inputs.
|
|
3291
|
+
* The values from each row will be mapped to named parameters in the test callback.
|
|
3292
|
+
*
|
|
3293
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3294
|
+
* - %p - pretty-format output
|
|
3295
|
+
* - %s - String value
|
|
3296
|
+
* - %d, %i - Number as integer
|
|
3297
|
+
* - %f - Floating point value
|
|
3298
|
+
* - %j - JSON string
|
|
3299
|
+
* - %o - Object representation
|
|
3300
|
+
* - %# - Index of the test case
|
|
3301
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3302
|
+
*
|
|
3303
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3304
|
+
* - $variable - Injects the property value
|
|
3305
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3306
|
+
* - $# - Injects the index of the test case
|
|
3307
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3308
|
+
*
|
|
3309
|
+
* @example
|
|
3310
|
+
* ```ts
|
|
3311
|
+
* test.each`
|
|
3312
|
+
* a | b | expected
|
|
3313
|
+
* ${1} | ${1} | ${2}
|
|
3314
|
+
* ${2} | ${2} | ${4}
|
|
3315
|
+
* ${3} | ${3} | ${6}
|
|
3316
|
+
* `('$a + $b should be $expected', ({a, b, expected}) => {
|
|
3317
|
+
* expect(a + b).toBe(expected);
|
|
3318
|
+
* });
|
|
3319
|
+
* ```
|
|
3320
|
+
* @see each
|
|
3321
|
+
* @see TestCallbackType
|
|
3322
|
+
*
|
|
3323
|
+
* @since 1.0.0
|
|
3324
|
+
*/
|
|
3325
|
+
each<T extends ReadonlyArray<unknown>>(string: TemplateStringsArray, ...placeholders: T): TestCallbackType<Record<string, T[number]>>;
|
|
3326
|
+
/**
|
|
3327
|
+
* Creates a parameterized test suite with an array of test cases
|
|
3328
|
+
*
|
|
3329
|
+
* @template T - Array type representing the structure of each test case
|
|
3330
|
+
* @param cases - One or more test cases, where each case is an array of values
|
|
3331
|
+
* @returns A function that accepts a title template and callback to define the test suite
|
|
3332
|
+
* @throws Error - When invalid test cases are provided or parameter count doesn't match the callback
|
|
3333
|
+
*
|
|
3334
|
+
* @remarks
|
|
3335
|
+
* This method allows creating data-driven tests by providing an array of test cases.
|
|
3336
|
+
* Each case array represents a separate test run with different parameters.
|
|
3337
|
+
* The values from each array will be passed as arguments to the test callback function.
|
|
3338
|
+
*
|
|
3339
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3340
|
+
* - %p - pretty-format output
|
|
3341
|
+
* - %s - String value
|
|
3342
|
+
* - %d, %i - Number as integer
|
|
3343
|
+
* - %f - Floating point value
|
|
3344
|
+
* - %j - JSON string
|
|
3345
|
+
* - %o - Object representation
|
|
3346
|
+
* - %# - Index of the test case
|
|
3347
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3348
|
+
*
|
|
3349
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3350
|
+
* - $variable - Injects the property value
|
|
3351
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3352
|
+
* - $# - Injects the index of the test case
|
|
3353
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3354
|
+
*
|
|
3355
|
+
* @example
|
|
3356
|
+
* ```ts
|
|
3357
|
+
* test.each(
|
|
3358
|
+
* [1, 1, 2],
|
|
3359
|
+
* [2, 2, 4],
|
|
3360
|
+
* [3, 3, 6]
|
|
3361
|
+
* )('adds %i + %i to equal %i', (a, b, expected) => {
|
|
3362
|
+
* expect(a + b).toBe(expected);
|
|
3363
|
+
* });
|
|
3364
|
+
* ```
|
|
3365
|
+
* @see each
|
|
3366
|
+
* @see TestCallbackType
|
|
3367
|
+
*
|
|
3368
|
+
* @since 1.0.0
|
|
3369
|
+
*/
|
|
3370
|
+
each<T extends Array<unknown> | [unknown]>(...cases: T[]): TestCallbackType<T>;
|
|
3371
|
+
/**
|
|
3372
|
+
* Creates a parameterized test suite with an array of generic test cases
|
|
3373
|
+
*
|
|
3374
|
+
* @template T - Type representing the structure of each test case
|
|
3375
|
+
* @param args - One or more test cases of type T to be used as parameters
|
|
3376
|
+
* @returns A function that accepts a title template and callback to define the test suite
|
|
3377
|
+
* @throws Error - When invalid test cases are provided or parameter format doesn't match the callback expectations
|
|
3378
|
+
*
|
|
3379
|
+
* @remarks
|
|
3380
|
+
* This method provides a flexible way to create data-driven tests using a variety of case types.
|
|
3381
|
+
* The test callback will be executed once for each provided test case.
|
|
3382
|
+
* Parameter values from each case will be passed to the test callback function.
|
|
3383
|
+
*
|
|
3384
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3385
|
+
* - %p - pretty-format output
|
|
3386
|
+
* - %s - String value
|
|
3387
|
+
* - %d, %i - Number as integer
|
|
3388
|
+
* - %f - Floating point value
|
|
3389
|
+
* - %j - JSON string
|
|
3390
|
+
* - %o - Object representation
|
|
3391
|
+
* - %# - Index of the test case
|
|
3392
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3393
|
+
*
|
|
3394
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3395
|
+
* - $variable - Injects the property value
|
|
3396
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3397
|
+
* - $# - Injects the index of the test case
|
|
3398
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3399
|
+
*
|
|
3400
|
+
* @example
|
|
3401
|
+
* ```ts
|
|
3402
|
+
* // Using objects as test cases
|
|
3403
|
+
* test.each(
|
|
3404
|
+
* { a: 1, b: 1, expected: 2 },
|
|
3405
|
+
* { a: 2, b: 2, expected: 4 },
|
|
3406
|
+
* { a: 3, b: 3, expected: 6 }
|
|
3407
|
+
* )('adds $a + $b to equal $expected', ({a, b, expected}) => {
|
|
3408
|
+
* expect(a + b).toBe(expected);
|
|
3409
|
+
* });
|
|
3410
|
+
* ```
|
|
3411
|
+
*
|
|
3412
|
+
* @see each
|
|
3413
|
+
* @see TestCallbackType
|
|
3414
|
+
* @since 1.0.0
|
|
3415
|
+
*/
|
|
3416
|
+
each<T>(...args: readonly T[]): TestCallbackType<T>;
|
|
3417
|
+
/**
|
|
3418
|
+
* Creates a parameterized test suite by spreading an array of test cases
|
|
3419
|
+
*
|
|
3420
|
+
* @template T - Array type containing the individual test cases
|
|
3421
|
+
* @param args - Individual test cases spread from an array
|
|
3422
|
+
* @returns A function that accepts a title template and callback to define the test suite
|
|
3423
|
+
* @throws Error - When invalid test cases are provided or case format is incompatible with the test callback
|
|
3424
|
+
*
|
|
3425
|
+
* @remarks
|
|
3426
|
+
* This method enables data-driven testing by spreading an array of test cases into individual parameters.
|
|
3427
|
+
* Each element in the array represents a separate test case that will be executed individually.
|
|
3428
|
+
* The test callback will be invoked once for each test case in the array.
|
|
3429
|
+
*
|
|
3430
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3431
|
+
* - %p - pretty-format output
|
|
3432
|
+
* - %s - String value
|
|
3433
|
+
* - %d, %i - Number as integer
|
|
3434
|
+
* - %f - Floating point value
|
|
3435
|
+
* - %j - JSON string
|
|
3436
|
+
* - %o - Object representation
|
|
3437
|
+
* - %# - Index of the test case
|
|
3438
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3439
|
+
*
|
|
3440
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3441
|
+
* - $variable - Injects the property value
|
|
3442
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3443
|
+
* - $# - Injects the index of the test case
|
|
3444
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3445
|
+
*
|
|
3446
|
+
* @example
|
|
3447
|
+
* ```ts
|
|
3448
|
+
* const testCases = [
|
|
3449
|
+
* [1, 1, 2],
|
|
3450
|
+
* [2, 2, 4],
|
|
3451
|
+
* [3, 3, 6]
|
|
3452
|
+
* ];
|
|
3453
|
+
*
|
|
3454
|
+
* test.each(...testCases)('adds %i + %i to equal %i', (a, b, expected) => {
|
|
3455
|
+
* expect(a + b).toBe(expected);
|
|
3456
|
+
* });
|
|
3457
|
+
* ```
|
|
3458
|
+
* @see each
|
|
3459
|
+
* @see TestCallbackType
|
|
3460
|
+
*
|
|
3461
|
+
* @since 1.0.0
|
|
3462
|
+
*/
|
|
3463
|
+
each<T extends Array<unknown>>(...args: T): TestCallbackType<T[number]>;
|
|
3464
|
+
/**
|
|
3465
|
+
* Executes a function block as a test case with the specified description
|
|
3466
|
+
*
|
|
3467
|
+
* @param description - Human-readable description of what this test case is verifying
|
|
3468
|
+
* @param block - Function to be executed as the test case implementation
|
|
3469
|
+
* @param args - Optional array of arguments to pass to the test function
|
|
3470
|
+
* @param timeout - Optional timeout in milliseconds for this specific test case
|
|
3471
|
+
* @returns void
|
|
3472
|
+
* @throws Error - When the test function throws an exception or an assertion fails
|
|
3473
|
+
*
|
|
3474
|
+
* @remarks
|
|
3475
|
+
* This method provides a way to directly invoke a test function with optional arguments.
|
|
3476
|
+
* It registers the test with the test runner and executes it during the test run phase.
|
|
3477
|
+
* The provided description will be used for test reporting and identification.
|
|
3478
|
+
*
|
|
3479
|
+
* @example
|
|
3480
|
+
* ```ts
|
|
3481
|
+
* test.invoke('should add two numbers correctly', (a, b) => {
|
|
3482
|
+
* expect(a + b).toBe(3);
|
|
3483
|
+
* }, [1, 2], 1000);
|
|
3484
|
+
* ```
|
|
3485
|
+
*
|
|
3486
|
+
* @see FunctionType
|
|
3487
|
+
* @since 1.0.0
|
|
3488
|
+
*/
|
|
3489
|
+
invoke(description: string, block: FunctionType, args?: Array<unknown>, timeout?: number): void;
|
|
3490
|
+
}
|
|
3491
|
+
|
|
3492
|
+
/**
|
|
3493
|
+
* Import will remove at compile time
|
|
3494
|
+
*/
|
|
3495
|
+
/**
|
|
3496
|
+
* Converts a template string and input data into an array of structured objects
|
|
3497
|
+
*
|
|
3498
|
+
* @param templateString - Template string containing column headings separated by pipe characters
|
|
3499
|
+
* @param inputData - Array of values to be organized according to the template structure
|
|
3500
|
+
* @returns Array of objects where each object represents a row with properties named after the headings
|
|
3501
|
+
*
|
|
3502
|
+
* @throws Error - When template headings are empty or missing pipe delimiters
|
|
3503
|
+
* @throws Error - When the input data length is not a multiple of the headings length
|
|
3504
|
+
*
|
|
3505
|
+
* @example
|
|
3506
|
+
* ```ts
|
|
3507
|
+
* const template = parseTemplate(`name|age|role`, ['John', 30, 'Developer', 'Jane', 25, 'Designer']);
|
|
3508
|
+
* // Returns: [
|
|
3509
|
+
* // { name: 'John', age: 30, role: 'Developer' },
|
|
3510
|
+
* // { name: 'Jane', age: 25, role: 'Designer' }
|
|
3511
|
+
* // ]
|
|
3512
|
+
* ```
|
|
3513
|
+
*
|
|
3514
|
+
* @see each - Function that uses parseTemplate for test case generation
|
|
3515
|
+
*
|
|
3516
|
+
* @since 1.0.0
|
|
3517
|
+
*/
|
|
3518
|
+
declare function parseTemplate(templateString: TemplateStringsArray, inputData: Array<unknown>): Array<Record<string, unknown>>;
|
|
3519
|
+
/**
|
|
3520
|
+
* Executes a function block as a test case with the specified description
|
|
3521
|
+
*
|
|
3522
|
+
* @template T - Type extending Array of unknown values
|
|
3523
|
+
* @param executor - The function that will execute each test case
|
|
3524
|
+
* @param args - Arguments representing test cases or a tagged template literal
|
|
3525
|
+
* @returns A function that accepts a test name, test block function, and optional timeout
|
|
3526
|
+
*
|
|
3527
|
+
* @throws Error - When called with fewer than 2 arguments
|
|
3528
|
+
*
|
|
3529
|
+
* @remarks
|
|
3530
|
+
* This method provides a way to directly invoke a test function with optional arguments.
|
|
3531
|
+
* It is typically used with the `each` function to create parameterized test cases.
|
|
3532
|
+
* The provided description will be used for test reporting and identification.
|
|
3533
|
+
*
|
|
3534
|
+
* The description supports parameter formatting with the following placeholders:
|
|
3535
|
+
* - %p - pretty-format output
|
|
3536
|
+
* - %s - String value
|
|
3537
|
+
* - %d, %i - Number as integer
|
|
3538
|
+
* - %f - Floating point value
|
|
3539
|
+
* - %j - JSON string
|
|
3540
|
+
* - %o - Object representation
|
|
3541
|
+
* - %# - Index of the test case
|
|
3542
|
+
* - %% - Single percent sign (doesn't consume an argument)
|
|
3543
|
+
*
|
|
3544
|
+
* Alternatively, you can inject object properties using $variable notation:
|
|
3545
|
+
* - $variable - Injects the property value
|
|
3546
|
+
* - $variable.path.to.value - Injects nested property values (works only with own properties)
|
|
3547
|
+
* - $# - Injects the index of the test case
|
|
3548
|
+
* - Note: $variable cannot be combined with printf formatting except for %%
|
|
3549
|
+
*
|
|
3550
|
+
* @example
|
|
3551
|
+
* ```ts
|
|
3552
|
+
* // As part of the each function with direct arguments
|
|
3553
|
+
* test.each(1, 2, 3)('test with %s', (val) => {
|
|
3554
|
+
* expect(val).toBeGreaterThan(0);
|
|
3555
|
+
* });
|
|
3556
|
+
*
|
|
3557
|
+
* test.each(
|
|
3558
|
+
* { name: 'John', age: 30 },
|
|
3559
|
+
* { name: 'Jane', age: 25 }
|
|
3560
|
+
* )('User %s is %s years old', (name, age) => {
|
|
3561
|
+
* expect(typeof name).toBe('string');
|
|
3562
|
+
* expect(typeof age).toBe('number');
|
|
3563
|
+
* });
|
|
3564
|
+
*
|
|
3565
|
+
* test.each`
|
|
3566
|
+
* a | b | sum
|
|
3567
|
+
* ${ 1 } | ${ 2 } | ${ 3 }
|
|
3568
|
+
* ${ 2 } | ${ 5 } | ${ 7 }
|
|
3569
|
+
* `('adds $a + $b -> $sum', ({ a, b, sum }) => {
|
|
3570
|
+
* expect(a + b).toBe(sum);
|
|
3571
|
+
* });
|
|
3572
|
+
*
|
|
3573
|
+
* describe.each` a | b | expected ${ 1 } | ${ 2 } | ${ 3 } ${ 2 } | ${ 3 } | ${ 5 }`
|
|
3574
|
+
* ('%# adds $a and $b to get $expected', ({ a, b, expected }) => {
|
|
3575
|
+
* test('calculates sum', () => {
|
|
3576
|
+
* expect(a + b).toBe(expected);
|
|
3577
|
+
* });
|
|
3578
|
+
* });
|
|
3579
|
+
* ```
|
|
3580
|
+
*
|
|
3581
|
+
* @see InvokeType - Type definition for the executor function
|
|
3582
|
+
* @see FunctionType - Type definition for the test block function
|
|
3583
|
+
* @see parseTemplate - Function used to parse tagged template literals
|
|
3584
|
+
*
|
|
3585
|
+
* @since 1.0.0
|
|
3586
|
+
*/
|
|
3587
|
+
declare function each<T extends Array<unknown>>(executor: InvokeType, ...args: T): (name: string, blockFn: FunctionType, timeout?: number) => void;
|
|
3588
|
+
|
|
3589
|
+
/**
|
|
3590
|
+
* Import will remove at compile time
|
|
3591
|
+
*/
|
|
3592
|
+
/**
|
|
3593
|
+
* Type definition for a test executor function
|
|
3594
|
+
*
|
|
3595
|
+
* @since 1.0.0
|
|
3596
|
+
*/
|
|
3597
|
+
type InvokeType = (description: string, block: FunctionType, args: Array<unknown>, timeout?: number) => void;
|
|
3598
|
+
|
|
3599
|
+
/**
|
|
3600
|
+
* A regular expression pattern used to match variable-like strings prefixed with a `$` symbol.
|
|
3601
|
+
*
|
|
3602
|
+
* @default /\$([#\\w.])+/g
|
|
3603
|
+
*
|
|
3604
|
+
* @see resolveVariable
|
|
3605
|
+
* @see interpolateVariables
|
|
3606
|
+
*
|
|
3607
|
+
* @since 1.0.0
|
|
3608
|
+
*/
|
|
3609
|
+
/**
|
|
3610
|
+
* Formats a given value into a human-readable string representation.
|
|
3611
|
+
*
|
|
3612
|
+
* @param value - The value to be formatted
|
|
3613
|
+
* @returns The formatted string representation of the input value
|
|
3614
|
+
*
|
|
3615
|
+
* @remarks
|
|
3616
|
+
* This method ensures that non-string values are converted into a JSON string with
|
|
3617
|
+
* indentation for easier readability. If the input is a string, it is returned as-is.
|
|
3618
|
+
*
|
|
3619
|
+
* @example
|
|
3620
|
+
* ```ts
|
|
3621
|
+
* // String input is returned unchanged
|
|
3622
|
+
* const str = prettyFormat("hello"); // Returns: "hello"
|
|
3623
|
+
*
|
|
3624
|
+
* // Objects are converted to indented JSON
|
|
3625
|
+
* const obj = prettyFormat({ name: "John", age: 30 });
|
|
3626
|
+
* // Returns:
|
|
3627
|
+
* // {
|
|
3628
|
+
* // "name": "John",
|
|
3629
|
+
* // "age": 30
|
|
3630
|
+
* // }
|
|
3631
|
+
* ```
|
|
3632
|
+
*
|
|
3633
|
+
* @see printf
|
|
3634
|
+
*
|
|
3635
|
+
* @since 1.0.0
|
|
3636
|
+
*/
|
|
3637
|
+
declare function prettyFormat(value: unknown): string;
|
|
3638
|
+
/**
|
|
3639
|
+
* Retrieves a value from a nested object structure based on a specified path.
|
|
3640
|
+
*
|
|
3641
|
+
* @param data - The object containing the nested data to traverse
|
|
3642
|
+
* @param path - An array of strings representing the sequence of keys to access the desired value
|
|
3643
|
+
* @returns The value found at the specified path within the object, or `undefined` if the path does not exist
|
|
3644
|
+
*
|
|
3645
|
+
* @remarks
|
|
3646
|
+
* This function safely traverses the given object structure without throwing errors
|
|
3647
|
+
* for missing keys or null/undefined values.
|
|
3648
|
+
*
|
|
3649
|
+
* @example
|
|
3650
|
+
* ```ts
|
|
3651
|
+
* const data = {
|
|
3652
|
+
* user: {
|
|
3653
|
+
* profile: {
|
|
3654
|
+
* name: "John Doe",
|
|
3655
|
+
* email: "john@example.com"
|
|
3656
|
+
* }
|
|
3657
|
+
* }
|
|
3658
|
+
* };
|
|
3659
|
+
*
|
|
3660
|
+
* const name = getValueByPath(data, ["user", "profile", "name"]);
|
|
3661
|
+
* // Returns: "John Doe"
|
|
3662
|
+
*
|
|
3663
|
+
* const missing = getValueByPath(data, ["user", "settings", "theme"]);
|
|
3664
|
+
* // Returns: undefined
|
|
3665
|
+
* ```
|
|
3666
|
+
*
|
|
3667
|
+
* @see resolveVariable
|
|
3668
|
+
*
|
|
3669
|
+
* @since 1.0.0
|
|
3670
|
+
*/
|
|
3671
|
+
declare function getValueByPath(data: Record<string, unknown>, path: string[]): unknown;
|
|
3672
|
+
/**
|
|
3673
|
+
* Resolves a variable token into its corresponding value from the provided data object.
|
|
3674
|
+
*
|
|
3675
|
+
* @param token - The variable token to resolve
|
|
3676
|
+
* @param data - The object containing data used for resolving the variable token
|
|
3677
|
+
* @param arrayIndex - The index value to be used if the token represents an array index (`$#`)
|
|
3678
|
+
* @returns The resolved value as a string, or the original token if resolution fails
|
|
3679
|
+
*
|
|
3680
|
+
* @remarks
|
|
3681
|
+
* This function assumes the use of dot notation in token paths to access nested properties
|
|
3682
|
+
* within the `data` object. Tokens starting with `$#` will be replaced by the array index.
|
|
3683
|
+
*
|
|
3684
|
+
* @example
|
|
3685
|
+
* ```ts
|
|
3686
|
+
* const data = {
|
|
3687
|
+
* user: {
|
|
3688
|
+
* name: "John"
|
|
3689
|
+
* }
|
|
3690
|
+
* };
|
|
3691
|
+
*
|
|
3692
|
+
* const value1 = resolveVariable("$user.name", data, 0);
|
|
3693
|
+
* // Returns: "John"
|
|
3694
|
+
*
|
|
3695
|
+
* const value2 = resolveVariable("$#", data, 5);
|
|
3696
|
+
* // Returns: "5"
|
|
3697
|
+
* ```
|
|
3698
|
+
*
|
|
3699
|
+
* @see getValueByPath
|
|
3700
|
+
* @see interpolateVariables
|
|
3701
|
+
*
|
|
3702
|
+
* @since 1.0.0
|
|
3703
|
+
*/
|
|
3704
|
+
declare function resolveVariable(token: string, data: Record<string, unknown>, arrayIndex: number): string;
|
|
3705
|
+
/**
|
|
3706
|
+
* Replaces variable placeholders within a template string with corresponding values from a provided data object.
|
|
3707
|
+
*
|
|
3708
|
+
* @param template - The template string containing variable placeholders
|
|
3709
|
+
* @param data - An object mapping variable names to their respective values
|
|
3710
|
+
* @param arrayIndex - The index to be applied when resolving variables that reference array values
|
|
3711
|
+
* @returns The resulting string with all variable placeholders replaced by their resolved values
|
|
3712
|
+
*
|
|
3713
|
+
* @throws Error - If a required variable cannot be resolved
|
|
3714
|
+
*
|
|
3715
|
+
* @remarks
|
|
3716
|
+
* The function uses a regular expression to identify variable placeholders within the template string
|
|
3717
|
+
* and replaces them with corresponding values derived from the `data` object.
|
|
3718
|
+
*
|
|
3719
|
+
* @example
|
|
3720
|
+
* ```ts
|
|
3721
|
+
* const data = {
|
|
3722
|
+
* user: {
|
|
3723
|
+
* name: "Jane",
|
|
3724
|
+
* id: 42
|
|
3725
|
+
* },
|
|
3726
|
+
* status: "active"
|
|
3727
|
+
* };
|
|
3728
|
+
*
|
|
3729
|
+
* const result = interpolateVariables("User $user.name ($user.id) is $status", data, 0);
|
|
3730
|
+
* // Returns: "User Jane (42) is active"
|
|
3731
|
+
* ```
|
|
3732
|
+
*
|
|
3733
|
+
* @see VARIABLE_PATTERN
|
|
3734
|
+
* @see resolveVariable
|
|
3735
|
+
*
|
|
3736
|
+
* @since 1.0.0
|
|
3737
|
+
*/
|
|
3738
|
+
declare function interpolateVariables(template: string, data: Record<string, unknown>, arrayIndex: number): string;
|
|
3739
|
+
/**
|
|
3740
|
+
* Formats a given string by interpolating it with the provided parameters.
|
|
3741
|
+
*
|
|
3742
|
+
* @param description - The format string containing variables or tokens to be replaced
|
|
3743
|
+
* @param params - An array of values to interpolate or substitute into the format string
|
|
3744
|
+
* @param index - An index used for specific token replacements (e.g., `%#`)
|
|
3745
|
+
* @returns The formatted string after performing substitutions and formatting
|
|
3746
|
+
*
|
|
3747
|
+
* @remarks
|
|
3748
|
+
* This function supports two formatting approaches:
|
|
3749
|
+
* 1. `$variable` style interpolation using the first object in the `params` array
|
|
3750
|
+
* 2. Printf-style format specifiers with the following options:
|
|
3751
|
+
* - `%p`: Pretty prints a value
|
|
3752
|
+
* - `%s`: Converts value to a string
|
|
3753
|
+
* - `%d`: Converts value to a numeric string
|
|
3754
|
+
* - `%i`: Converts value to an integer string (floor of the number)
|
|
3755
|
+
* - `%f`: Converts value to a floating-point string
|
|
3756
|
+
* - `%j`: Converts value to its JSON representation
|
|
3757
|
+
* - `%o`: Outputs the object's `toString` representation
|
|
3758
|
+
* - `%#`: Replaced with the provided `index`
|
|
3759
|
+
* - `%%`: Escapes the `%` character
|
|
3760
|
+
*
|
|
3761
|
+
* If the `description` contains `$`-style variables, it will interpolate those using the first object in the `params` array.
|
|
3762
|
+
* This behavior does not apply if the string also contains `%%`.
|
|
3763
|
+
*
|
|
3764
|
+
* @example
|
|
3765
|
+
* ```ts
|
|
3766
|
+
* // Format with printf-style specifiers
|
|
3767
|
+
* const result1 = printf("Value: %s, Number: %d", ["test", 42], 0);
|
|
3768
|
+
* // Returns: "Value: test, Number: 42"
|
|
3769
|
+
*
|
|
3770
|
+
* // Format with variable interpolation
|
|
3771
|
+
* const data = { name: "Alice", age: 30 };
|
|
3772
|
+
* const result2 = printf("Name: $name, Age: $age", [data], 0);
|
|
3773
|
+
* // Returns: "Name: Alice, Age: 30"
|
|
3774
|
+
* ```
|
|
3775
|
+
*
|
|
3776
|
+
* @see prettyFormat
|
|
3777
|
+
* @see interpolateVariables
|
|
3778
|
+
*
|
|
3779
|
+
* @since 1.0.0
|
|
3780
|
+
*/
|
|
3781
|
+
declare function printf(description: string, params: Array<unknown>, index: number): string;
|
|
3782
|
+
|
|
3783
|
+
/**
|
|
3784
|
+
* Import will remove at compile time
|
|
3785
|
+
*/
|
|
3786
|
+
/**
|
|
3787
|
+
* Implementation of the describe directive that allows for test suite definition with chainable modifiers.
|
|
3788
|
+
* Acts as a function that can be invoked directly while also providing chainable property access.
|
|
3789
|
+
*
|
|
3790
|
+
* @param description - The test suite description
|
|
3791
|
+
* @param block - The test suite implementation function
|
|
3792
|
+
*
|
|
3793
|
+
* @throws Error - When attempting to nest describe blocks inside tests
|
|
3794
|
+
* @throws Error - When using incompatible flag combinations (e.g., skip with only)
|
|
3795
|
+
*
|
|
3796
|
+
* @remarks
|
|
3797
|
+
* This class extends Function to allow it to be invoked as a function while also
|
|
3798
|
+
* providing methods and properties. It uses a Proxy to capture function invocations
|
|
3799
|
+
* and redirects them to the invoke method.
|
|
3800
|
+
*
|
|
3801
|
+
* The describe directive creates a group of related tests, providing organization and structure
|
|
3802
|
+
* to test suites. It supports chainable modifiers for customizing test suite behavior,
|
|
3803
|
+
* and parameterized testing via the 'each' method.
|
|
3804
|
+
*
|
|
3805
|
+
* @example
|
|
3806
|
+
* ```ts
|
|
3807
|
+
* // Basic describe block
|
|
3808
|
+
* describe('Calculator', () => {
|
|
3809
|
+
* test('should add numbers', () => {
|
|
3810
|
+
* expect(2 + 2).toBe(4);
|
|
3811
|
+
* });
|
|
3812
|
+
* });
|
|
3813
|
+
*
|
|
3814
|
+
* // With modifiers
|
|
3815
|
+
* describe.only('Priority feature', () => {
|
|
3816
|
+
* test('important test case', () => {
|
|
3817
|
+
* expect(true).toBe(true);
|
|
3818
|
+
* });
|
|
3819
|
+
* });
|
|
3820
|
+
*
|
|
3821
|
+
* // With parameterized tests using each
|
|
3822
|
+
* describe.each([1, 2, 3])('Math operations for %s', (value) => {
|
|
3823
|
+
* test('square', () => {
|
|
3824
|
+
* expect(value * value).toBeGreaterThan(0);
|
|
3825
|
+
* });
|
|
3826
|
+
* });
|
|
3827
|
+
* ```
|
|
3828
|
+
*
|
|
3829
|
+
* @see SuiteState
|
|
3830
|
+
* @see DescribeDirectiveInterface
|
|
3831
|
+
*
|
|
3832
|
+
* @since 1.0.0
|
|
3833
|
+
*/
|
|
3834
|
+
declare class DescribeDirective extends Function {
|
|
3835
|
+
/**
|
|
3836
|
+
* Singleton instance of the DescribeDirective class.
|
|
3837
|
+
*
|
|
3838
|
+
* @see DescribeDirectiveInterface
|
|
3839
|
+
* @since 1.0.0
|
|
3840
|
+
*/
|
|
3841
|
+
private static instance;
|
|
3842
|
+
/**
|
|
3843
|
+
* Configuration options controlling test suite behavior
|
|
3844
|
+
*
|
|
3845
|
+
* @see DescribeOptionsInterface
|
|
3846
|
+
* @since 1.0.0
|
|
3847
|
+
*/
|
|
3848
|
+
private options;
|
|
3849
|
+
/**
|
|
3850
|
+
* Returns the singleton instance of DescribeDirective, creating it if it doesn't exist
|
|
3851
|
+
*
|
|
3852
|
+
* @returns The singleton instance of the describe directive implementation
|
|
3853
|
+
*
|
|
3854
|
+
* @remarks
|
|
3855
|
+
* This method implements the singleton pattern, ensuring only one instance of the describe directive
|
|
3856
|
+
* exists throughout the application lifecycle.
|
|
3857
|
+
*
|
|
3858
|
+
* @see DescribeDirectiveInterface
|
|
3859
|
+
* @since 1.0.0
|
|
3860
|
+
*/
|
|
3861
|
+
static getInstance(): DescribeDirectiveInterface;
|
|
3862
|
+
/**
|
|
3863
|
+
* Creates a skipped test suite that will be recognized but not executed during test runs
|
|
3864
|
+
*
|
|
3865
|
+
* @returns The same DescribeDirective instance with skip flag enabled, allowing for method chaining
|
|
3866
|
+
* @throws Error - When attempting to combine with 'only' flag which would create conflicting behavior
|
|
3867
|
+
*
|
|
3868
|
+
* @remarks
|
|
3869
|
+
* When applied, the test suite will be marked as skipped in test reports but won't be executed.
|
|
3870
|
+
* Cannot be combined with the 'only' modifier due to conflicting behavior.
|
|
3871
|
+
*
|
|
3872
|
+
* @example
|
|
3873
|
+
* ```ts
|
|
3874
|
+
* describe.skip('temporarily disabled suite', () => {
|
|
3875
|
+
* test('will not run', () => {
|
|
3876
|
+
* expect(result).toBe(true);
|
|
3877
|
+
* });
|
|
3878
|
+
* });
|
|
3879
|
+
* ```
|
|
3880
|
+
*
|
|
3881
|
+
* @see DescribeDirectiveInterface
|
|
3882
|
+
* @since 1.0.0
|
|
3883
|
+
*/
|
|
3884
|
+
get skip(): this;
|
|
3885
|
+
/**
|
|
3886
|
+
* Creates an exclusive test suite that will run while other non-exclusive suites are skipped
|
|
3887
|
+
*
|
|
3888
|
+
* @returns The same DescribeDirective instance with only flag enabled, allowing for method chaining
|
|
3889
|
+
* @throws Error - When attempting to combine with 'skip' flag which would create conflicting behavior
|
|
3890
|
+
*
|
|
3891
|
+
* @remarks
|
|
3892
|
+
* When applied, only this test suite and other suites marked with 'only' will be executed.
|
|
3893
|
+
* This is useful for focusing on specific test suites during development or debugging.
|
|
3894
|
+
* Cannot be combined with the 'skip' modifier due to conflicting behavior.
|
|
3895
|
+
*
|
|
3896
|
+
* @example
|
|
3897
|
+
* ```ts
|
|
3898
|
+
* describe.only('focus on this suite', () => {
|
|
3899
|
+
* test('will run', () => {
|
|
3900
|
+
* expect(result).toBe(expectedValue);
|
|
3901
|
+
* });
|
|
3902
|
+
* });
|
|
3903
|
+
* ```
|
|
3904
|
+
*
|
|
3905
|
+
* @see DescribeDirectiveInterface
|
|
3906
|
+
* @since 1.0.0
|
|
3907
|
+
*/
|
|
3908
|
+
get only(): this;
|
|
3909
|
+
/**
|
|
3910
|
+
* Generates parameterized test suites from template strings and placeholders.
|
|
3911
|
+
*
|
|
3912
|
+
* @template T - An array type extending ReadonlyArray<unknown> that contains the placeholder values
|
|
3913
|
+
*
|
|
3914
|
+
* @param string - A template string array that defines the structure of test suite descriptions
|
|
3915
|
+
* @param placeholders - Values to be inserted into the template strings to create test suites
|
|
3916
|
+
*
|
|
3917
|
+
* @returns A callback function compatible with Jest's describe method that provides parameterized test data
|
|
3918
|
+
*
|
|
3919
|
+
* @throws TypeError - When the number of placeholders doesn't match the template string requirements
|
|
3920
|
+
*
|
|
3921
|
+
* @remarks
|
|
3922
|
+
* This method uses tagged template literals to create parameterized test suites.
|
|
3923
|
+
* The values provided as placeholders will be passed to describe callbacks as named parameters.
|
|
3924
|
+
*
|
|
3925
|
+
* The returned function accepts the following parameters:
|
|
3926
|
+
* - name: String - The title of the describe block.
|
|
3927
|
+
* Generate unique describe titles by positionally injecting parameters with printf formatting:
|
|
3928
|
+
* %p - pretty-format.
|
|
3929
|
+
* %s - String.
|
|
3930
|
+
* %d - Number.
|
|
3931
|
+
* %i - Integer.
|
|
3932
|
+
* %f - Floating point value.
|
|
3933
|
+
* %j - JSON.
|
|
3934
|
+
* %o - Object.
|
|
3935
|
+
* %# - Index of the test suite.
|
|
3936
|
+
* %% - single percent sign ('%'). This does not consume an argument.
|
|
3937
|
+
* Or generate unique describe titles by injecting properties of test suite object with $variable:
|
|
3938
|
+
* - To inject nested object values use a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
3939
|
+
* - You can use $# to inject the index of the test suite
|
|
3940
|
+
* - You cannot use $variable with the printf formatting except for %%
|
|
3941
|
+
* - fn: Function - The describe callback to be run, this is the function that will receive the parameters in each row as function arguments.
|
|
3942
|
+
* - timeout (optional): Number - Time in milliseconds to wait for each row before aborting. Default is 5 seconds.
|
|
3943
|
+
*
|
|
3944
|
+
* @example
|
|
3945
|
+
* ```ts
|
|
3946
|
+
* describe.each`
|
|
3947
|
+
* a | b | expected
|
|
3948
|
+
* ${1} | ${1} | ${2}
|
|
3949
|
+
* ${1} | ${2} | ${3}
|
|
3950
|
+
* ${2} | ${2} | ${4}
|
|
3951
|
+
* `('addition: $a + $b = $expected', ({a, b, expected}) => {
|
|
3952
|
+
* test('equals expected value', () => {
|
|
3953
|
+
* expect(a + b).toBe(expected);
|
|
3954
|
+
* });
|
|
3955
|
+
* });
|
|
3956
|
+
* ```
|
|
3957
|
+
*
|
|
3958
|
+
* @see DescribeCallbackType
|
|
3959
|
+
* @since 1.0.0
|
|
3960
|
+
*/
|
|
3961
|
+
each<T extends ReadonlyArray<unknown>>(string: TemplateStringsArray, ...placeholders: T): DescribeCallbackType<Record<string, T[number]>>;
|
|
3962
|
+
/**
|
|
3963
|
+
* Creates parameterized test suites from arrays of test case values.
|
|
3964
|
+
*
|
|
3965
|
+
* @template T - A type extending Array<unknown> or [unknown] that contains the test case values
|
|
3966
|
+
*
|
|
3967
|
+
* @param cases - Arrays of values representing individual test cases
|
|
3968
|
+
*
|
|
3969
|
+
* @returns A callback function compatible with Jest's describe method that provides parameterized test data
|
|
3970
|
+
*
|
|
3971
|
+
* @throws TypeError - When the test cases have inconsistent formats or types
|
|
3972
|
+
*
|
|
3973
|
+
* @remarks
|
|
3974
|
+
* This method allows creating parameterized test suites from arrays of values.
|
|
3975
|
+
* Each array provided as a case will be passed to the describe callback as separate arguments.
|
|
3976
|
+
*
|
|
3977
|
+
* The returned function accepts the following parameters:
|
|
3978
|
+
* - name: String - The title of the describe block.
|
|
3979
|
+
* Generate unique describe titles by positionally injecting parameters with printf formatting:
|
|
3980
|
+
* %p - pretty-format.
|
|
3981
|
+
* %s - String.
|
|
3982
|
+
* %d - Number.
|
|
3983
|
+
* %i - Integer.
|
|
3984
|
+
* %f - Floating point value.
|
|
3985
|
+
* %j - JSON.
|
|
3986
|
+
* %o - Object.
|
|
3987
|
+
* %# - Index of the test suite.
|
|
3988
|
+
* %% - single percent sign ('%'). This does not consume an argument.
|
|
3989
|
+
* Or generate unique describe titles by injecting properties of test suite object with $variable:
|
|
3990
|
+
* - To inject nested object values use a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
3991
|
+
* - You can use $# to inject the index of the test suite
|
|
3992
|
+
* - You cannot use $variable with the printf formatting except for %%
|
|
3993
|
+
* - fn: Function - The describe callback to be run, this is the function that will receive the parameters in each row as function arguments.
|
|
3994
|
+
* - timeout (optional): Number - Time in milliseconds to wait for each row before aborting. Default is 5 seconds.
|
|
3995
|
+
*
|
|
3996
|
+
* @example
|
|
3997
|
+
* ```ts
|
|
3998
|
+
* describe.each(
|
|
3999
|
+
* [1, 1, 2],
|
|
4000
|
+
* [1, 2, 3],
|
|
4001
|
+
* [2, 2, 4]
|
|
4002
|
+
* )('addition: %d + %d = %d', (a, b, expected) => {
|
|
4003
|
+
* test('equals expected value', () => {
|
|
4004
|
+
* expect(a + b).toBe(expected);
|
|
4005
|
+
* });
|
|
4006
|
+
* });
|
|
4007
|
+
* ```
|
|
4008
|
+
*
|
|
4009
|
+
* @see DescribeCallbackType
|
|
4010
|
+
* @since 1.0.0
|
|
4011
|
+
*/
|
|
4012
|
+
each<T extends Array<unknown> | [unknown]>(...cases: T[]): DescribeCallbackType<T>;
|
|
4013
|
+
/**
|
|
4014
|
+
* Creates parameterized test suites from individual test case values.
|
|
4015
|
+
*
|
|
4016
|
+
* @template T - The type of test case values (objects or primitives)
|
|
4017
|
+
*
|
|
4018
|
+
* @param args - Individual test case values to be used in the test suites
|
|
4019
|
+
*
|
|
4020
|
+
* @returns A callback function compatible with Jest's describe method that provides parameterized test data
|
|
4021
|
+
*
|
|
4022
|
+
* @throws TypeError - When the test cases have incompatible types
|
|
4023
|
+
*
|
|
4024
|
+
* @remarks
|
|
4025
|
+
* This method allows creating parameterized test suites from individual values.
|
|
4026
|
+
* Each value provided as an argument will be passed to the describe callback.
|
|
4027
|
+
* This overload is particularly useful for objects and primitive values.
|
|
4028
|
+
*
|
|
4029
|
+
* The returned function accepts the following parameters:
|
|
4030
|
+
* - name: String - The title of the describe block.
|
|
4031
|
+
* Generate unique describe titles by positionally injecting parameters with printf formatting:
|
|
4032
|
+
* %p - pretty-format.
|
|
4033
|
+
* %s - String.
|
|
4034
|
+
* %d - Number.
|
|
4035
|
+
* %i - Integer.
|
|
4036
|
+
* %f - Floating point value.
|
|
4037
|
+
* %j - JSON.
|
|
4038
|
+
* %o - Object.
|
|
4039
|
+
* %# - Index of the test suite.
|
|
4040
|
+
* %% - single percent sign ('%'). This does not consume an argument.
|
|
4041
|
+
* Or generate unique describe titles by injecting properties of test suite object with $variable:
|
|
4042
|
+
* - To inject nested object values use a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4043
|
+
* - You can use $# to inject the index of the test suite
|
|
4044
|
+
* - You cannot use $variable with the printf formatting except for %%
|
|
4045
|
+
* - fn: Function - The describe callback to be run, this is the function that will receive the test case value as an argument.
|
|
4046
|
+
* - timeout (optional): Number - Time in milliseconds to wait for each test case before aborting. Default is 5 seconds.
|
|
4047
|
+
*
|
|
4048
|
+
* @example
|
|
4049
|
+
* ```ts
|
|
4050
|
+
* describe.each(
|
|
4051
|
+
* { a: 1, b: 1, expected: 2 },
|
|
4052
|
+
* { a: 1, b: 2, expected: 3 },
|
|
4053
|
+
* { a: 2, b: 2, expected: 4 }
|
|
4054
|
+
* )('addition: $a + $b = $expected', ({ a, b, expected }) => {
|
|
4055
|
+
* test('equals expected value', () => {
|
|
4056
|
+
* expect(a + b).toBe(expected);
|
|
4057
|
+
* });
|
|
4058
|
+
* });
|
|
4059
|
+
* ```
|
|
4060
|
+
*
|
|
4061
|
+
* @see DescribeCallbackType
|
|
4062
|
+
* @since 1.0.0
|
|
4063
|
+
*/
|
|
4064
|
+
each<T>(...args: readonly T[]): DescribeCallbackType<T>;
|
|
4065
|
+
/**
|
|
4066
|
+
* Creates parameterized test suites from individual objects or primitive values.
|
|
4067
|
+
*
|
|
4068
|
+
* @template T - The type of test case values
|
|
4069
|
+
*
|
|
4070
|
+
* @param args - Individual objects or primitive values to be used as test cases
|
|
4071
|
+
*
|
|
4072
|
+
* @returns A callback function compatible with Jest's describe method
|
|
4073
|
+
*
|
|
4074
|
+
* @remarks
|
|
4075
|
+
* This method creates parameterized test suites where each argument represents a complete test case.
|
|
4076
|
+
* Useful for testing with objects or primitive values that each represent a single test scenario.
|
|
4077
|
+
*
|
|
4078
|
+
* The returned function accepts the following parameters:
|
|
4079
|
+
* - name: String - The title of the describe block.
|
|
4080
|
+
* Generate unique describe titles by positionally injecting parameters with printf formatting:
|
|
4081
|
+
* %p - pretty-format.
|
|
4082
|
+
* %s - String.
|
|
4083
|
+
* %d - Number.
|
|
4084
|
+
* %i - Integer.
|
|
4085
|
+
* %f - Floating point value.
|
|
4086
|
+
* %j - JSON.
|
|
4087
|
+
* %o - Object.
|
|
4088
|
+
* %# - Index of the test suite.
|
|
4089
|
+
* %% - single percent sign ('%'). This does not consume an argument.
|
|
4090
|
+
* Or generate unique describe titles by injecting properties of test suite object with $variable:
|
|
4091
|
+
* - To inject nested object values use a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4092
|
+
* - You can use $# to inject the index of the test suite
|
|
4093
|
+
* - You cannot use $variable with the printf formatting except for %%
|
|
4094
|
+
* - fn: Function - The describe callback to be run, this is the function that will receive the test case value as an argument.
|
|
4095
|
+
* - timeout (optional): Number - Time in milliseconds to wait for each test case before aborting. Default is 5 seconds.
|
|
4096
|
+
*
|
|
4097
|
+
* @example
|
|
4098
|
+
* ```ts
|
|
4099
|
+
* describe.each(
|
|
4100
|
+
* { input: 'hello', expected: 'HELLO' },
|
|
4101
|
+
* { input: 'world', expected: 'WORLD' }
|
|
4102
|
+
* )('toUpperCase($input) → $expected', (testCase) => {
|
|
4103
|
+
* test('converts correctly', () => {
|
|
4104
|
+
* expect(testCase.input.toUpperCase()).toBe(testCase.expected);
|
|
4105
|
+
* });
|
|
4106
|
+
* });
|
|
4107
|
+
* ```
|
|
4108
|
+
*
|
|
4109
|
+
* @see DescribeCallbackType
|
|
4110
|
+
* @since 1.0.0
|
|
4111
|
+
*/
|
|
4112
|
+
each<T extends Array<unknown>>(...args: T): DescribeCallbackType<T[number]>;
|
|
4113
|
+
}
|
|
4114
|
+
|
|
4115
|
+
/**
|
|
4116
|
+
* Import will remove at compile time
|
|
4117
|
+
*/
|
|
4118
|
+
/**
|
|
4119
|
+
* Represents a callback function type used for parameterized test suite definitions.
|
|
4120
|
+
*
|
|
4121
|
+
* @template T - The type of arguments passed to the test function
|
|
4122
|
+
*
|
|
4123
|
+
* @param name - The title of the describe block, which can include parameter placeholders
|
|
4124
|
+
* @param fn - The function containing the tests to be run, which receives the test parameters
|
|
4125
|
+
*
|
|
4126
|
+
* @returns void
|
|
4127
|
+
*
|
|
4128
|
+
* @remarks
|
|
4129
|
+
* This type definition is used for creating parameterized test suites where test data
|
|
4130
|
+
* is passed to the test function. The name parameter supports various placeholder formats
|
|
4131
|
+
* to incorporate test data into the test suite title.
|
|
4132
|
+
*
|
|
4133
|
+
* @since 1.0.0
|
|
4134
|
+
*/
|
|
4135
|
+
type DescribeCallbackType<T> = (name: string, fn: (args: T) => void) => void;
|
|
4136
|
+
/**
|
|
4137
|
+
* Interface defining the describe directive functionality for test organization.
|
|
4138
|
+
*
|
|
4139
|
+
* The describe directive creates a block that groups together related tests.
|
|
4140
|
+
* It serves as the primary organizational structure for test suites, allowing
|
|
4141
|
+
* developers to group related test cases and create a hierarchy of test blocks.
|
|
4142
|
+
*
|
|
4143
|
+
* @template T - The type parameter for the describe directive
|
|
4144
|
+
*
|
|
4145
|
+
* @remarks
|
|
4146
|
+
* Describe blocks can be nested within each other to create logical groupings of tests.
|
|
4147
|
+
* They can also have before/after hooks attached to them for setup and teardown code.
|
|
4148
|
+
*
|
|
4149
|
+
* @example
|
|
4150
|
+
* ```ts
|
|
4151
|
+
* describe('Calculator', () => {
|
|
4152
|
+
* describe('add method', () => {
|
|
4153
|
+
* test('adds two positive numbers', () => {
|
|
4154
|
+
* expect(calculator.add(1, 2)).toBe(3);
|
|
4155
|
+
* });
|
|
4156
|
+
*
|
|
4157
|
+
* test('adds a positive and negative number', () => {
|
|
4158
|
+
* expect(calculator.add(1, -2)).toBe(-1);
|
|
4159
|
+
* });
|
|
4160
|
+
* });
|
|
4161
|
+
* });
|
|
4162
|
+
* ```
|
|
4163
|
+
*
|
|
4164
|
+
* @see TestCase
|
|
4165
|
+
* @see TestDirectiveInterface
|
|
4166
|
+
*
|
|
4167
|
+
* @since 1.0.0
|
|
4168
|
+
*/
|
|
4169
|
+
interface DescribeDirectiveInterface {
|
|
4170
|
+
/**
|
|
4171
|
+
* Executes a test suite block with the given name and test function.
|
|
4172
|
+
*
|
|
4173
|
+
* @param name - The title of the test suite
|
|
4174
|
+
* @param fn - The function containing the tests to be run within this suite
|
|
4175
|
+
*
|
|
4176
|
+
* @returns void
|
|
4177
|
+
*
|
|
4178
|
+
* @example
|
|
4179
|
+
* ```ts
|
|
4180
|
+
* describe('User authentication', () => {
|
|
4181
|
+
* test('should log in with valid credentials', () => {
|
|
4182
|
+
* // Test implementation
|
|
4183
|
+
* expect(login('user', 'password')).toBe(true);
|
|
4184
|
+
* });
|
|
4185
|
+
*
|
|
4186
|
+
* test('should fail with invalid credentials', () => {
|
|
4187
|
+
* // Test implementation
|
|
4188
|
+
* expect(login('user', 'wrong')).toBe(false);
|
|
4189
|
+
* });
|
|
4190
|
+
* });
|
|
4191
|
+
* ```
|
|
4192
|
+
*
|
|
4193
|
+
* @since 1.0.0
|
|
4194
|
+
*/
|
|
4195
|
+
(name: string, fn: () => void): void;
|
|
4196
|
+
/**
|
|
4197
|
+
* Marks this describe to be skipped during test execution.
|
|
4198
|
+
*
|
|
4199
|
+
* @returns this - The current instance for method chaining
|
|
4200
|
+
*
|
|
4201
|
+
* @example
|
|
4202
|
+
* ```ts
|
|
4203
|
+
* describe.skip('Features under development', () => {
|
|
4204
|
+
* test('new feature', () => {
|
|
4205
|
+
* // This test will be skipped
|
|
4206
|
+
* });
|
|
4207
|
+
* });
|
|
4208
|
+
* ```
|
|
4209
|
+
*
|
|
4210
|
+
* @since 1.0.0
|
|
4211
|
+
*/
|
|
4212
|
+
get skip(): this;
|
|
4213
|
+
/**
|
|
4214
|
+
* Marks this test suite to be the only one executed in the current context.
|
|
4215
|
+
*
|
|
4216
|
+
* @returns this - The current instance for method chaining
|
|
4217
|
+
*
|
|
4218
|
+
* @example
|
|
4219
|
+
* ```ts
|
|
4220
|
+
* describe.only('Critical functionality', () => {
|
|
4221
|
+
* test('core feature', () => {
|
|
4222
|
+
* // Only this suite will run
|
|
4223
|
+
* });
|
|
4224
|
+
* });
|
|
4225
|
+
* ```
|
|
4226
|
+
*
|
|
4227
|
+
* @since 1.0.0
|
|
4228
|
+
*/
|
|
4229
|
+
get only(): this;
|
|
4230
|
+
/**
|
|
4231
|
+
* Creates parameterized test suites using template literals with placeholders.
|
|
4232
|
+
*
|
|
4233
|
+
* @template T - Array type representing the test case data
|
|
4234
|
+
*
|
|
4235
|
+
* @param string - Template string with placeholders for test data
|
|
4236
|
+
* @param placeholders - Values to be substituted into the template string placeholders
|
|
4237
|
+
*
|
|
4238
|
+
* @returns A callback function that accepts a test name pattern and implementation function
|
|
4239
|
+
*
|
|
4240
|
+
* @remarks
|
|
4241
|
+
* The name parameter can include formatters:
|
|
4242
|
+
* - Generate unique test titles by positionally injecting parameters with printf formatting:
|
|
4243
|
+
* - %p - pretty-format
|
|
4244
|
+
* - %s - String
|
|
4245
|
+
* - %d - Number
|
|
4246
|
+
* - %i - Integer
|
|
4247
|
+
* - %f - Floating point value
|
|
4248
|
+
* - %j - JSON
|
|
4249
|
+
* - %o - Object
|
|
4250
|
+
* - %# - Index of the test case
|
|
4251
|
+
* - %% - Single percent sign ('%'). This does not consume an argument
|
|
4252
|
+
* - Or generate unique test titles by injecting properties of test case object with $variable:
|
|
4253
|
+
* - To inject nested object values supply a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4254
|
+
* - Use $# to inject the index of the test case
|
|
4255
|
+
* - You cannot use $variable with printf formatting except for %%
|
|
4256
|
+
*
|
|
4257
|
+
* The fn parameter is the function that will receive the parameters in each row as function arguments.
|
|
4258
|
+
* Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
|
|
4259
|
+
*
|
|
4260
|
+
* @example
|
|
4261
|
+
* ```ts
|
|
4262
|
+
* describe.each`
|
|
4263
|
+
* a | b | expected
|
|
4264
|
+
* ${1} | ${1} | ${2}
|
|
4265
|
+
* ${2} | ${3} | ${5}
|
|
4266
|
+
* `('$a + $b = $expected', ({ a, b, expected }) => {
|
|
4267
|
+
* test('adds correctly', () => {
|
|
4268
|
+
* expect(a + b).toBe(expected);
|
|
4269
|
+
* });
|
|
4270
|
+
* });
|
|
4271
|
+
* ```
|
|
4272
|
+
*
|
|
4273
|
+
* @see DescribeCallbackType
|
|
4274
|
+
* @since 1.0.0
|
|
4275
|
+
*/
|
|
4276
|
+
each<T extends ReadonlyArray<unknown>>(string: TemplateStringsArray, ...placeholders: T): DescribeCallbackType<Record<string, T[number]>>;
|
|
4277
|
+
/**
|
|
4278
|
+
* Creates parameterized test suites from arrays of test case values.
|
|
4279
|
+
*
|
|
4280
|
+
* @template T - Type representing arrays of test cases
|
|
4281
|
+
*
|
|
4282
|
+
* @param cases - Arrays containing the test case data
|
|
4283
|
+
*
|
|
4284
|
+
* @returns A callback function that accepts a test name pattern and implementation function
|
|
4285
|
+
*
|
|
4286
|
+
* @remarks
|
|
4287
|
+
* The name parameter can include formatters:
|
|
4288
|
+
* - Generate unique test titles by positionally injecting parameters with printf formatting:
|
|
4289
|
+
* - %p - pretty-format
|
|
4290
|
+
* - %s - String
|
|
4291
|
+
* - %d - Number
|
|
4292
|
+
* - %i - Integer
|
|
4293
|
+
* - %f - Floating point value
|
|
4294
|
+
* - %j - JSON
|
|
4295
|
+
* - %o - Object
|
|
4296
|
+
* - %# - Index of the test case
|
|
4297
|
+
* - %% - Single percent sign ('%'). This does not consume an argument
|
|
4298
|
+
* - Or generate unique test titles by injecting properties of test case object with $variable:
|
|
4299
|
+
* - To inject nested object values supply a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4300
|
+
* - Use $# to inject the index of the test case
|
|
4301
|
+
* - You cannot use $variable with printf formatting except for %%
|
|
4302
|
+
*
|
|
4303
|
+
* The fn parameter is the function that will receive the parameters in each row as function arguments.
|
|
4304
|
+
* Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
|
|
4305
|
+
*
|
|
4306
|
+
* @example
|
|
4307
|
+
* ```ts
|
|
4308
|
+
* describe.each([
|
|
4309
|
+
* [1, 1, 2],
|
|
4310
|
+
* [2, 3, 5]
|
|
4311
|
+
* ])('add(%i, %i) = %i', (a, b, expected) => {
|
|
4312
|
+
* test('adds correctly', () => {
|
|
4313
|
+
* expect(a + b).toBe(expected);
|
|
4314
|
+
* });
|
|
4315
|
+
* });
|
|
4316
|
+
* ```
|
|
4317
|
+
*
|
|
4318
|
+
* @see DescribeCallbackType
|
|
4319
|
+
* @since 1.0.0
|
|
4320
|
+
*/
|
|
4321
|
+
each<T extends Array<unknown> | [unknown]>(...cases: T[]): DescribeCallbackType<T>;
|
|
4322
|
+
/**
|
|
4323
|
+
* Creates parameterized test suites from individual test case values.
|
|
4324
|
+
*
|
|
4325
|
+
* @template T - Type representing the test case data
|
|
4326
|
+
*
|
|
4327
|
+
* @param args - Individual test case values
|
|
4328
|
+
*
|
|
4329
|
+
* @returns A callback function that accepts a test name pattern and implementation function
|
|
4330
|
+
*
|
|
4331
|
+
* @remarks
|
|
4332
|
+
* The name parameter can include formatters:
|
|
4333
|
+
* - Generate unique test titles by positionally injecting parameters with printf formatting:
|
|
4334
|
+
* - %p - pretty-format
|
|
4335
|
+
* - %s - String
|
|
4336
|
+
* - %d - Number
|
|
4337
|
+
* - %i - Integer
|
|
4338
|
+
* - %f - Floating point value
|
|
4339
|
+
* - %j - JSON
|
|
4340
|
+
* - %o - Object
|
|
4341
|
+
* - %# - Index of the test case
|
|
4342
|
+
* - %% - Single percent sign ('%'). This does not consume an argument
|
|
4343
|
+
* - Or generate unique test titles by injecting properties of test case object with $variable:
|
|
4344
|
+
* - To inject nested object values supply a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4345
|
+
* - Use $# to inject the index of the test case
|
|
4346
|
+
* - You cannot use $variable with printf formatting except for %%
|
|
4347
|
+
*
|
|
4348
|
+
* The fn parameter is the function that will receive the parameters in each row as function arguments.
|
|
4349
|
+
* Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
|
|
4350
|
+
*
|
|
4351
|
+
* @example
|
|
4352
|
+
* ```ts
|
|
4353
|
+
* describe.each(
|
|
4354
|
+
* { name: 'John', age: 30 },
|
|
4355
|
+
* { name: 'Jane', age: 25 }
|
|
4356
|
+
* )('Testing $name', (person) => {
|
|
4357
|
+
* test('age check', () => {
|
|
4358
|
+
* expect(person.age).toBeGreaterThan(18);
|
|
4359
|
+
* });
|
|
4360
|
+
* });
|
|
4361
|
+
* ```
|
|
4362
|
+
*
|
|
4363
|
+
* @see DescribeCallbackType
|
|
4364
|
+
* @since 1.0.0
|
|
4365
|
+
*/
|
|
4366
|
+
each<T>(...args: readonly T[]): DescribeCallbackType<T>;
|
|
4367
|
+
/**
|
|
4368
|
+
* Creates parameterized test suites from individual objects or primitive values.
|
|
4369
|
+
*
|
|
4370
|
+
* @template T - Array type representing the test case data
|
|
4371
|
+
*
|
|
4372
|
+
* @param args - Individual test case values in an array
|
|
4373
|
+
*
|
|
4374
|
+
* @returns A callback function that accepts a test name pattern and implementation function
|
|
4375
|
+
*
|
|
4376
|
+
* @remarks
|
|
4377
|
+
* The name parameter can include formatters:
|
|
4378
|
+
* - Generate unique test titles by positionally injecting parameters with printf formatting:
|
|
4379
|
+
* - %p - pretty-format
|
|
4380
|
+
* - %s - String
|
|
4381
|
+
* - %d - Number
|
|
4382
|
+
* - %i - Integer
|
|
4383
|
+
* - %f - Floating point value
|
|
4384
|
+
* - %j - JSON
|
|
4385
|
+
* - %o - Object
|
|
4386
|
+
* - %# - Index of the test case
|
|
4387
|
+
* - %% - Single percent sign ('%'). This does not consume an argument
|
|
4388
|
+
* - Or generate unique test titles by injecting properties of test case object with $variable:
|
|
4389
|
+
* - To inject nested object values supply a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4390
|
+
* - Use $# to inject the index of the test case
|
|
4391
|
+
* - You cannot use $variable with printf formatting except for %%
|
|
4392
|
+
*
|
|
4393
|
+
* The fn parameter is the function that will receive the parameters in each row as function arguments.
|
|
4394
|
+
* Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
|
|
4395
|
+
*
|
|
4396
|
+
* @example
|
|
4397
|
+
* ```ts
|
|
4398
|
+
* describe.each(
|
|
4399
|
+
* 1, 2, 3, 4
|
|
4400
|
+
* )('Testing with value %i', (value) => {
|
|
4401
|
+
* test('is positive', () => {
|
|
4402
|
+
* expect(value).toBeGreaterThan(0);
|
|
4403
|
+
* });
|
|
4404
|
+
* });
|
|
4405
|
+
* ```
|
|
4406
|
+
*
|
|
4407
|
+
* @see DescribeCallbackType
|
|
4408
|
+
*
|
|
4409
|
+
* @since 1.0.0
|
|
4410
|
+
*/
|
|
4411
|
+
each<T extends Array<unknown>>(...args: T): DescribeCallbackType<T[number]>;
|
|
4412
|
+
/**
|
|
4413
|
+
* Invokes a test block with the specified description and function.
|
|
4414
|
+
*
|
|
4415
|
+
* @param description - String for the title of the test block
|
|
4416
|
+
* @param block - Function that contains the test logic to be executed
|
|
4417
|
+
* @param args - Optional array of arguments to pass to the test function
|
|
4418
|
+
*
|
|
4419
|
+
* @remarks
|
|
4420
|
+
* The description parameter can include formatters:
|
|
4421
|
+
* - Generate unique test titles by positionally injecting parameters with printf formatting:
|
|
4422
|
+
* - %p - pretty-format
|
|
4423
|
+
* - %s - String
|
|
4424
|
+
* - %d - Number
|
|
4425
|
+
* - %i - Integer
|
|
4426
|
+
* - %f - Floating point value
|
|
4427
|
+
* - %j - JSON
|
|
4428
|
+
* - %o - Object
|
|
4429
|
+
* - %# - Index of the test case
|
|
4430
|
+
* - %% - Single percent sign ('%'). This does not consume an argument
|
|
4431
|
+
* - Or generate unique test titles by injecting properties of test case object with $variable:
|
|
4432
|
+
* - To inject nested object values supply a keyPath i.e. $variable.path.to.value (only works for "own" properties)
|
|
4433
|
+
* - Use $# to inject the index of the test case
|
|
4434
|
+
* - You cannot use $variable with printf formatting except for %%
|
|
4435
|
+
*
|
|
4436
|
+
* The block parameter is the function that will receive the parameters in each row as function arguments.
|
|
4437
|
+
* Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
|
|
4438
|
+
*
|
|
4439
|
+
* @returns void
|
|
4440
|
+
*/
|
|
4441
|
+
invoke(description: string, block: FunctionType, args?: Array<unknown>): void;
|
|
4442
|
+
}
|
|
4443
|
+
|
|
4444
|
+
/**
|
|
4445
|
+
* Import will remove at compile time
|
|
4446
|
+
*/
|
|
4447
|
+
/**
|
|
4448
|
+
* Imports
|
|
4449
|
+
*/
|
|
4450
|
+
/**
|
|
4451
|
+
* Retrieves the parent object of the specified function if it exists within the global context.
|
|
4452
|
+
*
|
|
4453
|
+
* @template FunctionLikeType - The type representing the input function.
|
|
4454
|
+
*
|
|
4455
|
+
* @param fn - The function whose parent object is to be retrieved.
|
|
4456
|
+
* @returns The parent object containing the function, or `undefined` if no such object is found.
|
|
4457
|
+
*
|
|
4458
|
+
* @remarks
|
|
4459
|
+
* This method searches for the parent object of a given function within the global context
|
|
4460
|
+
* by checking if the function name exists as a key of an object in the global scope.
|
|
4461
|
+
* If the function exists directly in the global scope, the global object itself is returned.
|
|
4462
|
+
*
|
|
4463
|
+
* @since 1.0.0
|
|
4464
|
+
*/
|
|
4465
|
+
declare function getParentObject(fn: FunctionLikeType): Record<string, unknown> | undefined;
|
|
4466
|
+
/**
|
|
4467
|
+
* Creates a mock function interface with the specified implementation and optional restore function.
|
|
4468
|
+
*
|
|
4469
|
+
* @template ReturnType - The return type of the mocked function.
|
|
4470
|
+
* @template Context - The context type that the mocked function binds to.
|
|
4471
|
+
* @template Args - The argument type of the mocked function. Defaults to an array of unknown values.
|
|
4472
|
+
*
|
|
4473
|
+
* @param implementation - An optional implementation of the mocked function.
|
|
4474
|
+
* @param restore - An optional restore function used to reset the mock.
|
|
4475
|
+
* @returns A mocked function interface with the specified behaviors.
|
|
4476
|
+
*
|
|
4477
|
+
* @remarks
|
|
4478
|
+
* This function creates a mock function handler, typically used in testing scenarios.
|
|
4479
|
+
* You can provide an implementation for the mock behavior or specify a restore
|
|
4480
|
+
* handler for resetting the mock's state.
|
|
4481
|
+
*
|
|
4482
|
+
* @example
|
|
4483
|
+
* ```ts
|
|
4484
|
+
* // Creating a mock with a custom implementation
|
|
4485
|
+
* const mock = xJet.fn((x: number) => x * 2);
|
|
4486
|
+
* console.log(mock(5)); // 10
|
|
4487
|
+
*
|
|
4488
|
+
* // Creating a mock with a restore function
|
|
4489
|
+
* const mockWithRestore = xJet.fnImplementation(undefined, () => { console.log('Restored!'); });
|
|
4490
|
+
* mockWithRestore.restore(); // "Restored!"
|
|
4491
|
+
* ```
|
|
4492
|
+
*
|
|
4493
|
+
* @see MockState
|
|
4494
|
+
*
|
|
4495
|
+
* @since 1.0.0
|
|
4496
|
+
*/
|
|
4497
|
+
declare function fnImplementation<ReturnType, Args extends Array<unknown>, Context>(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: FunctionLikeType<void>): FnMockInterface<ReturnType, Args, Context>;
|
|
4498
|
+
/**
|
|
4499
|
+
* Creates a mock instance for the given method or constructor.
|
|
4500
|
+
*
|
|
4501
|
+
* @template Method - The type of the method being mocked.
|
|
4502
|
+
* @template Context - The `this` context type of the method.
|
|
4503
|
+
* @template Args - The types of the arguments accepted by the method.
|
|
4504
|
+
*
|
|
4505
|
+
* @param method - The method or constructor to mock. This can either be a function-like type or a
|
|
4506
|
+
* constructor-like type.
|
|
4507
|
+
* @returns A `MockState` instance associated with the provided method, allowing for capturing
|
|
4508
|
+
* interactions and controlling behavior during testing.
|
|
4509
|
+
*
|
|
4510
|
+
* @remarks
|
|
4511
|
+
* This method identifies whether the provided method is a function or constructor and creates
|
|
4512
|
+
* a suitable mock state. If the method is already mocked, the existing mock state is returned.
|
|
4513
|
+
* Throws an error if the method does not belong to an object or if it has an invalid type.
|
|
4514
|
+
*
|
|
4515
|
+
* @example
|
|
4516
|
+
* ```ts
|
|
4517
|
+
* // Mocking a regular method
|
|
4518
|
+
* function greet(name: string) {
|
|
4519
|
+
* return `Hello, ${ name }`;
|
|
4520
|
+
* }
|
|
4521
|
+
*
|
|
4522
|
+
* const greetMock = xJet.mock(greet);
|
|
4523
|
+
* greetMock.mockImplementation(() => 'Hi!');
|
|
4524
|
+
* console.log(greet('World')); // "Hi!"
|
|
4525
|
+
*
|
|
4526
|
+
* // Mocking a constructor
|
|
4527
|
+
* class Person {
|
|
4528
|
+
* constructor(public name: string) {}
|
|
4529
|
+
* }
|
|
4530
|
+
* const personMock = xJet.mock(Person);
|
|
4531
|
+
* personMock.mockImplementation((name: string) => ({ name: `${name} (mocked)` }));
|
|
4532
|
+
* const person = new Person('Alice');
|
|
4533
|
+
* console.log(person.name); // "Alice (mocked)"
|
|
4534
|
+
*
|
|
4535
|
+
* // Restoring the original method
|
|
4536
|
+
* greetMock.mockRestore();
|
|
4537
|
+
* console.log(greet('World')); // "Hello, World"
|
|
4538
|
+
* ```
|
|
4539
|
+
*
|
|
4540
|
+
* @since 1.0.0
|
|
4541
|
+
*/
|
|
4542
|
+
declare function mockImplementation<Method, Args extends Array<unknown>, Context>(method: FunctionLikeType<Method, Args, Context> | ConstructorLikeType<Method, Args>): MockState<Method, Args, Context>;
|
|
4543
|
+
|
|
4544
|
+
/**
|
|
4545
|
+
* Import will remove at compile time
|
|
4546
|
+
*/
|
|
4547
|
+
/**
|
|
4548
|
+
* Represents a mockable function interface with a customizable return type, context,
|
|
4549
|
+
* and argument list. This interface extends `MockState` to facilitate tracking
|
|
4550
|
+
* and testing of function behaviors and states.
|
|
4551
|
+
*
|
|
4552
|
+
* @template ReturnType - Specifies the return type of the function. Defaults to `unknown`.
|
|
4553
|
+
* @template Context - Defines the function's "this" context type. Defaults to `unknown`.
|
|
4554
|
+
* @template Args - Sets the argument type(s) for the function, represented as an array. Defaults to `unknown[]`.
|
|
4555
|
+
*
|
|
4556
|
+
* @remarks
|
|
4557
|
+
* This interface is useful for creating test doubles or mock implementations that simulate
|
|
4558
|
+
* complex behaviors (allows for both `function-like` behavior and `constructor-like` behavior)
|
|
4559
|
+
* while tracking interactions and state information.
|
|
4560
|
+
*
|
|
4561
|
+
* @see MockState
|
|
4562
|
+
*
|
|
4563
|
+
* @since 1.0.0
|
|
4564
|
+
*/
|
|
4565
|
+
interface FnMockInterface<ReturnType = unknown, Args extends Array<unknown> = any, Context = any> extends MockState<ReturnType, Args, Context> {
|
|
4566
|
+
new (...args: Args): ReturnType;
|
|
4567
|
+
(this: Context, ...args: Args): ReturnType;
|
|
4568
|
+
}
|
|
4569
|
+
|
|
4570
|
+
/**
|
|
4571
|
+
* Import will remove at compile time
|
|
4572
|
+
*/
|
|
4573
|
+
/**
|
|
4574
|
+
* Creates and registers a hook with the current test suite
|
|
4575
|
+
*
|
|
4576
|
+
* @param hookType - Type of hook to register
|
|
4577
|
+
* @param callback - Function to execute for this hook
|
|
4578
|
+
* @param location - The precise source code location where the error occurred
|
|
4579
|
+
* @param timeout - Maximum execution time in milliseconds
|
|
4580
|
+
* @returns void
|
|
4581
|
+
*
|
|
4582
|
+
* @example
|
|
4583
|
+
* ```ts
|
|
4584
|
+
* createHook(HookType.BEFORE_EACH, () => {
|
|
4585
|
+
* // Setup code to run before each test
|
|
4586
|
+
* resetTestDatabase();
|
|
4587
|
+
* }, 10000);
|
|
4588
|
+
* ```
|
|
4589
|
+
*
|
|
4590
|
+
* @see HookType
|
|
4591
|
+
* @see HookModel
|
|
4592
|
+
* @see SuiteState
|
|
4593
|
+
*
|
|
4594
|
+
* @since 1.0.0
|
|
4595
|
+
*/
|
|
4596
|
+
declare function createHook(hookType: HookType, callback: FunctionType, location: string, timeout?: number): void;
|
|
4597
|
+
/**
|
|
4598
|
+
* Registers an after-all hook to be executed once after all tests in a suite have completed
|
|
4599
|
+
*
|
|
4600
|
+
* @param callback - Function to execute after all tests in the suite
|
|
4601
|
+
* @param timeout - Maximum execution time in milliseconds
|
|
4602
|
+
* @returns void
|
|
4603
|
+
*
|
|
4604
|
+
* @throws Error - If called outside a test suite context
|
|
4605
|
+
*
|
|
4606
|
+
* @remarks
|
|
4607
|
+
* This function is a wrapper around the createHook function, specifically for creating AFTER_ALL hooks.
|
|
4608
|
+
* After-all hooks are useful for cleanup operations that should occur once after all tests complete.
|
|
4609
|
+
*
|
|
4610
|
+
* @example
|
|
4611
|
+
* ```ts
|
|
4612
|
+
* afterAllDirective(() => {
|
|
4613
|
+
* // Teardown code to run after all tests
|
|
4614
|
+
* disconnectFromDatabase();
|
|
4615
|
+
* }, 10000);
|
|
4616
|
+
* ```
|
|
4617
|
+
*
|
|
4618
|
+
* @see createHook
|
|
4619
|
+
* @see HookType.AFTER_ALL
|
|
4620
|
+
*
|
|
4621
|
+
* @since 1.0.0
|
|
4622
|
+
*/
|
|
4623
|
+
declare function afterAllDirective(callback: FunctionType, timeout?: number): void;
|
|
4624
|
+
/**
|
|
4625
|
+
* Registers a before-all hook to be executed once before any tests in a suite run
|
|
4626
|
+
*
|
|
4627
|
+
* @param callback - Function to execute before any tests in the suite
|
|
4628
|
+
* @param timeout - Maximum execution time in milliseconds
|
|
4629
|
+
* @returns void
|
|
4630
|
+
*
|
|
4631
|
+
* @throws Error - If called outside a test suite context
|
|
4632
|
+
*
|
|
4633
|
+
* @remarks
|
|
4634
|
+
* This function is a wrapper around the createHook function, specifically for creating BEFORE_ALL hooks.
|
|
4635
|
+
* Before-all hooks are useful for setup operations that should occur once before any tests run.
|
|
4636
|
+
*
|
|
4637
|
+
* @default timeout 5000
|
|
4638
|
+
*
|
|
4639
|
+
* @example
|
|
4640
|
+
* ```ts
|
|
4641
|
+
* beforeAllDirective(() => {
|
|
4642
|
+
* // Setup code to run before any tests
|
|
4643
|
+
* initializeTestDatabase();
|
|
4644
|
+
* }, 10000);
|
|
4645
|
+
* ```
|
|
4646
|
+
*
|
|
4647
|
+
* @see createHook
|
|
4648
|
+
* @see HookType.BEFORE_ALL
|
|
4649
|
+
*
|
|
4650
|
+
* @since 1.0.0
|
|
4651
|
+
*/
|
|
4652
|
+
declare function beforeAllDirective(callback: FunctionType, timeout?: number): void;
|
|
4653
|
+
/**
|
|
4654
|
+
* Registers an after-each hook to be executed after each test in a suite completes
|
|
4655
|
+
*
|
|
4656
|
+
* @param callback - Function to execute after each test
|
|
4657
|
+
* @param timeout - Maximum execution time in milliseconds
|
|
4658
|
+
* @returns void
|
|
4659
|
+
*
|
|
4660
|
+
* @throws Error - If called outside a test suite context
|
|
4661
|
+
*
|
|
4662
|
+
* @remarks
|
|
4663
|
+
* This function is a wrapper around the createHook function, specifically for creating AFTER_EACH hooks.
|
|
4664
|
+
* After-each hooks run after each test case and are useful for cleanup operations that should occur
|
|
4665
|
+
* after every individual test.
|
|
4666
|
+
*
|
|
4667
|
+
* @default timeout 5000
|
|
4668
|
+
*
|
|
4669
|
+
* @example
|
|
4670
|
+
* ```ts
|
|
4671
|
+
* afterEachDirective(() => {
|
|
4672
|
+
* // Cleanup code to run after each test
|
|
4673
|
+
* resetTestState();
|
|
4674
|
+
* }, 8000);
|
|
4675
|
+
* ```
|
|
4676
|
+
*
|
|
4677
|
+
* @see createHook
|
|
4678
|
+
* @see HookType.AFTER_EACH
|
|
4679
|
+
*
|
|
4680
|
+
* @since 1.0.0
|
|
4681
|
+
*/
|
|
4682
|
+
declare function afterEachDirective(callback: FunctionType, timeout?: number): void;
|
|
4683
|
+
/**
|
|
4684
|
+
* Registers a before-each hook to be executed before each test in a suite runs
|
|
4685
|
+
*
|
|
4686
|
+
* @param callback - Function to execute before each test
|
|
4687
|
+
* @param timeout - Maximum execution time in milliseconds
|
|
4688
|
+
*
|
|
4689
|
+
* @returns void
|
|
4690
|
+
*
|
|
4691
|
+
* @throws Error - If called outside a test suite context
|
|
4692
|
+
*
|
|
4693
|
+
* @remarks
|
|
4694
|
+
* This function is a wrapper around the createHook function, specifically for creating BEFORE_EACH hooks.
|
|
4695
|
+
* Before-each hooks run before each test case and are useful for setup operations that should occur
|
|
4696
|
+
* before every individual test.
|
|
4697
|
+
*
|
|
4698
|
+
* @default timeout 5000
|
|
4699
|
+
*
|
|
4700
|
+
* @example
|
|
4701
|
+
* ```ts
|
|
4702
|
+
* beforeEachDirective(() => {
|
|
4703
|
+
* // Setup code to run before each test
|
|
4704
|
+
* prepareTestEnvironment();
|
|
4705
|
+
* }, 8000);
|
|
4706
|
+
* ```
|
|
4707
|
+
*
|
|
4708
|
+
* @see createHook
|
|
4709
|
+
* @see HookType.BEFORE_EACH
|
|
4710
|
+
*
|
|
4711
|
+
* @since 1.0.0
|
|
4712
|
+
*/
|
|
4713
|
+
declare function beforeEachDirective(callback: FunctionType, timeout?: number): void;
|