@omnitron-dev/testing 0.4.1 → 0.4.3
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/dist/async/deferred.d.ts +15 -0
- package/dist/async/deferred.js +35 -0
- package/dist/async/deferred.js.map +1 -0
- package/dist/async/events.d.ts +32 -0
- package/dist/async/events.js +176 -0
- package/dist/async/events.js.map +1 -0
- package/dist/async/index.d.ts +5 -0
- package/dist/async/index.js +6 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/mock-timer.d.ts +19 -0
- package/dist/async/mock-timer.js +87 -0
- package/dist/async/mock-timer.js.map +1 -0
- package/dist/async/retry.d.ts +8 -0
- package/dist/async/retry.js +36 -0
- package/dist/async/retry.js.map +1 -0
- package/dist/async/wait.d.ts +10 -0
- package/dist/async/wait.js +35 -0
- package/dist/async/wait.js.map +1 -0
- package/dist/docker/database-manager.d.ts +22 -0
- package/dist/docker/database-manager.js +97 -0
- package/dist/docker/database-manager.js.map +1 -0
- package/dist/docker/docker-test-manager.d.ts +44 -0
- package/dist/docker/docker-test-manager.js +805 -0
- package/dist/docker/docker-test-manager.js.map +1 -0
- package/dist/docker/index.d.ts +4 -0
- package/dist/docker/index.js +4 -0
- package/dist/docker/index.js.map +1 -0
- package/dist/docker/redis-manager.d.ts +12 -0
- package/dist/docker/redis-manager.js +453 -0
- package/dist/docker/redis-manager.js.map +1 -0
- package/dist/docker/types.d.ts +103 -0
- package/dist/docker/types.js +2 -0
- package/dist/docker/types.js.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -0
- package/dist/helpers/assertions.d.ts +6 -0
- package/dist/helpers/assertions.js +37 -0
- package/dist/helpers/assertions.js.map +1 -0
- package/dist/helpers/console.d.ts +1 -0
- package/dist/helpers/console.js +23 -0
- package/dist/helpers/console.js.map +1 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/helpers/temp-dir.d.ts +2 -0
- package/dist/helpers/temp-dir.js +13 -0
- package/dist/helpers/temp-dir.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/performance/index.d.ts +2 -0
- package/dist/performance/index.js +3 -0
- package/dist/performance/index.js.map +1 -0
- package/dist/performance/memory-detector.d.ts +9 -0
- package/dist/performance/memory-detector.js +61 -0
- package/dist/performance/memory-detector.js.map +1 -0
- package/dist/performance/perf-timer.d.ts +9 -0
- package/dist/performance/perf-timer.js +49 -0
- package/dist/performance/perf-timer.js.map +1 -0
- package/dist/titan/container-utils.d.ts +20 -0
- package/dist/titan/container-utils.js +78 -0
- package/dist/titan/container-utils.js.map +1 -0
- package/dist/titan/database/database-testing.module.d.ts +81 -0
- package/dist/titan/database/database-testing.module.js +419 -0
- package/dist/titan/database/database-testing.module.js.map +1 -0
- package/dist/titan/database/index.d.ts +3 -0
- package/dist/titan/database/index.js +4 -0
- package/dist/titan/database/index.js.map +1 -0
- package/dist/titan/database/test-utilities.d.ts +31 -0
- package/dist/titan/database/test-utilities.js +145 -0
- package/dist/titan/database/test-utilities.js.map +1 -0
- package/dist/titan/index.d.ts +14 -0
- package/dist/titan/index.js +10 -0
- package/dist/titan/index.js.map +1 -0
- package/dist/titan/nexus/index.d.ts +3 -0
- package/dist/titan/nexus/index.js +4 -0
- package/dist/titan/nexus/index.js.map +1 -0
- package/dist/titan/nexus/mock-provider.d.ts +41 -0
- package/dist/titan/nexus/mock-provider.js +127 -0
- package/dist/titan/nexus/mock-provider.js.map +1 -0
- package/dist/titan/nexus/test-container.d.ts +120 -0
- package/dist/titan/nexus/test-container.js +559 -0
- package/dist/titan/nexus/test-container.js.map +1 -0
- package/dist/titan/nexus/test-utilities.d.ts +135 -0
- package/dist/titan/nexus/test-utilities.js +484 -0
- package/dist/titan/nexus/test-utilities.js.map +1 -0
- package/dist/titan/pm/index.d.ts +4 -0
- package/dist/titan/pm/index.js +4 -0
- package/dist/titan/pm/index.js.map +1 -0
- package/dist/titan/pm/mock-process-spawner.d.ts +60 -0
- package/dist/titan/pm/mock-process-spawner.js +321 -0
- package/dist/titan/pm/mock-process-spawner.js.map +1 -0
- package/dist/titan/pm/mock-spawner.d.ts +27 -0
- package/dist/titan/pm/mock-spawner.js +99 -0
- package/dist/titan/pm/mock-spawner.js.map +1 -0
- package/dist/titan/pm/test-process-manager.d.ts +49 -0
- package/dist/titan/pm/test-process-manager.js +188 -0
- package/dist/titan/pm/test-process-manager.js.map +1 -0
- package/dist/titan/redis-test-utils.d.ts +33 -0
- package/dist/titan/redis-test-utils.js +414 -0
- package/dist/titan/redis-test-utils.js.map +1 -0
- package/dist/titan/test-application.d.ts +21 -0
- package/dist/titan/test-application.js +90 -0
- package/dist/titan/test-application.js.map +1 -0
- package/dist/titan/test-fixtures.d.ts +97 -0
- package/dist/titan/test-fixtures.js +223 -0
- package/dist/titan/test-fixtures.js.map +1 -0
- package/dist/titan/test-mocks.d.ts +29 -0
- package/dist/titan/test-mocks.js +262 -0
- package/dist/titan/test-mocks.js.map +1 -0
- package/dist/titan/test-module.d.ts +46 -0
- package/dist/titan/test-module.js +120 -0
- package/dist/titan/test-module.js.map +1 -0
- package/package.json +41 -4
- package/src/async/README.md +336 -0
- package/src/async/deferred.ts +98 -0
- package/src/async/events.ts +386 -0
- package/src/async/index.ts +48 -0
- package/src/async/mock-timer.ts +220 -0
- package/src/async/retry.ts +118 -0
- package/src/async/wait.ts +151 -0
- package/src/docker/database-manager.ts +242 -0
- package/src/docker/docker-test-manager.ts +1186 -0
- package/src/docker/index.ts +48 -0
- package/src/docker/redis-manager.ts +738 -0
- package/src/docker/types.ts +216 -0
- package/src/errors.ts +31 -0
- package/src/helpers/assertions.ts +171 -0
- package/src/helpers/console.ts +74 -0
- package/src/helpers/index.ts +15 -0
- package/src/helpers/temp-dir.ts +50 -0
- package/src/index.ts +2 -0
- package/src/performance/index.ts +11 -0
- package/src/performance/memory-detector.ts +166 -0
- package/src/performance/perf-timer.ts +146 -0
- package/src/titan/container-utils.ts +128 -0
- package/src/titan/database/database-testing.module.ts +831 -0
- package/src/titan/database/index.ts +30 -0
- package/src/titan/database/test-utilities.ts +337 -0
- package/src/titan/index.ts +147 -0
- package/src/titan/nexus/index.ts +3 -0
- package/src/titan/nexus/mock-provider.ts +189 -0
- package/src/titan/nexus/test-container.ts +887 -0
- package/src/titan/nexus/test-utilities.ts +796 -0
- package/src/titan/pm/index.ts +22 -0
- package/src/titan/pm/mock-process-spawner.ts +481 -0
- package/src/titan/pm/mock-spawner.ts +156 -0
- package/src/titan/pm/test-process-manager.ts +345 -0
- package/src/titan/redis-test-utils.ts +566 -0
- package/src/titan/test-application.ts +154 -0
- package/src/titan/test-fixtures.ts +304 -0
- package/src/titan/test-mocks.ts +336 -0
- package/src/titan/test-module.ts +232 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event testing utilities for async operations
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { TimeoutError } from '../errors.js';
|
|
6
|
+
import { defer, type Deferred } from './deferred.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Event listener tracker for cleanup
|
|
10
|
+
*
|
|
11
|
+
* Tracks event listeners added during tests and provides
|
|
12
|
+
* automatic cleanup to prevent memory leaks.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const tracker = new EventListenerTracker();
|
|
17
|
+
* tracker.on(emitter, 'data', handler);
|
|
18
|
+
* // ... test code ...
|
|
19
|
+
* tracker.cleanup(); // Remove all tracked listeners
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export class EventListenerTracker {
|
|
23
|
+
private listeners: Array<{
|
|
24
|
+
target: any;
|
|
25
|
+
event: string;
|
|
26
|
+
handler: (...args: any[]) => any;
|
|
27
|
+
type: 'on' | 'once';
|
|
28
|
+
}> = [];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Track an event listener for cleanup
|
|
32
|
+
*
|
|
33
|
+
* @param target - Event emitter
|
|
34
|
+
* @param event - Event name
|
|
35
|
+
* @param handler - Event handler function
|
|
36
|
+
* @param type - Listener type ('on' or 'once')
|
|
37
|
+
*/
|
|
38
|
+
track(target: any, event: string, handler: (...args: any[]) => any, type: 'on' | 'once' = 'on'): void {
|
|
39
|
+
this.listeners.push({ target, event, handler, type });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Add event listener and track it for cleanup
|
|
44
|
+
*
|
|
45
|
+
* @param target - Event emitter
|
|
46
|
+
* @param event - Event name
|
|
47
|
+
* @param handler - Event handler function
|
|
48
|
+
*/
|
|
49
|
+
on(target: any, event: string, handler: (...args: any[]) => any): void {
|
|
50
|
+
target.on(event, handler);
|
|
51
|
+
this.track(target, event, handler, 'on');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Add one-time event listener and track it for cleanup
|
|
56
|
+
*
|
|
57
|
+
* @param target - Event emitter
|
|
58
|
+
* @param event - Event name
|
|
59
|
+
* @param handler - Event handler function
|
|
60
|
+
*/
|
|
61
|
+
once(target: any, event: string, handler: (...args: any[]) => any): void {
|
|
62
|
+
target.once(event, handler);
|
|
63
|
+
this.track(target, event, handler, 'once');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Remove all tracked listeners
|
|
68
|
+
*
|
|
69
|
+
* Safely removes all event listeners, catching and ignoring
|
|
70
|
+
* any errors that occur during cleanup.
|
|
71
|
+
*/
|
|
72
|
+
cleanup(): void {
|
|
73
|
+
for (const { target, event, handler } of this.listeners) {
|
|
74
|
+
try {
|
|
75
|
+
if (target.removeListener) {
|
|
76
|
+
target.removeListener(event, handler);
|
|
77
|
+
} else if (target.off) {
|
|
78
|
+
target.off(event, handler);
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
// Ignore errors during cleanup
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.listeners = [];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the number of tracked listeners
|
|
90
|
+
*/
|
|
91
|
+
get count(): number {
|
|
92
|
+
return this.listeners.length;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Event collector for testing
|
|
98
|
+
*
|
|
99
|
+
* Collects events from an event emitter for later inspection
|
|
100
|
+
* and assertions in tests.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const collector = new EventCollector(emitter);
|
|
105
|
+
* collector.collect('data');
|
|
106
|
+
* // ... trigger events ...
|
|
107
|
+
* const events = collector.getEvents('data');
|
|
108
|
+
* expect(events).toHaveLength(3);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export class EventCollector<T = any> {
|
|
112
|
+
private events: Array<{ event: string; data: T; timestamp: number }> = [];
|
|
113
|
+
private listeners = new Map<string, (...args: any[]) => void>();
|
|
114
|
+
|
|
115
|
+
constructor(private emitter: any) {}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Start collecting events
|
|
119
|
+
*
|
|
120
|
+
* @param eventName - Name of event to collect
|
|
121
|
+
* @returns This collector for chaining
|
|
122
|
+
*/
|
|
123
|
+
collect(eventName: string): this {
|
|
124
|
+
const listener = (...args: any[]) => {
|
|
125
|
+
this.events.push({
|
|
126
|
+
event: eventName,
|
|
127
|
+
data: args.length === 1 ? args[0] : args,
|
|
128
|
+
timestamp: Date.now(),
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
this.listeners.set(eventName, listener);
|
|
133
|
+
this.emitter.on(eventName, listener);
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Stop collecting events
|
|
139
|
+
*
|
|
140
|
+
* @param eventName - Name of event to stop collecting, or undefined to stop all
|
|
141
|
+
*/
|
|
142
|
+
stop(eventName?: string): void {
|
|
143
|
+
if (eventName) {
|
|
144
|
+
const listener = this.listeners.get(eventName);
|
|
145
|
+
if (listener) {
|
|
146
|
+
this.emitter.off(eventName, listener);
|
|
147
|
+
this.listeners.delete(eventName);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
for (const [event, listener] of this.listeners) {
|
|
151
|
+
this.emitter.off(event, listener);
|
|
152
|
+
}
|
|
153
|
+
this.listeners.clear();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get collected events
|
|
159
|
+
*
|
|
160
|
+
* @param eventName - Filter by event name, or undefined for all events
|
|
161
|
+
* @returns Array of collected events with data and timestamps
|
|
162
|
+
*/
|
|
163
|
+
getEvents(eventName?: string): Array<{ event: string; data: T; timestamp: number }> {
|
|
164
|
+
if (eventName) {
|
|
165
|
+
return this.events.filter((e) => e.event === eventName);
|
|
166
|
+
}
|
|
167
|
+
return [...this.events];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Clear collected events
|
|
172
|
+
*/
|
|
173
|
+
clear(): void {
|
|
174
|
+
this.events = [];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Wait for a specific event
|
|
179
|
+
*
|
|
180
|
+
* @param eventName - Name of event to wait for
|
|
181
|
+
* @param timeout - Timeout in milliseconds (default: 5000)
|
|
182
|
+
* @returns Promise resolving to event data
|
|
183
|
+
* @throws TimeoutError if timeout is reached
|
|
184
|
+
*/
|
|
185
|
+
async waitForEvent(eventName: string, timeout = 5000): Promise<T> {
|
|
186
|
+
const deferred = defer<T>();
|
|
187
|
+
|
|
188
|
+
const listener = (data: T) => {
|
|
189
|
+
deferred.resolve(data);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
this.emitter.once(eventName, listener);
|
|
193
|
+
|
|
194
|
+
const timer = setTimeout(() => {
|
|
195
|
+
this.emitter.off(eventName, listener);
|
|
196
|
+
deferred.reject(new TimeoutError('event: ' + eventName, timeout));
|
|
197
|
+
}, timeout);
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
const result = await deferred.promise;
|
|
201
|
+
clearTimeout(timer);
|
|
202
|
+
return result;
|
|
203
|
+
} catch (error) {
|
|
204
|
+
clearTimeout(timer);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Get event count
|
|
211
|
+
*
|
|
212
|
+
* @param eventName - Filter by event name, or undefined for total count
|
|
213
|
+
* @returns Number of collected events
|
|
214
|
+
*/
|
|
215
|
+
getEventCount(eventName?: string): number {
|
|
216
|
+
if (eventName) {
|
|
217
|
+
return this.events.filter((e) => e.event === eventName).length;
|
|
218
|
+
}
|
|
219
|
+
return this.events.length;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Assert event was emitted
|
|
224
|
+
*
|
|
225
|
+
* @param eventName - Name of event to check
|
|
226
|
+
* @param expectedCount - Expected number of emissions (optional)
|
|
227
|
+
* @throws Error if assertion fails
|
|
228
|
+
*/
|
|
229
|
+
assertEmitted(eventName: string, expectedCount?: number): void {
|
|
230
|
+
const count = this.getEventCount(eventName);
|
|
231
|
+
if (expectedCount !== undefined) {
|
|
232
|
+
if (count !== expectedCount) {
|
|
233
|
+
throw new Error(`Expected ${expectedCount} ${eventName} events, got ${count}`);
|
|
234
|
+
}
|
|
235
|
+
} else if (count === 0) {
|
|
236
|
+
throw new Error(`Expected ${eventName} event to be emitted`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Assert event was not emitted
|
|
242
|
+
*
|
|
243
|
+
* @param eventName - Name of event to check
|
|
244
|
+
* @throws Error if assertion fails
|
|
245
|
+
*/
|
|
246
|
+
assertNotEmitted(eventName: string): void {
|
|
247
|
+
const count = this.getEventCount(eventName);
|
|
248
|
+
if (count > 0) {
|
|
249
|
+
throw new Error(`Expected ${eventName} event not to be emitted, but got ${count}`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Wait for multiple events
|
|
256
|
+
*
|
|
257
|
+
* Waits for all specified events to be emitted on the target,
|
|
258
|
+
* with a timeout for each individual event.
|
|
259
|
+
*
|
|
260
|
+
* @param target - Event emitter
|
|
261
|
+
* @param events - Array of event names to wait for
|
|
262
|
+
* @param timeout - Timeout per event in milliseconds (default: 5000)
|
|
263
|
+
* @returns Promise resolving to array of event data
|
|
264
|
+
* @throws TimeoutError if any event times out
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const [userData, profileData] = await waitForEvents(
|
|
269
|
+
* emitter,
|
|
270
|
+
* ['user', 'profile'],
|
|
271
|
+
* 3000
|
|
272
|
+
* );
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
export async function waitForEvents(target: any, events: string[], timeout = 5000): Promise<any[]> {
|
|
276
|
+
const promises: Promise<any>[] = [];
|
|
277
|
+
|
|
278
|
+
for (const event of events) {
|
|
279
|
+
const promise = new Promise((resolve, reject) => {
|
|
280
|
+
const timeoutId = setTimeout(() => {
|
|
281
|
+
reject(new TimeoutError('event: ' + event, timeout));
|
|
282
|
+
}, timeout);
|
|
283
|
+
|
|
284
|
+
const handler = (data: any) => {
|
|
285
|
+
clearTimeout(timeoutId);
|
|
286
|
+
resolve(data);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
target.once(event, handler);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
promises.push(promise);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return Promise.all(promises);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Collect events until a condition is met
|
|
300
|
+
*
|
|
301
|
+
* Collects events from the target until the condition function
|
|
302
|
+
* returns true, with a timeout for the entire operation.
|
|
303
|
+
*
|
|
304
|
+
* @param target - Event emitter
|
|
305
|
+
* @param event - Event name to collect
|
|
306
|
+
* @param condition - Function that returns true when enough events are collected
|
|
307
|
+
* @param timeout - Timeout in milliseconds (default: 5000)
|
|
308
|
+
* @returns Promise resolving to array of collected events
|
|
309
|
+
* @throws TimeoutError if timeout is reached
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* const events = await collectEvents(
|
|
314
|
+
* emitter,
|
|
315
|
+
* 'data',
|
|
316
|
+
* (events) => events.length >= 5,
|
|
317
|
+
* 3000
|
|
318
|
+
* );
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
export async function collectEvents<T>(
|
|
322
|
+
target: any,
|
|
323
|
+
event: string,
|
|
324
|
+
condition: (events: T[]) => boolean,
|
|
325
|
+
timeout = 5000
|
|
326
|
+
): Promise<T[]> {
|
|
327
|
+
const events: T[] = [];
|
|
328
|
+
const deferred = defer<T[]>();
|
|
329
|
+
|
|
330
|
+
const handler = (data: T) => {
|
|
331
|
+
events.push(data);
|
|
332
|
+
if (condition(events)) {
|
|
333
|
+
target.off(event, handler);
|
|
334
|
+
deferred.resolve(events);
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
target.on(event, handler);
|
|
339
|
+
|
|
340
|
+
const timeoutId = setTimeout(() => {
|
|
341
|
+
target.off(event, handler);
|
|
342
|
+
deferred.reject(new TimeoutError('collectEvents', timeout));
|
|
343
|
+
}, timeout);
|
|
344
|
+
|
|
345
|
+
try {
|
|
346
|
+
return await deferred.promise;
|
|
347
|
+
} finally {
|
|
348
|
+
clearTimeout(timeoutId);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Create an event spy
|
|
354
|
+
*
|
|
355
|
+
* Creates a spy that records all events emitted on the target,
|
|
356
|
+
* returning an object with the collected events and a cleanup function.
|
|
357
|
+
*
|
|
358
|
+
* @param target - Event emitter
|
|
359
|
+
* @param event - Event name to spy on
|
|
360
|
+
* @returns Object with events array and clear function
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* const spy = createEventSpy(emitter, 'data');
|
|
365
|
+
* // ... trigger events ...
|
|
366
|
+
* expect(spy.events).toHaveLength(3);
|
|
367
|
+
* spy.clear();
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
export function createEventSpy(target: any, event: string): { events: any[]; clear: () => void } {
|
|
371
|
+
const events: any[] = [];
|
|
372
|
+
|
|
373
|
+
const handler = (data: any) => {
|
|
374
|
+
events.push(data);
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
target.on(event, handler);
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
events,
|
|
381
|
+
clear: () => {
|
|
382
|
+
target.off(event, handler);
|
|
383
|
+
events.length = 0;
|
|
384
|
+
},
|
|
385
|
+
};
|
|
386
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Async testing utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides comprehensive utilities for testing asynchronous code,
|
|
5
|
+
* including promises, events, timers, and retry logic.
|
|
6
|
+
*
|
|
7
|
+
* @module async
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Deferred promises
|
|
11
|
+
export {
|
|
12
|
+
DeferredPromise,
|
|
13
|
+
createDeferred,
|
|
14
|
+
defer,
|
|
15
|
+
type Deferred,
|
|
16
|
+
} from './deferred.js';
|
|
17
|
+
|
|
18
|
+
// Retry and timeout
|
|
19
|
+
export {
|
|
20
|
+
retry,
|
|
21
|
+
withTimeout,
|
|
22
|
+
type RetryOptions,
|
|
23
|
+
} from './retry.js';
|
|
24
|
+
|
|
25
|
+
// Wait utilities
|
|
26
|
+
export {
|
|
27
|
+
waitFor,
|
|
28
|
+
waitForCondition,
|
|
29
|
+
delay,
|
|
30
|
+
nextTick,
|
|
31
|
+
flushPromises,
|
|
32
|
+
type WaitOptions,
|
|
33
|
+
} from './wait.js';
|
|
34
|
+
|
|
35
|
+
// Event utilities
|
|
36
|
+
export {
|
|
37
|
+
EventListenerTracker,
|
|
38
|
+
EventCollector,
|
|
39
|
+
waitForEvents,
|
|
40
|
+
collectEvents,
|
|
41
|
+
createEventSpy,
|
|
42
|
+
} from './events.js';
|
|
43
|
+
|
|
44
|
+
// Mock timers
|
|
45
|
+
export {
|
|
46
|
+
MockTimerController,
|
|
47
|
+
createMockTimer,
|
|
48
|
+
} from './mock-timer.js';
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock timer utilities for testing time-dependent code
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Mock timer controller for testing time-dependent code
|
|
7
|
+
*
|
|
8
|
+
* Provides manual control over setTimeout, setInterval, and time advancement,
|
|
9
|
+
* allowing deterministic testing of code that relies on timers.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const timer = new MockTimerController();
|
|
14
|
+
* timer.install();
|
|
15
|
+
*
|
|
16
|
+
* setTimeout(() => console.log('done'), 1000);
|
|
17
|
+
* timer.tick(1000); // Advances time and triggers callback
|
|
18
|
+
*
|
|
19
|
+
* timer.restore();
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export class MockTimerController {
|
|
23
|
+
private timers = new Map<NodeJS.Timeout, { callback: (...args: any[]) => any; delay: number; time: number }>();
|
|
24
|
+
private currentTime = 0;
|
|
25
|
+
private nextId = 1;
|
|
26
|
+
private originalTimers?: {
|
|
27
|
+
setTimeout: typeof setTimeout;
|
|
28
|
+
clearTimeout: typeof clearTimeout;
|
|
29
|
+
setInterval: typeof setInterval;
|
|
30
|
+
clearInterval: typeof clearInterval;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Install mock timers, replacing native implementations
|
|
35
|
+
*
|
|
36
|
+
* Saves the original timer functions for later restoration.
|
|
37
|
+
*/
|
|
38
|
+
install(): void {
|
|
39
|
+
// Save original timers
|
|
40
|
+
this.originalTimers = {
|
|
41
|
+
setTimeout: global.setTimeout,
|
|
42
|
+
clearTimeout: global.clearTimeout,
|
|
43
|
+
setInterval: global.setInterval,
|
|
44
|
+
clearInterval: global.clearInterval,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Replace with mocks
|
|
48
|
+
(global as any).setTimeout = this.setTimeout.bind(this);
|
|
49
|
+
(global as any).clearTimeout = this.clearTimeout.bind(this);
|
|
50
|
+
(global as any).setInterval = this.setInterval.bind(this);
|
|
51
|
+
(global as any).clearInterval = this.clearInterval.bind(this);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Restore native timer implementations
|
|
56
|
+
*
|
|
57
|
+
* Restores the original timer functions that were saved during install().
|
|
58
|
+
*/
|
|
59
|
+
restore(): void {
|
|
60
|
+
if (this.originalTimers) {
|
|
61
|
+
global.setTimeout = this.originalTimers.setTimeout;
|
|
62
|
+
global.clearTimeout = this.originalTimers.clearTimeout;
|
|
63
|
+
global.setInterval = this.originalTimers.setInterval;
|
|
64
|
+
global.clearInterval = this.originalTimers.clearInterval;
|
|
65
|
+
this.originalTimers = undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Mock implementation of setTimeout
|
|
71
|
+
*
|
|
72
|
+
* @param callback - Function to call when timer fires
|
|
73
|
+
* @param delay - Delay in milliseconds
|
|
74
|
+
* @returns Timer ID for cancellation
|
|
75
|
+
*/
|
|
76
|
+
setTimeout(callback: (...args: any[]) => any, delay: number): NodeJS.Timeout {
|
|
77
|
+
const id = this.nextId++ as any;
|
|
78
|
+
this.timers.set(id, { callback, delay, time: this.currentTime + delay });
|
|
79
|
+
return id;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Mock implementation of clearTimeout
|
|
84
|
+
*
|
|
85
|
+
* @param id - Timer ID to cancel
|
|
86
|
+
*/
|
|
87
|
+
clearTimeout(id: NodeJS.Timeout): void {
|
|
88
|
+
this.timers.delete(id);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Mock implementation of setInterval
|
|
93
|
+
*
|
|
94
|
+
* Note: This is a simplified implementation that doesn't truly repeat.
|
|
95
|
+
* For more complex interval testing, consider using multiple setTimeout calls.
|
|
96
|
+
*
|
|
97
|
+
* @param callback - Function to call when timer fires
|
|
98
|
+
* @param delay - Delay in milliseconds
|
|
99
|
+
* @returns Timer ID for cancellation
|
|
100
|
+
*/
|
|
101
|
+
setInterval(callback: (...args: any[]) => any, delay: number): NodeJS.Timeout {
|
|
102
|
+
// Simplified implementation - for true intervals, would need recurring logic
|
|
103
|
+
return this.setTimeout(() => {
|
|
104
|
+
callback();
|
|
105
|
+
this.setInterval(callback, delay);
|
|
106
|
+
}, delay);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Mock implementation of clearInterval
|
|
111
|
+
*
|
|
112
|
+
* @param id - Timer ID to cancel
|
|
113
|
+
*/
|
|
114
|
+
clearInterval(id: NodeJS.Timeout): void {
|
|
115
|
+
this.clearTimeout(id);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Advance time by specified amount
|
|
120
|
+
*
|
|
121
|
+
* Fires all timers that should execute within the advanced time period,
|
|
122
|
+
* in chronological order.
|
|
123
|
+
*
|
|
124
|
+
* @param ms - Milliseconds to advance
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* setTimeout(() => console.log('A'), 100);
|
|
129
|
+
* setTimeout(() => console.log('B'), 200);
|
|
130
|
+
* timer.tick(150); // Logs 'A'
|
|
131
|
+
* timer.tick(100); // Logs 'B'
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
tick(ms: number): void {
|
|
135
|
+
const targetTime = this.currentTime + ms;
|
|
136
|
+
|
|
137
|
+
while (this.currentTime < targetTime) {
|
|
138
|
+
const nextTimer = this.getNextTimer();
|
|
139
|
+
if (!nextTimer || nextTimer.time > targetTime) {
|
|
140
|
+
this.currentTime = targetTime;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this.currentTime = nextTimer.time;
|
|
145
|
+
nextTimer.callback();
|
|
146
|
+
this.timers.delete(nextTimer.id);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Get the current mock time
|
|
152
|
+
*
|
|
153
|
+
* @returns Current time in milliseconds since mock timer creation
|
|
154
|
+
*/
|
|
155
|
+
getCurrentTime(): number {
|
|
156
|
+
return this.currentTime;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get the number of pending timers
|
|
161
|
+
*
|
|
162
|
+
* @returns Count of active timers
|
|
163
|
+
*/
|
|
164
|
+
getPendingCount(): number {
|
|
165
|
+
return this.timers.size;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Clear all pending timers
|
|
170
|
+
*/
|
|
171
|
+
clearAll(): void {
|
|
172
|
+
this.timers.clear();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Reset the mock timer to initial state
|
|
177
|
+
*/
|
|
178
|
+
reset(): void {
|
|
179
|
+
this.timers.clear();
|
|
180
|
+
this.currentTime = 0;
|
|
181
|
+
this.nextId = 1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Get the next timer to fire
|
|
186
|
+
*
|
|
187
|
+
* @private
|
|
188
|
+
* @returns Timer details or null if no timers pending
|
|
189
|
+
*/
|
|
190
|
+
private getNextTimer(): { id: NodeJS.Timeout; callback: (...args: any[]) => any; time: number } | null {
|
|
191
|
+
let next: any = null;
|
|
192
|
+
let nextId: NodeJS.Timeout | null = null;
|
|
193
|
+
|
|
194
|
+
for (const [id, timer] of this.timers) {
|
|
195
|
+
if (!next || timer.time < next.time) {
|
|
196
|
+
next = timer;
|
|
197
|
+
nextId = id;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return next ? { id: nextId!, ...next } : null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Create a mock timer controller
|
|
207
|
+
*
|
|
208
|
+
* Factory function for creating MockTimerController instances.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* const timer = createMockTimer();
|
|
213
|
+
* timer.install();
|
|
214
|
+
* // ... test code ...
|
|
215
|
+
* timer.restore();
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export function createMockTimer(): MockTimerController {
|
|
219
|
+
return new MockTimerController();
|
|
220
|
+
}
|