@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,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js adapter for Vitest tests
|
|
3
|
+
* This is mostly a passthrough since Vitest natively runs in Node.js
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { timerUtils } from './test-adapter.js';
|
|
7
|
+
|
|
8
|
+
// Export timer utilities for consistency
|
|
9
|
+
export const fakeTimers = {
|
|
10
|
+
useFakeTimers: () => {
|
|
11
|
+
timerUtils.useFakeTimers();
|
|
12
|
+
return fakeTimers;
|
|
13
|
+
},
|
|
14
|
+
useRealTimers: () => {
|
|
15
|
+
timerUtils.useRealTimers();
|
|
16
|
+
return fakeTimers;
|
|
17
|
+
},
|
|
18
|
+
advanceTimersByTime: (ms: number) => {
|
|
19
|
+
timerUtils.advanceTimersByTime(ms);
|
|
20
|
+
},
|
|
21
|
+
runAllTimers: () => {
|
|
22
|
+
timerUtils.runAllTimers();
|
|
23
|
+
},
|
|
24
|
+
clearAllTimers: () => {
|
|
25
|
+
timerUtils.clearAllTimers();
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Re-export Vitest globals for consistency with other adapters
|
|
30
|
+
export const describe = (global as any).describe;
|
|
31
|
+
export const it = (global as any).it;
|
|
32
|
+
export const test = (global as any).test;
|
|
33
|
+
export const expect = (global as any).expect;
|
|
34
|
+
export const beforeEach = (global as any).beforeEach;
|
|
35
|
+
export const afterEach = (global as any).afterEach;
|
|
36
|
+
export const beforeAll = (global as any).beforeAll;
|
|
37
|
+
export const afterAll = (global as any).afterAll;
|
|
38
|
+
|
|
39
|
+
// Re-export vi object
|
|
40
|
+
export const vi = (global as any).vi;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for test-adapter runtime detection and utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
isBun,
|
|
7
|
+
sleep,
|
|
8
|
+
isDeno,
|
|
9
|
+
isNode,
|
|
10
|
+
RUNTIME,
|
|
11
|
+
timerUtils,
|
|
12
|
+
supportsFakeTimers,
|
|
13
|
+
skipIfNoFakeTimers,
|
|
14
|
+
normalizeGlobalChecks,
|
|
15
|
+
} from './test-adapter';
|
|
16
|
+
|
|
17
|
+
describe('test-adapter runtime detection', () => {
|
|
18
|
+
it('should detect the current runtime', () => {
|
|
19
|
+
const validRuntimes = ['bun', 'deno', 'node'];
|
|
20
|
+
expect(validRuntimes).toContain(RUNTIME);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should have exactly one runtime flag set to true', () => {
|
|
24
|
+
const flags = [isBun, isDeno, isNode];
|
|
25
|
+
const trueFlags = flags.filter((flag) => flag === true);
|
|
26
|
+
expect(trueFlags.length).toBe(1);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should correctly detect Node.js runtime', () => {
|
|
30
|
+
if (typeof process !== 'undefined' && process.versions?.node) {
|
|
31
|
+
expect(isNode).toBe(true);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should correctly detect Bun runtime', () => {
|
|
36
|
+
if (typeof Bun !== 'undefined') {
|
|
37
|
+
expect(isBun).toBe(true);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should correctly detect Deno runtime', () => {
|
|
42
|
+
if (typeof Deno !== 'undefined') {
|
|
43
|
+
expect(isDeno).toBe(true);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('timer utilities', () => {
|
|
49
|
+
it('should report fake timers support correctly', () => {
|
|
50
|
+
const hasFakeTimers = supportsFakeTimers();
|
|
51
|
+
if (isNode && typeof (globalThis as any).vi !== 'undefined') {
|
|
52
|
+
expect(hasFakeTimers).toBe(true);
|
|
53
|
+
} else {
|
|
54
|
+
expect(hasFakeTimers).toBe(false);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should provide timer utility methods', () => {
|
|
59
|
+
expect(typeof timerUtils.useFakeTimers).toBe('function');
|
|
60
|
+
expect(typeof timerUtils.useRealTimers).toBe('function');
|
|
61
|
+
expect(typeof timerUtils.advanceTimersByTime).toBe('function');
|
|
62
|
+
expect(typeof timerUtils.runAllTimers).toBe('function');
|
|
63
|
+
expect(typeof timerUtils.clearAllTimers).toBe('function');
|
|
64
|
+
expect(typeof timerUtils.canUseFakeTimers).toBe('function');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should correctly report skipIfNoFakeTimers', () => {
|
|
68
|
+
const shouldSkip = skipIfNoFakeTimers();
|
|
69
|
+
const hasFakeTimers = timerUtils.canUseFakeTimers();
|
|
70
|
+
|
|
71
|
+
// skipIfNoFakeTimers returns true if no fake timers, false if fake timers available
|
|
72
|
+
if (hasFakeTimers) {
|
|
73
|
+
expect(shouldSkip).toBe(false); // Don't skip if we have fake timers
|
|
74
|
+
} else {
|
|
75
|
+
expect(shouldSkip).toBe(true); // Skip if no fake timers
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('utility functions', () => {
|
|
81
|
+
it('should provide sleep utility', async () => {
|
|
82
|
+
const start = Date.now();
|
|
83
|
+
await sleep(10);
|
|
84
|
+
const elapsed = Date.now() - start;
|
|
85
|
+
expect(elapsed).toBeGreaterThanOrEqual(9); // Allow for small timing variations
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should handle normalizeGlobalChecks correctly', () => {
|
|
89
|
+
const result = normalizeGlobalChecks({});
|
|
90
|
+
expect(result).toBe(true);
|
|
91
|
+
|
|
92
|
+
if (isBun) {
|
|
93
|
+
expect(normalizeGlobalChecks(globalThis)).toBe(false);
|
|
94
|
+
} else {
|
|
95
|
+
expect(normalizeGlobalChecks(globalThis)).toBe(true);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common test adapter utilities for cross-runtime testing
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Detect runtime environment
|
|
6
|
+
export const RUNTIME = (() => {
|
|
7
|
+
if (typeof (globalThis as any).Bun !== 'undefined') return 'bun';
|
|
8
|
+
if (typeof (globalThis as any).Deno !== 'undefined') return 'deno';
|
|
9
|
+
return 'node';
|
|
10
|
+
})();
|
|
11
|
+
|
|
12
|
+
// Timer utilities that work across runtimes
|
|
13
|
+
export const timerUtils = {
|
|
14
|
+
useFakeTimers() {
|
|
15
|
+
// Node.js with Vitest supports fake timers via `vi`
|
|
16
|
+
if (RUNTIME === 'node' && typeof (globalThis as any).vi !== 'undefined') {
|
|
17
|
+
(globalThis as any).vi.useFakeTimers();
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
useRealTimers() {
|
|
22
|
+
if (RUNTIME === 'node' && typeof (globalThis as any).vi !== 'undefined') {
|
|
23
|
+
(globalThis as any).vi.useRealTimers();
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
advanceTimersByTime(ms: number) {
|
|
28
|
+
if (RUNTIME === 'node' && typeof (globalThis as any).vi !== 'undefined') {
|
|
29
|
+
(globalThis as any).vi.advanceTimersByTime(ms);
|
|
30
|
+
} else {
|
|
31
|
+
// For Bun and Deno, we can't advance timers
|
|
32
|
+
// Tests that rely on this should be skipped or use real timers
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
runAllTimers() {
|
|
37
|
+
if (RUNTIME === 'node' && typeof (globalThis as any).vi !== 'undefined') {
|
|
38
|
+
(globalThis as any).vi.runAllTimers();
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
clearAllTimers() {
|
|
43
|
+
if (RUNTIME === 'node' && typeof (globalThis as any).vi !== 'undefined') {
|
|
44
|
+
(globalThis as any).vi.clearAllTimers();
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
canUseFakeTimers(): boolean {
|
|
49
|
+
return RUNTIME === 'node';
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Helper to skip tests that require features not available in current runtime
|
|
54
|
+
export function skipIfNoFakeTimers() {
|
|
55
|
+
if (!timerUtils.canUseFakeTimers()) {
|
|
56
|
+
return true; // Skip the test
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Normalize global object checks for different runtimes
|
|
62
|
+
export function normalizeGlobalChecks(value: any): boolean {
|
|
63
|
+
// In Bun, globalThis is considered a plain object, which differs from Node/Deno
|
|
64
|
+
if (RUNTIME === 'bun' && value === globalThis) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Export runtime detection utilities
|
|
71
|
+
export const isBun = RUNTIME === 'bun';
|
|
72
|
+
export const isDeno = RUNTIME === 'deno';
|
|
73
|
+
export const isNode = RUNTIME === 'node';
|
|
74
|
+
export const isVitest =
|
|
75
|
+
isNode && typeof (globalThis as any).vi !== 'undefined' && typeof (globalThis as any).vi.useFakeTimers === 'function';
|
|
76
|
+
|
|
77
|
+
// Check if fake timers are supported
|
|
78
|
+
export const supportsFakeTimers = () => isVitest;
|
|
79
|
+
|
|
80
|
+
// Timer utilities
|
|
81
|
+
export const setupFakeTimers = () => {
|
|
82
|
+
timerUtils.useFakeTimers();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const teardownFakeTimers = () => {
|
|
86
|
+
timerUtils.useRealTimers();
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const advanceTimersByTime = (ms: number) => {
|
|
90
|
+
timerUtils.advanceTimersByTime(ms);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const clearAllTimers = () => {
|
|
94
|
+
timerUtils.clearAllTimers();
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Promise utilities for cross-runtime compatibility
|
|
98
|
+
export const expectAsync = async (fn: () => Promise<any>) => {
|
|
99
|
+
if (isBun) {
|
|
100
|
+
// Bun has different handling for async expectations
|
|
101
|
+
try {
|
|
102
|
+
await fn();
|
|
103
|
+
throw new Error('Expected promise to reject but it resolved');
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return {
|
|
106
|
+
toThrow: (expectedError?: any) => {
|
|
107
|
+
if (expectedError) {
|
|
108
|
+
(globalThis as any).expect(error).toEqual(expectedError);
|
|
109
|
+
} else {
|
|
110
|
+
(globalThis as any).expect(error).toBeDefined();
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
// Vitest/Node.js
|
|
117
|
+
return (globalThis as any).expect(fn()).rejects;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Sleep utility for real timer tests
|
|
122
|
+
export const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Container Utilities for Testing
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for working with containers in tests
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Container, type InjectionToken, type Provider } from '@omnitron-dev/titan/nexus';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Register providers from a module into a container
|
|
11
|
+
*
|
|
12
|
+
* Handles tuple format [token, provider] and direct constructors
|
|
13
|
+
*/
|
|
14
|
+
export function registerModuleProviders(container: Container, providers: any[] | undefined): void {
|
|
15
|
+
if (!providers) return;
|
|
16
|
+
|
|
17
|
+
for (const provider of providers) {
|
|
18
|
+
if (Array.isArray(provider) && provider.length === 2) {
|
|
19
|
+
// Tuple format [token, provider]
|
|
20
|
+
const [token, providerDef] = provider;
|
|
21
|
+
container.register(token, providerDef);
|
|
22
|
+
} else if (typeof provider === 'function') {
|
|
23
|
+
// Constructor - register as class provider
|
|
24
|
+
container.register(provider, { useClass: provider });
|
|
25
|
+
} else {
|
|
26
|
+
// Provider object without token - shouldn't happen
|
|
27
|
+
console.warn('Provider without token:', provider);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Register a single provider in the container
|
|
34
|
+
*/
|
|
35
|
+
export function registerProvider(container: Container, token: InjectionToken<any>, provider: Provider): void {
|
|
36
|
+
// Always use register - Container will handle the provider type
|
|
37
|
+
container.register(token, provider);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Create a test container with providers
|
|
42
|
+
*/
|
|
43
|
+
export function createTestContainer(providers?: any[]): Container {
|
|
44
|
+
const container = new Container();
|
|
45
|
+
registerModuleProviders(container, providers);
|
|
46
|
+
return container;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Helper to resolve a service from a module
|
|
51
|
+
*/
|
|
52
|
+
export async function resolveFromModule<T>(module: { providers?: any[] }, token: InjectionToken<T>): Promise<T> {
|
|
53
|
+
const container = createTestContainer(module.providers);
|
|
54
|
+
return container.resolveAsync(token);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Helper to check if a provider exists in a module
|
|
59
|
+
*/
|
|
60
|
+
export function hasProvider(module: { providers?: any[] }, token: InjectionToken<any>): boolean {
|
|
61
|
+
if (!module.providers) return false;
|
|
62
|
+
|
|
63
|
+
for (const provider of module.providers) {
|
|
64
|
+
if (Array.isArray(provider)) {
|
|
65
|
+
if (provider[0] === token) return true;
|
|
66
|
+
} else if (provider === token) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Extract all tokens from a module's providers
|
|
76
|
+
*/
|
|
77
|
+
export function getModuleTokens(module: { providers?: any[] }): InjectionToken<any>[] {
|
|
78
|
+
if (!module.providers) return [];
|
|
79
|
+
|
|
80
|
+
const tokens: InjectionToken<any>[] = [];
|
|
81
|
+
|
|
82
|
+
for (const provider of module.providers) {
|
|
83
|
+
if (Array.isArray(provider)) {
|
|
84
|
+
tokens.push(provider[0]);
|
|
85
|
+
} else if (typeof provider === 'function') {
|
|
86
|
+
tokens.push(provider);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return tokens;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Create a mock container for testing
|
|
95
|
+
*/
|
|
96
|
+
export class MockContainer extends Container {
|
|
97
|
+
private mocks = new Map<InjectionToken<any>, any>();
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Register a mock for a token
|
|
101
|
+
*/
|
|
102
|
+
mock<T>(token: InjectionToken<T>, value: T): this {
|
|
103
|
+
this.mocks.set(token, value);
|
|
104
|
+
this.register(token, { useValue: value });
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get a mock by token
|
|
110
|
+
*/
|
|
111
|
+
getMock<T>(token: InjectionToken<T>): T | undefined {
|
|
112
|
+
return this.mocks.get(token);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Clear all mocks
|
|
117
|
+
*/
|
|
118
|
+
clearMocks(): void {
|
|
119
|
+
this.mocks.clear();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Check if a token has a mock
|
|
124
|
+
*/
|
|
125
|
+
hasMock(token: InjectionToken<any>): boolean {
|
|
126
|
+
return this.mocks.has(token);
|
|
127
|
+
}
|
|
128
|
+
}
|