@omnitron-dev/testing 0.1.1
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 +21 -0
- package/README.md +19 -0
- 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 +812 -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 +452 -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 +18 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- 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/runtime/bun-adapter.d.ts +1 -0
- package/dist/runtime/bun-adapter.js +100 -0
- package/dist/runtime/bun-adapter.js.map +1 -0
- package/dist/runtime/deno-adapter.d.ts +27 -0
- package/dist/runtime/deno-adapter.js +221 -0
- package/dist/runtime/deno-adapter.js.map +1 -0
- package/dist/runtime/node-adapter.d.ts +16 -0
- package/dist/runtime/node-adapter.js +30 -0
- package/dist/runtime/node-adapter.js.map +1 -0
- package/dist/runtime/test-adapter.d.ts +22 -0
- package/dist/runtime/test-adapter.js +93 -0
- package/dist/runtime/test-adapter.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 +82 -0
- package/dist/titan/database/database-testing.module.js +433 -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 +146 -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 +61 -0
- package/dist/titan/pm/mock-process-spawner.js +343 -0
- package/dist/titan/pm/mock-process-spawner.js.map +1 -0
- package/dist/titan/pm/mock-spawner.d.ts +28 -0
- package/dist/titan/pm/mock-spawner.js +101 -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 +192 -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 +415 -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 +30 -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 +47 -0
- package/dist/titan/test-module.js +120 -0
- package/dist/titan/test-module.js.map +1 -0
- package/package.json +146 -0
- package/runtime/bun.d.ts +2 -0
- package/runtime/bun.js +5 -0
- package/runtime/bun.ts +7 -0
- package/runtime/deno.d.ts +2 -0
- package/runtime/deno.js +4 -0
- package/runtime/vitest.d.ts +2 -0
- package/runtime/vitest.js +16 -0
- package/runtime/vitest.ts +7 -0
- 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 +23 -0
- package/src/async/mock-timer.ts +220 -0
- package/src/async/retry.ts +115 -0
- package/src/async/wait.ts +148 -0
- package/src/docker/database-manager.ts +242 -0
- package/src/docker/docker-test-manager.ts +1198 -0
- package/src/docker/index.ts +48 -0
- package/src/docker/redis-manager.ts +741 -0
- package/src/docker/types.ts +216 -0
- package/src/errors.ts +38 -0
- package/src/globals.d.ts +24 -0
- package/src/helpers/assertions.ts +168 -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 +43 -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/runtime/bun-adapter.ts +127 -0
- package/src/runtime/deno-adapter.ts +286 -0
- package/src/runtime/node-adapter.ts +40 -0
- package/src/runtime/test-adapter.test.ts +98 -0
- package/src/runtime/test-adapter.ts +122 -0
- package/src/titan/container-utils.ts +128 -0
- package/src/titan/database/database-testing.module.ts +851 -0
- package/src/titan/database/index.ts +30 -0
- package/src/titan/database/test-utilities.ts +340 -0
- package/src/titan/index.ts +128 -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 +895 -0
- package/src/titan/nexus/test-utilities.ts +797 -0
- package/src/titan/pm/index.ts +22 -0
- package/src/titan/pm/mock-process-spawner.ts +534 -0
- package/src/titan/pm/mock-spawner.ts +162 -0
- package/src/titan/pm/test-process-manager.ts +352 -0
- package/src/titan/redis-test-utils.ts +567 -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 +233 -0
|
@@ -0,0 +1,797 @@
|
|
|
1
|
+
import { TestContainer } from './test-container.js';
|
|
2
|
+
import { Container } from '@omnitron-dev/titan/nexus';
|
|
3
|
+
import { createToken, getTokenName } from '@omnitron-dev/titan/nexus';
|
|
4
|
+
import {
|
|
5
|
+
Scope,
|
|
6
|
+
IModule,
|
|
7
|
+
Provider,
|
|
8
|
+
ProviderDefinition,
|
|
9
|
+
Constructor,
|
|
10
|
+
InjectionToken,
|
|
11
|
+
InjectionToken as Token,
|
|
12
|
+
RegistrationOptions,
|
|
13
|
+
} from '@omnitron-dev/titan/nexus';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Test utility functions for Nexus testing
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Vitest mock function type (declared for type checking but may not be used)
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
|
+
declare const vi: any;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Test mock configuration
|
|
25
|
+
*/
|
|
26
|
+
export interface TestMockConfig {
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Provider with explicit token (NestJS style)
|
|
32
|
+
*/
|
|
33
|
+
export interface TestProvider<T = any> {
|
|
34
|
+
provide: Token<T>;
|
|
35
|
+
useValue?: T;
|
|
36
|
+
useClass?: Constructor<T>;
|
|
37
|
+
useFactory?: (...args: any[]) => T;
|
|
38
|
+
useToken?: Token<T>;
|
|
39
|
+
inject?: Token<any>[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Test module configuration
|
|
44
|
+
*/
|
|
45
|
+
export interface TestModuleConfig {
|
|
46
|
+
name: string;
|
|
47
|
+
providers?: (Provider<any> | TestProvider<any>)[];
|
|
48
|
+
mocks?: (Provider<any> | TestProvider<any>)[];
|
|
49
|
+
imports?: IModule[];
|
|
50
|
+
exports?: Token<any>[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Test harness configuration
|
|
55
|
+
*/
|
|
56
|
+
export interface TestHarnessConfig {
|
|
57
|
+
component?: Constructor<any>;
|
|
58
|
+
providers?: Array<[InjectionToken<any>, ProviderDefinition<any>] | TestProvider<any>>;
|
|
59
|
+
zone?: {
|
|
60
|
+
onEnter?: () => void;
|
|
61
|
+
onLeave?: () => void;
|
|
62
|
+
onError?: (error: Error) => void;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Test container options with leak detection
|
|
68
|
+
*/
|
|
69
|
+
export interface TestContainerOptionsExtended {
|
|
70
|
+
autoMock?: boolean;
|
|
71
|
+
detectLeaks?: boolean;
|
|
72
|
+
trackMemory?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Memory usage info
|
|
77
|
+
*/
|
|
78
|
+
export interface MemoryUsage {
|
|
79
|
+
heapUsed: number;
|
|
80
|
+
heapTotal: number;
|
|
81
|
+
objectCount: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Snapshot container for state management
|
|
86
|
+
*/
|
|
87
|
+
export class SnapshotContainer extends Container {
|
|
88
|
+
private snapshots = new Map<string, Map<Token<any>, ProviderDefinition<any>>>();
|
|
89
|
+
private snapshotCounter = 0;
|
|
90
|
+
private snapshotStack: string[] = [];
|
|
91
|
+
|
|
92
|
+
snapshot(): string {
|
|
93
|
+
const id = `snapshot_${++this.snapshotCounter}`;
|
|
94
|
+
const state = new Map<Token<any>, ProviderDefinition<any>>();
|
|
95
|
+
|
|
96
|
+
// Capture current registrations
|
|
97
|
+
if ('registrations' in this) {
|
|
98
|
+
const registrations = (this as any).registrations as Map<Token<any>, any>;
|
|
99
|
+
for (const [token, registration] of registrations) {
|
|
100
|
+
if (!Array.isArray(registration)) {
|
|
101
|
+
state.set(token, registration.provider);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.snapshots.set(id, state);
|
|
107
|
+
this.snapshotStack.push(id);
|
|
108
|
+
|
|
109
|
+
return id;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
restore(snapshotId: string): void {
|
|
113
|
+
const snapshot = this.snapshots.get(snapshotId);
|
|
114
|
+
if (!snapshot) {
|
|
115
|
+
throw new Error(`Snapshot ${snapshotId} not found`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Clear current registrations and instances completely
|
|
119
|
+
if ('registrations' in this) {
|
|
120
|
+
(this as any).registrations.clear();
|
|
121
|
+
(this as any).instances.clear();
|
|
122
|
+
// Also clear scoped instances
|
|
123
|
+
if ('scopedInstances' in this) {
|
|
124
|
+
(this as any).scopedInstances.clear();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Restore from snapshot
|
|
129
|
+
for (const [token, provider] of snapshot) {
|
|
130
|
+
this.register(token, provider);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Remove from stack
|
|
134
|
+
const stackIndex = this.snapshotStack.indexOf(snapshotId);
|
|
135
|
+
if (stackIndex >= 0) {
|
|
136
|
+
this.snapshotStack.splice(stackIndex);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
withSnapshot<T>(callback: () => T): T {
|
|
141
|
+
const snapshotId = this.snapshot();
|
|
142
|
+
try {
|
|
143
|
+
return callback();
|
|
144
|
+
} finally {
|
|
145
|
+
this.restore(snapshotId);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Override register to clear cached instances on override
|
|
151
|
+
*/
|
|
152
|
+
override register<T>(
|
|
153
|
+
tokenOrProvider: InjectionToken<T> | Provider<T> | Constructor<T>,
|
|
154
|
+
providerOrOptions?: ProviderDefinition<T> | RegistrationOptions,
|
|
155
|
+
optionsArg?: RegistrationOptions
|
|
156
|
+
): this {
|
|
157
|
+
// Check if it's an override registration
|
|
158
|
+
const options =
|
|
159
|
+
optionsArg ||
|
|
160
|
+
(typeof providerOrOptions === 'object' &&
|
|
161
|
+
!('useValue' in providerOrOptions) &&
|
|
162
|
+
!('useClass' in providerOrOptions) &&
|
|
163
|
+
!('useFactory' in providerOrOptions) &&
|
|
164
|
+
!('useToken' in providerOrOptions)
|
|
165
|
+
? (providerOrOptions as RegistrationOptions)
|
|
166
|
+
: undefined);
|
|
167
|
+
if (options?.override && 'instances' in this) {
|
|
168
|
+
// Clear cached instance when overriding
|
|
169
|
+
const token =
|
|
170
|
+
typeof tokenOrProvider === 'object' && 'provide' in tokenOrProvider
|
|
171
|
+
? tokenOrProvider.provide
|
|
172
|
+
: (tokenOrProvider as InjectionToken<T>);
|
|
173
|
+
(this as any).instances.delete(token);
|
|
174
|
+
}
|
|
175
|
+
return super.register(tokenOrProvider, providerOrOptions as any, optionsArg);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Isolated container with selective imports
|
|
181
|
+
*/
|
|
182
|
+
export class IsolatedContainer extends Container {
|
|
183
|
+
constructor() {
|
|
184
|
+
super();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static withImports(source: Container, tokens: Token<any>[]): IsolatedContainer {
|
|
188
|
+
const isolated = new IsolatedContainer();
|
|
189
|
+
|
|
190
|
+
for (const token of tokens) {
|
|
191
|
+
try {
|
|
192
|
+
const value = source.resolve(token);
|
|
193
|
+
isolated.register(token, { useValue: value });
|
|
194
|
+
} catch (_error) {
|
|
195
|
+
// Token not available in source
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return isolated;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Test module implementation
|
|
205
|
+
*/
|
|
206
|
+
export class TestModule implements IModule {
|
|
207
|
+
constructor(
|
|
208
|
+
public readonly name: string,
|
|
209
|
+
public readonly providers: Provider<any>[] = [],
|
|
210
|
+
public readonly imports: IModule[] = [],
|
|
211
|
+
public readonly exports: Token<any>[] = []
|
|
212
|
+
) {}
|
|
213
|
+
|
|
214
|
+
static override(
|
|
215
|
+
original: IModule,
|
|
216
|
+
overrides: { providers?: Array<Provider<any> | [InjectionToken<any>, Provider<any>]> }
|
|
217
|
+
): TestModule {
|
|
218
|
+
const providers = overrides.providers || original.providers || [];
|
|
219
|
+
|
|
220
|
+
// Extract tokens from providers for export
|
|
221
|
+
const exports = providers
|
|
222
|
+
.map((p) => {
|
|
223
|
+
if (Array.isArray(p)) {
|
|
224
|
+
return p[0]; // Token is first element of tuple
|
|
225
|
+
}
|
|
226
|
+
// For TestProvider format with 'provide' property
|
|
227
|
+
if (typeof p === 'object' && p && 'provide' in p) {
|
|
228
|
+
return (p as any).provide;
|
|
229
|
+
}
|
|
230
|
+
// For plain providers, we can't extract a token, so skip
|
|
231
|
+
return undefined;
|
|
232
|
+
})
|
|
233
|
+
.filter(Boolean) as Token<any>[];
|
|
234
|
+
|
|
235
|
+
// Combine with original exports
|
|
236
|
+
const originalExports = original.exports || [];
|
|
237
|
+
const allExports = [...new Set([...originalExports, ...exports])];
|
|
238
|
+
|
|
239
|
+
return new TestModule(original.name, providers as any, original.imports || [], allExports);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Test harness for component testing
|
|
245
|
+
*/
|
|
246
|
+
export class TestHarness {
|
|
247
|
+
private container: Container;
|
|
248
|
+
private zone?: TestHarnessConfig['zone'];
|
|
249
|
+
private initialized = false;
|
|
250
|
+
|
|
251
|
+
constructor(private config: TestHarnessConfig) {
|
|
252
|
+
this.container = new Container();
|
|
253
|
+
this.zone = config.zone;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async initialize(): Promise<void> {
|
|
257
|
+
if (this.config.providers) {
|
|
258
|
+
for (const providerEntry of this.config.providers) {
|
|
259
|
+
if (Array.isArray(providerEntry)) {
|
|
260
|
+
// Tuple format [token, provider]
|
|
261
|
+
const [token, provider] = providerEntry;
|
|
262
|
+
this.container.register(token, provider);
|
|
263
|
+
} else if ('provide' in providerEntry) {
|
|
264
|
+
// TestProvider format
|
|
265
|
+
const token = providerEntry.provide;
|
|
266
|
+
let provider: ProviderDefinition<any>;
|
|
267
|
+
|
|
268
|
+
if ('useValue' in providerEntry && providerEntry.useValue !== undefined) {
|
|
269
|
+
provider = { useValue: providerEntry.useValue };
|
|
270
|
+
} else if ('useClass' in providerEntry && providerEntry.useClass) {
|
|
271
|
+
provider = { useClass: providerEntry.useClass, inject: providerEntry.inject };
|
|
272
|
+
} else if ('useFactory' in providerEntry && providerEntry.useFactory) {
|
|
273
|
+
provider = { useFactory: providerEntry.useFactory, inject: providerEntry.inject };
|
|
274
|
+
} else if ('useToken' in providerEntry && providerEntry.useToken) {
|
|
275
|
+
provider = { useToken: providerEntry.useToken };
|
|
276
|
+
} else {
|
|
277
|
+
throw new Error('Invalid provider configuration');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
this.container.register(token, provider);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
this.initialized = true;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
get<T>(token: Token<T>): T {
|
|
289
|
+
if (!this.initialized) {
|
|
290
|
+
throw new Error('TestHarness not initialized');
|
|
291
|
+
}
|
|
292
|
+
return this.container.resolve(token);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async detectChanges(): Promise<void> {
|
|
296
|
+
// Force re-resolution of transient services
|
|
297
|
+
// This is a simplified implementation
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async runInZone<T>(callback: () => Promise<T>): Promise<T> {
|
|
301
|
+
if (this.zone?.onEnter) {
|
|
302
|
+
this.zone.onEnter();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
try {
|
|
306
|
+
const result = await callback();
|
|
307
|
+
if (this.zone?.onLeave) {
|
|
308
|
+
this.zone.onLeave();
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (this.zone?.onError) {
|
|
313
|
+
this.zone.onError(error as Error);
|
|
314
|
+
}
|
|
315
|
+
if (this.zone?.onLeave) {
|
|
316
|
+
this.zone.onLeave();
|
|
317
|
+
}
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async cleanup(): Promise<void> {
|
|
323
|
+
await this.container.dispose();
|
|
324
|
+
this.initialized = false;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Expectation helpers
|
|
330
|
+
*/
|
|
331
|
+
export interface ResolutionExpectation<T> {
|
|
332
|
+
toResolve(): ResolutionExpectation<T>;
|
|
333
|
+
withValue(expected: T): ResolutionExpectation<T>;
|
|
334
|
+
inTime(maxMs: number): Promise<void>;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export interface RejectionExpectation {
|
|
338
|
+
toThrow(message?: string): RejectionExpectation;
|
|
339
|
+
withErrorType(errorType: Constructor<Error>): Promise<void>;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export interface DependencyExpectation {
|
|
343
|
+
toHaveDependency(token: Token<any>): DependencyExpectation;
|
|
344
|
+
withCardinality(count: number): void;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export interface LifecycleExpectation {
|
|
348
|
+
toCallOnInit(): LifecycleExpectation;
|
|
349
|
+
toCallOnDestroy(): Promise<void>;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
class ResolutionExpectationImpl<T> implements ResolutionExpectation<T> {
|
|
353
|
+
private expectedValue?: T;
|
|
354
|
+
private shouldResolve = false;
|
|
355
|
+
private maxTime?: number;
|
|
356
|
+
|
|
357
|
+
constructor(
|
|
358
|
+
private container: Container,
|
|
359
|
+
private token: Token<T>
|
|
360
|
+
) {}
|
|
361
|
+
|
|
362
|
+
toResolve(): ResolutionExpectation<T> {
|
|
363
|
+
this.shouldResolve = true;
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
withValue(expected: T): ResolutionExpectation<T> {
|
|
368
|
+
this.expectedValue = expected;
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
inTime(maxMs: number): Promise<void> {
|
|
373
|
+
this.maxTime = maxMs;
|
|
374
|
+
return this.verify();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private async verify(): Promise<void> {
|
|
378
|
+
const start = Date.now();
|
|
379
|
+
|
|
380
|
+
try {
|
|
381
|
+
const result = this.container.resolve(this.token);
|
|
382
|
+
|
|
383
|
+
if (this.expectedValue !== undefined && result !== this.expectedValue) {
|
|
384
|
+
throw new Error(`Expected ${this.expectedValue}, got ${result}`);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (this.maxTime !== undefined) {
|
|
388
|
+
const duration = Date.now() - start;
|
|
389
|
+
if (duration > this.maxTime) {
|
|
390
|
+
throw new Error(`Resolution took ${duration}ms, expected < ${this.maxTime}ms`);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
} catch (error) {
|
|
394
|
+
if (this.shouldResolve) {
|
|
395
|
+
throw error;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
class RejectionExpectationImpl implements RejectionExpectation {
|
|
402
|
+
private expectedMessage?: string;
|
|
403
|
+
private expectedErrorType?: Constructor<Error>;
|
|
404
|
+
|
|
405
|
+
constructor(
|
|
406
|
+
private container: Container,
|
|
407
|
+
private token: Token<any>
|
|
408
|
+
) {}
|
|
409
|
+
|
|
410
|
+
toThrow(message?: string): RejectionExpectation {
|
|
411
|
+
this.expectedMessage = message;
|
|
412
|
+
return this;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
async withErrorType(errorType: Constructor<Error>): Promise<void> {
|
|
416
|
+
this.expectedErrorType = errorType;
|
|
417
|
+
return this.verify();
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private async verify(): Promise<void> {
|
|
421
|
+
try {
|
|
422
|
+
this.container.resolve(this.token);
|
|
423
|
+
throw new Error('Expected resolution to throw');
|
|
424
|
+
} catch (error) {
|
|
425
|
+
if (this.expectedErrorType && !(error instanceof this.expectedErrorType)) {
|
|
426
|
+
throw new Error(`Expected ${this.expectedErrorType.name}, got ${(error as Error).constructor.name}`, { cause: error });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (this.expectedMessage && !(error as Error).message.includes(this.expectedMessage)) {
|
|
430
|
+
throw new Error(`Expected error message to contain "${this.expectedMessage}"`, { cause: error });
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
class DependencyExpectationImpl implements DependencyExpectation {
|
|
437
|
+
private expectedToken?: Token<any>;
|
|
438
|
+
private expectedCardinality?: number;
|
|
439
|
+
|
|
440
|
+
constructor(
|
|
441
|
+
private container: Container,
|
|
442
|
+
private token: Token<any>
|
|
443
|
+
) {}
|
|
444
|
+
|
|
445
|
+
toHaveDependency(token: Token<any>): DependencyExpectation {
|
|
446
|
+
this.expectedToken = token;
|
|
447
|
+
return this;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
withCardinality(count: number): void {
|
|
451
|
+
this.expectedCardinality = count;
|
|
452
|
+
this.verify();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private verify(): void {
|
|
456
|
+
// Simplified dependency verification
|
|
457
|
+
if (this.expectedToken) {
|
|
458
|
+
try {
|
|
459
|
+
this.container.resolve(this.expectedToken);
|
|
460
|
+
} catch (_error) {
|
|
461
|
+
throw new Error(`Dependency ${String(this.expectedToken)} not found`, { cause: _error });
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
class LifecycleExpectationImpl implements LifecycleExpectation {
|
|
468
|
+
private shouldCallOnInit = false;
|
|
469
|
+
private shouldCallOnDestroy = false;
|
|
470
|
+
|
|
471
|
+
constructor(
|
|
472
|
+
private container: Container,
|
|
473
|
+
private token: Token<any>
|
|
474
|
+
) {}
|
|
475
|
+
|
|
476
|
+
toCallOnInit(): LifecycleExpectation {
|
|
477
|
+
this.shouldCallOnInit = true;
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
async toCallOnDestroy(): Promise<void> {
|
|
482
|
+
this.shouldCallOnDestroy = true;
|
|
483
|
+
return this.verify();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
private async verify(): Promise<void> {
|
|
487
|
+
// Simplified lifecycle verification
|
|
488
|
+
// In a real implementation, you'd track lifecycle method calls
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Wait for async operations to complete
|
|
494
|
+
*/
|
|
495
|
+
export async function waitFor(
|
|
496
|
+
condition: () => boolean | Promise<boolean>,
|
|
497
|
+
timeout = 5000,
|
|
498
|
+
interval = 100
|
|
499
|
+
): Promise<void> {
|
|
500
|
+
const startTime = Date.now();
|
|
501
|
+
|
|
502
|
+
while (Date.now() - startTime < timeout) {
|
|
503
|
+
const result = await condition();
|
|
504
|
+
if (result) {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
throw new Error(`Timeout waiting for condition after ${timeout}ms`);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Create a deferred promise for testing async operations
|
|
515
|
+
*/
|
|
516
|
+
export function createDeferred<T>(): {
|
|
517
|
+
promise: Promise<T>;
|
|
518
|
+
resolve: (value: T) => void;
|
|
519
|
+
reject: (error: Error) => void;
|
|
520
|
+
} {
|
|
521
|
+
let resolve: (value: T) => void;
|
|
522
|
+
let reject: (error: Error) => void;
|
|
523
|
+
|
|
524
|
+
const promise = new Promise<T>((res, rej) => {
|
|
525
|
+
resolve = res;
|
|
526
|
+
reject = rej;
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
return {
|
|
530
|
+
promise,
|
|
531
|
+
resolve: resolve!,
|
|
532
|
+
reject: reject!,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Create a test provider
|
|
538
|
+
*/
|
|
539
|
+
export function createTestProvider<T>(
|
|
540
|
+
value: T,
|
|
541
|
+
options?: {
|
|
542
|
+
scope?: Scope;
|
|
543
|
+
tags?: string[];
|
|
544
|
+
}
|
|
545
|
+
): ProviderDefinition<T> {
|
|
546
|
+
return {
|
|
547
|
+
useValue: value,
|
|
548
|
+
scope: options?.scope,
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Create a test factory provider
|
|
554
|
+
*/
|
|
555
|
+
export function createTestFactoryProvider<T>(
|
|
556
|
+
factory: (...args: any[]) => T,
|
|
557
|
+
options?: {
|
|
558
|
+
inject?: Token<any>[];
|
|
559
|
+
scope?: Scope;
|
|
560
|
+
tags?: string[];
|
|
561
|
+
}
|
|
562
|
+
): ProviderDefinition<T> {
|
|
563
|
+
return {
|
|
564
|
+
useFactory: factory,
|
|
565
|
+
inject: options?.inject,
|
|
566
|
+
scope: options?.scope,
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Create a test class provider
|
|
572
|
+
*/
|
|
573
|
+
export function createTestClassProvider<T>(
|
|
574
|
+
cls: new (...args: any[]) => T,
|
|
575
|
+
options?: {
|
|
576
|
+
inject?: Token<any>[];
|
|
577
|
+
scope?: Scope;
|
|
578
|
+
tags?: string[];
|
|
579
|
+
}
|
|
580
|
+
): ProviderDefinition<T> {
|
|
581
|
+
return {
|
|
582
|
+
useClass: cls,
|
|
583
|
+
inject: options?.inject,
|
|
584
|
+
scope: options?.scope,
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Assert that a function throws a specific error
|
|
590
|
+
*/
|
|
591
|
+
export async function assertThrows<E extends Error>(
|
|
592
|
+
fn: () => any | Promise<any>,
|
|
593
|
+
errorType?: new (...args: any[]) => E,
|
|
594
|
+
message?: string | RegExp
|
|
595
|
+
): Promise<E> {
|
|
596
|
+
try {
|
|
597
|
+
await fn();
|
|
598
|
+
throw new Error('Expected function to throw');
|
|
599
|
+
} catch (error) {
|
|
600
|
+
if (errorType && !(error instanceof errorType)) {
|
|
601
|
+
throw new Error(`Expected error of type ${errorType.name}, got ${(error as Error).constructor.name}`, { cause: error });
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (message) {
|
|
605
|
+
const errorMessage = (error as Error).message;
|
|
606
|
+
if (typeof message === 'string') {
|
|
607
|
+
if (!errorMessage.includes(message)) {
|
|
608
|
+
throw new Error(`Expected error message to include "${message}", got "${errorMessage}"`, { cause: error });
|
|
609
|
+
}
|
|
610
|
+
} else if (message instanceof RegExp) {
|
|
611
|
+
if (!message.test(errorMessage)) {
|
|
612
|
+
throw new Error(`Expected error message to match ${message}, got "${errorMessage}"`, { cause: error });
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return error as E;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Create a test token with metadata
|
|
623
|
+
*/
|
|
624
|
+
export function createTestToken<T>(name: string, metadata?: Record<string, any>): Token<T> {
|
|
625
|
+
return createToken<T>(name, metadata);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Verify container state
|
|
630
|
+
*/
|
|
631
|
+
export function verifyContainerState(
|
|
632
|
+
container: TestContainer,
|
|
633
|
+
expectations: {
|
|
634
|
+
registeredTokens?: Token<any>[];
|
|
635
|
+
resolvedTokens?: Token<any>[];
|
|
636
|
+
mocks?: Token<any>[];
|
|
637
|
+
spies?: Array<{ token: Token<any>; method: string }>;
|
|
638
|
+
}
|
|
639
|
+
): void {
|
|
640
|
+
const _metadata = container.getTestMetadata();
|
|
641
|
+
|
|
642
|
+
if (expectations.registeredTokens) {
|
|
643
|
+
for (const token of expectations.registeredTokens) {
|
|
644
|
+
// Check if token is registered
|
|
645
|
+
try {
|
|
646
|
+
container.resolve(token);
|
|
647
|
+
} catch {
|
|
648
|
+
// Token might not be resolvable but could be registered
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (expectations.resolvedTokens) {
|
|
654
|
+
for (const token of expectations.resolvedTokens) {
|
|
655
|
+
try {
|
|
656
|
+
container.resolve(token);
|
|
657
|
+
} catch {
|
|
658
|
+
throw new Error(`Expected token ${getTokenName(token)} to be resolvable`);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (expectations.mocks) {
|
|
664
|
+
for (const token of expectations.mocks) {
|
|
665
|
+
try {
|
|
666
|
+
container.getMock(token);
|
|
667
|
+
} catch {
|
|
668
|
+
throw new Error(`Expected token ${getTokenName(token)} to have a mock`);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (expectations.spies) {
|
|
674
|
+
for (const { token, method } of expectations.spies) {
|
|
675
|
+
try {
|
|
676
|
+
container.getSpy(token, method as any);
|
|
677
|
+
} catch {
|
|
678
|
+
throw new Error(`Expected token ${getTokenName(token)} to have a spy for method ${method}`);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Clean up test resources
|
|
686
|
+
*/
|
|
687
|
+
export function cleanupTest(container: TestContainer): void {
|
|
688
|
+
container.resetMocks();
|
|
689
|
+
container.clearMocks();
|
|
690
|
+
container.clearInteractions();
|
|
691
|
+
container.dispose();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* Create a test suite helper
|
|
696
|
+
*/
|
|
697
|
+
export function createTestSuite(
|
|
698
|
+
name: string,
|
|
699
|
+
setup: () => TestContainer | Promise<TestContainer>
|
|
700
|
+
): {
|
|
701
|
+
container: TestContainer;
|
|
702
|
+
beforeEach: () => Promise<void>;
|
|
703
|
+
afterEach: () => void;
|
|
704
|
+
} {
|
|
705
|
+
let container: TestContainer;
|
|
706
|
+
|
|
707
|
+
return {
|
|
708
|
+
get container() {
|
|
709
|
+
return container;
|
|
710
|
+
},
|
|
711
|
+
|
|
712
|
+
async beforeEach() {
|
|
713
|
+
container = await setup();
|
|
714
|
+
},
|
|
715
|
+
|
|
716
|
+
afterEach() {
|
|
717
|
+
if (container) {
|
|
718
|
+
cleanupTest(container);
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Create test module
|
|
726
|
+
*/
|
|
727
|
+
export function createTestModule(config: TestModuleConfig): TestModule {
|
|
728
|
+
// Convert TestProvider format to tuple format and collect tokens
|
|
729
|
+
const allProviders: Array<[Token<any>, ProviderDefinition<any>]> = [];
|
|
730
|
+
const tokens: Token<any>[] = [];
|
|
731
|
+
|
|
732
|
+
const processProviders = (providers: (Provider<any> | TestProvider<any>)[] | undefined) => {
|
|
733
|
+
if (!providers) return;
|
|
734
|
+
|
|
735
|
+
for (const provider of providers) {
|
|
736
|
+
if ('provide' in provider) {
|
|
737
|
+
// TestProvider format - extract token and convert to regular provider
|
|
738
|
+
const token = provider.provide;
|
|
739
|
+
tokens.push(token);
|
|
740
|
+
|
|
741
|
+
let regularProvider: ProviderDefinition<any>;
|
|
742
|
+
if ('useValue' in provider && provider.useValue !== undefined) {
|
|
743
|
+
regularProvider = { useValue: provider.useValue };
|
|
744
|
+
} else if ('useClass' in provider && provider.useClass) {
|
|
745
|
+
regularProvider = { useClass: provider.useClass, inject: provider.inject };
|
|
746
|
+
} else if ('useFactory' in provider && provider.useFactory) {
|
|
747
|
+
regularProvider = { useFactory: provider.useFactory, inject: provider.inject };
|
|
748
|
+
} else if ('useToken' in provider && provider.useToken) {
|
|
749
|
+
regularProvider = { useToken: provider.useToken };
|
|
750
|
+
} else {
|
|
751
|
+
throw new Error('Invalid provider configuration');
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
allProviders.push([token, regularProvider]);
|
|
755
|
+
} else {
|
|
756
|
+
// Regular Provider format - can't extract token automatically
|
|
757
|
+
// This is a limitation - regular providers need explicit exports
|
|
758
|
+
allProviders.push(provider as any);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
processProviders(config.providers);
|
|
764
|
+
processProviders(config.mocks);
|
|
765
|
+
|
|
766
|
+
// Use provided exports or auto-export collected tokens
|
|
767
|
+
const exports = config.exports || tokens;
|
|
768
|
+
|
|
769
|
+
// Create the module with providers in tuple format
|
|
770
|
+
return new TestModule(config.name, allProviders as any, config.imports, exports as Token<any>[]);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* Create test harness
|
|
775
|
+
*/
|
|
776
|
+
export function createTestHarness(config: TestHarnessConfig): TestHarness {
|
|
777
|
+
return new TestHarness(config);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Expectation helper functions
|
|
782
|
+
*/
|
|
783
|
+
export function expectResolution<T>(container: Container, token: Token<T>): ResolutionExpectation<T> {
|
|
784
|
+
return new ResolutionExpectationImpl(container, token);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
export function expectRejection(container: Container, token: Token<any>): RejectionExpectation {
|
|
788
|
+
return new RejectionExpectationImpl(container, token);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export function expectDependency(container: Container, token: Token<any>): DependencyExpectation {
|
|
792
|
+
return new DependencyExpectationImpl(container, token);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
export function expectLifecycle(container: Container, token: Token<any>): LifecycleExpectation {
|
|
796
|
+
return new LifecycleExpectationImpl(container, token);
|
|
797
|
+
}
|