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