@omnitron-dev/testing 0.4.2 → 0.4.4
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/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 +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 +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 +47 -0
- package/dist/titan/test-module.js +120 -0
- package/dist/titan/test-module.js.map +1 -0
- package/package.json +15 -4
- 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 +89 -1
- 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 +3 -2
- package/src/titan/test-application.ts +2 -2
- package/src/titan/test-fixtures.ts +2 -1
- package/src/titan/test-module.ts +5 -3
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Testing Utilities
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive utilities for testing database operations with support for:
|
|
5
|
+
* - Automatic transaction rollback
|
|
6
|
+
* - Test data seeding
|
|
7
|
+
* - Database cleanup strategies
|
|
8
|
+
* - Docker-based real database testing
|
|
9
|
+
* - SQLite in-memory fallback
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
DatabaseTestingModule,
|
|
14
|
+
DatabaseTestingService,
|
|
15
|
+
type DatabaseTestingOptions,
|
|
16
|
+
} from './database-testing.module.js';
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
createTestDatabase,
|
|
20
|
+
withTestDatabase,
|
|
21
|
+
createTestDatabaseConfigs,
|
|
22
|
+
cleanupTestDatabaseConfigs,
|
|
23
|
+
getRecommendedTestDatabase,
|
|
24
|
+
isDockerAvailable,
|
|
25
|
+
type DatabaseTestOptions,
|
|
26
|
+
type DatabaseTestContext,
|
|
27
|
+
} from './test-utilities.js';
|
|
28
|
+
|
|
29
|
+
// Re-export DATABASE_TESTING_SERVICE constant from titan for convenience
|
|
30
|
+
export { DATABASE_TESTING_SERVICE } from '@omnitron-dev/titan/module/database';
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Testing Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for testing database operations with real databases in Docker containers
|
|
5
|
+
* Falls back to SQLite when Docker is not available
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { DockerTestManager, DatabaseTestManager, type DockerContainer } from '@omnitron-dev/testing/docker';
|
|
9
|
+
import type { DatabaseModuleOptions } from '@omnitron-dev/titan/module/database';
|
|
10
|
+
|
|
11
|
+
export interface DatabaseTestOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Preferred database dialect to use for testing
|
|
14
|
+
* If Docker is available, will start a container with this database
|
|
15
|
+
* If Docker is not available, falls back to SQLite in-memory
|
|
16
|
+
*/
|
|
17
|
+
dialect?: 'postgres' | 'mysql' | 'mariadb' | 'sqlite';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Database name
|
|
21
|
+
*/
|
|
22
|
+
database?: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Database user
|
|
26
|
+
*/
|
|
27
|
+
user?: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Database password
|
|
31
|
+
*/
|
|
32
|
+
password?: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Custom Docker path
|
|
36
|
+
*/
|
|
37
|
+
dockerPath?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Force SQLite even if Docker is available
|
|
41
|
+
*/
|
|
42
|
+
forceSqlite?: boolean;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Verbose logging
|
|
46
|
+
*/
|
|
47
|
+
verbose?: boolean;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Custom port (or 'auto' for automatic port allocation)
|
|
51
|
+
*/
|
|
52
|
+
port?: number | 'auto';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface DatabaseTestContext {
|
|
56
|
+
/**
|
|
57
|
+
* Database connection configuration
|
|
58
|
+
*/
|
|
59
|
+
connection: DatabaseModuleOptions['connection'];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Dialect being used
|
|
63
|
+
*/
|
|
64
|
+
dialect: 'postgres' | 'mysql' | 'mariadb' | 'sqlite';
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Docker container (if using Docker)
|
|
68
|
+
*/
|
|
69
|
+
container?: DockerContainer;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Cleanup function to stop container and release resources
|
|
73
|
+
*/
|
|
74
|
+
cleanup: () => Promise<void>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether Docker is being used
|
|
78
|
+
*/
|
|
79
|
+
isDocker: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Check if Docker is available
|
|
84
|
+
*/
|
|
85
|
+
export function isDockerAvailable(dockerPath?: string): boolean {
|
|
86
|
+
try {
|
|
87
|
+
DockerTestManager.getInstance({ dockerPath });
|
|
88
|
+
return true;
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Create a test database context with automatic Docker detection
|
|
96
|
+
*
|
|
97
|
+
* This function will:
|
|
98
|
+
* 1. Check if Docker is available
|
|
99
|
+
* 2. If Docker is available and not forceSqlite, start a database container
|
|
100
|
+
* 3. If Docker is not available or forceSqlite is true, use in-memory SQLite
|
|
101
|
+
* 4. Return a context object with connection config and cleanup function
|
|
102
|
+
*/
|
|
103
|
+
export async function createTestDatabase(options: DatabaseTestOptions = {}): Promise<DatabaseTestContext> {
|
|
104
|
+
const {
|
|
105
|
+
dialect = 'postgres',
|
|
106
|
+
database = 'testdb',
|
|
107
|
+
user = 'testuser',
|
|
108
|
+
password = 'testpass',
|
|
109
|
+
dockerPath,
|
|
110
|
+
forceSqlite = false,
|
|
111
|
+
verbose = false,
|
|
112
|
+
port = 'auto',
|
|
113
|
+
} = options;
|
|
114
|
+
|
|
115
|
+
// Check if Docker is available
|
|
116
|
+
const dockerAvailable = !forceSqlite && isDockerAvailable(dockerPath);
|
|
117
|
+
|
|
118
|
+
if (dockerAvailable) {
|
|
119
|
+
try {
|
|
120
|
+
if (verbose) {
|
|
121
|
+
console.log(`Starting ${dialect} container for testing...`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let container: DockerContainer;
|
|
125
|
+
let connectionConfig: DatabaseModuleOptions['connection'];
|
|
126
|
+
|
|
127
|
+
switch (dialect) {
|
|
128
|
+
case 'postgres':
|
|
129
|
+
container = await DatabaseTestManager.createPostgresContainer({
|
|
130
|
+
port,
|
|
131
|
+
database,
|
|
132
|
+
user,
|
|
133
|
+
password,
|
|
134
|
+
});
|
|
135
|
+
connectionConfig = {
|
|
136
|
+
dialect: 'postgres',
|
|
137
|
+
connection: {
|
|
138
|
+
host: container.host,
|
|
139
|
+
port: container.ports.get(5432)!,
|
|
140
|
+
database,
|
|
141
|
+
user,
|
|
142
|
+
password,
|
|
143
|
+
},
|
|
144
|
+
pool: {
|
|
145
|
+
acquireTimeoutMillis: 5000,
|
|
146
|
+
idleTimeoutMillis: 10000,
|
|
147
|
+
max: 10,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
break;
|
|
151
|
+
|
|
152
|
+
case 'mysql':
|
|
153
|
+
case 'mariadb':
|
|
154
|
+
container = await DatabaseTestManager.createMySQLContainer({
|
|
155
|
+
port,
|
|
156
|
+
database,
|
|
157
|
+
user,
|
|
158
|
+
password,
|
|
159
|
+
});
|
|
160
|
+
connectionConfig = {
|
|
161
|
+
dialect: 'mysql',
|
|
162
|
+
connection: {
|
|
163
|
+
host: container.host,
|
|
164
|
+
port: container.ports.get(3306)!,
|
|
165
|
+
database,
|
|
166
|
+
user,
|
|
167
|
+
password,
|
|
168
|
+
},
|
|
169
|
+
pool: {
|
|
170
|
+
max: 10,
|
|
171
|
+
acquireTimeoutMillis: 5000,
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
break;
|
|
175
|
+
|
|
176
|
+
default:
|
|
177
|
+
throw new Error(`Unsupported dialect for Docker: ${dialect}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (verbose) {
|
|
181
|
+
console.log(`${dialect} container started on port ${container.port}`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
connection: connectionConfig,
|
|
186
|
+
dialect,
|
|
187
|
+
container,
|
|
188
|
+
isDocker: true,
|
|
189
|
+
cleanup: async () => {
|
|
190
|
+
if (verbose) {
|
|
191
|
+
console.log(`Stopping ${dialect} container...`);
|
|
192
|
+
}
|
|
193
|
+
await container.cleanup();
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
} catch (error) {
|
|
197
|
+
console.warn(`Failed to start Docker container, falling back to SQLite: ${error}`);
|
|
198
|
+
// Fall through to SQLite fallback
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Fallback to SQLite
|
|
203
|
+
if (verbose && !forceSqlite) {
|
|
204
|
+
console.log('Docker not available, using in-memory SQLite for testing');
|
|
205
|
+
} else if (verbose) {
|
|
206
|
+
console.log('Using in-memory SQLite for testing (forced)');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
connection: {
|
|
211
|
+
dialect: 'sqlite',
|
|
212
|
+
connection: 'file::memory:?cache=shared',
|
|
213
|
+
},
|
|
214
|
+
dialect: 'sqlite',
|
|
215
|
+
isDocker: false,
|
|
216
|
+
cleanup: async () => {
|
|
217
|
+
// No cleanup needed for in-memory SQLite
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Helper function to run tests with a specific database
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* await withTestDatabase({ dialect: 'postgres' }, async (context) => {
|
|
228
|
+
* const app = await Application.create(MyModule, {
|
|
229
|
+
* config: {
|
|
230
|
+
* database: context.connection,
|
|
231
|
+
* },
|
|
232
|
+
* });
|
|
233
|
+
*
|
|
234
|
+
* // Run tests...
|
|
235
|
+
*
|
|
236
|
+
* await app.stop();
|
|
237
|
+
* });
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
export async function withTestDatabase<T>(
|
|
241
|
+
options: DatabaseTestOptions,
|
|
242
|
+
testFn: (context: DatabaseTestContext) => Promise<T>
|
|
243
|
+
): Promise<T> {
|
|
244
|
+
const context = await createTestDatabase(options);
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
return await testFn(context);
|
|
248
|
+
} finally {
|
|
249
|
+
await context.cleanup();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Create test database configuration for multiple dialects
|
|
255
|
+
*
|
|
256
|
+
* This is useful for parameterized tests that run against multiple database types
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```typescript
|
|
260
|
+
* const configs = await createTestDatabaseConfigs(['postgres', 'mysql', 'sqlite']);
|
|
261
|
+
*
|
|
262
|
+
* describe.each(configs)('Database Tests - $dialect', ({ dialect, context, cleanup }) => {
|
|
263
|
+
* afterAll(async () => {
|
|
264
|
+
* await cleanup();
|
|
265
|
+
* });
|
|
266
|
+
*
|
|
267
|
+
* it('should work with ' + dialect, async () => {
|
|
268
|
+
* // Test code using context.connection
|
|
269
|
+
* });
|
|
270
|
+
* });
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
export async function createTestDatabaseConfigs(
|
|
274
|
+
dialects: Array<'postgres' | 'mysql' | 'mariadb' | 'sqlite'>,
|
|
275
|
+
options: Omit<DatabaseTestOptions, 'dialect'> = {}
|
|
276
|
+
): Promise<
|
|
277
|
+
Array<{
|
|
278
|
+
dialect: string;
|
|
279
|
+
context: DatabaseTestContext;
|
|
280
|
+
cleanup: () => Promise<void>;
|
|
281
|
+
}>
|
|
282
|
+
> {
|
|
283
|
+
const configs: Array<{
|
|
284
|
+
dialect: string;
|
|
285
|
+
context: DatabaseTestContext;
|
|
286
|
+
cleanup: () => Promise<void>;
|
|
287
|
+
}> = [];
|
|
288
|
+
|
|
289
|
+
for (const dialect of dialects) {
|
|
290
|
+
const context = await createTestDatabase({
|
|
291
|
+
...options,
|
|
292
|
+
dialect,
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
configs.push({
|
|
296
|
+
dialect,
|
|
297
|
+
context,
|
|
298
|
+
cleanup: context.cleanup,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return configs;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Cleanup all test database contexts
|
|
307
|
+
*/
|
|
308
|
+
export async function cleanupTestDatabaseConfigs(
|
|
309
|
+
configs: Array<{
|
|
310
|
+
cleanup: () => Promise<void>;
|
|
311
|
+
}>
|
|
312
|
+
): Promise<void> {
|
|
313
|
+
await Promise.all(configs.map((config) => config.cleanup()));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Get recommended database for CI/CD environment
|
|
318
|
+
*
|
|
319
|
+
* Returns the best database to use based on the environment:
|
|
320
|
+
* - CI environments: SQLite (fast, no dependencies)
|
|
321
|
+
* - Local development with Docker: PostgreSQL
|
|
322
|
+
* - Local development without Docker: SQLite
|
|
323
|
+
*/
|
|
324
|
+
export async function getRecommendedTestDatabase(options: DatabaseTestOptions = {}): Promise<DatabaseTestContext> {
|
|
325
|
+
const isCI = process.env['CI'] === 'true' || process.env['GITHUB_ACTIONS'] === 'true';
|
|
326
|
+
|
|
327
|
+
if (isCI) {
|
|
328
|
+
// Use SQLite in CI for speed and simplicity
|
|
329
|
+
return createTestDatabase({
|
|
330
|
+
...options,
|
|
331
|
+
forceSqlite: true,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Use preferred dialect with automatic fallback
|
|
336
|
+
return createTestDatabase(options);
|
|
337
|
+
}
|
package/src/titan/index.ts
CHANGED
|
@@ -51,9 +51,97 @@ export type { RedisTestFixtureOptions, RedisTestFixture } from './redis-test-uti
|
|
|
51
51
|
export {
|
|
52
52
|
registerModuleProviders,
|
|
53
53
|
registerProvider,
|
|
54
|
-
createTestContainer,
|
|
54
|
+
createTestContainer as createSimpleTestContainer,
|
|
55
55
|
resolveFromModule,
|
|
56
56
|
hasProvider,
|
|
57
57
|
getModuleTokens,
|
|
58
58
|
MockContainer,
|
|
59
59
|
} from './container-utils.js';
|
|
60
|
+
|
|
61
|
+
// Process Manager Testing
|
|
62
|
+
export {
|
|
63
|
+
TestProcessManager,
|
|
64
|
+
createTestProcessManager,
|
|
65
|
+
MockProcessSpawner,
|
|
66
|
+
AdvancedMockProcessSpawner,
|
|
67
|
+
SimpleMockProcessSpawner,
|
|
68
|
+
MockWorker,
|
|
69
|
+
MockNetronClient,
|
|
70
|
+
MockWorkerHandle,
|
|
71
|
+
} from './pm/index.js';
|
|
72
|
+
export type {
|
|
73
|
+
ITestProcessManagerConfig,
|
|
74
|
+
IOperationRecord,
|
|
75
|
+
} from './pm/index.js';
|
|
76
|
+
|
|
77
|
+
// Nexus DI Testing
|
|
78
|
+
export {
|
|
79
|
+
MockProviderDI,
|
|
80
|
+
createMockProvider,
|
|
81
|
+
createAutoMockProvider,
|
|
82
|
+
MockProvider,
|
|
83
|
+
SpyProvider,
|
|
84
|
+
StubProvider,
|
|
85
|
+
TestContainer,
|
|
86
|
+
createTestContainer,
|
|
87
|
+
createTestContainer as createNexusTestContainer,
|
|
88
|
+
createIsolatedTestContainer,
|
|
89
|
+
TestModuleBuilder as NexusTestModuleBuilder,
|
|
90
|
+
CallCountExpectation,
|
|
91
|
+
CallArgumentsExpectation,
|
|
92
|
+
NeverCalledExpectation,
|
|
93
|
+
SnapshotContainer,
|
|
94
|
+
IsolatedContainer,
|
|
95
|
+
TestModule as NexusTestModule,
|
|
96
|
+
TestHarness,
|
|
97
|
+
waitFor,
|
|
98
|
+
createDeferred,
|
|
99
|
+
createTestProvider,
|
|
100
|
+
createTestFactoryProvider,
|
|
101
|
+
createTestClassProvider,
|
|
102
|
+
assertThrows,
|
|
103
|
+
createTestToken,
|
|
104
|
+
verifyContainerState,
|
|
105
|
+
cleanupTest,
|
|
106
|
+
createTestSuite,
|
|
107
|
+
createTestModule as createNexusTestModule,
|
|
108
|
+
createTestHarness,
|
|
109
|
+
expectResolution,
|
|
110
|
+
expectRejection,
|
|
111
|
+
expectDependency,
|
|
112
|
+
expectLifecycle,
|
|
113
|
+
} from './nexus/index.js';
|
|
114
|
+
export type {
|
|
115
|
+
MockProviderConfig,
|
|
116
|
+
MockConfig,
|
|
117
|
+
TestContainerOptions,
|
|
118
|
+
Interaction,
|
|
119
|
+
SpyExpectation,
|
|
120
|
+
TestMockConfig,
|
|
121
|
+
TestProvider,
|
|
122
|
+
TestModuleConfig,
|
|
123
|
+
TestHarnessConfig,
|
|
124
|
+
TestContainerOptionsExtended,
|
|
125
|
+
MemoryUsage,
|
|
126
|
+
ResolutionExpectation,
|
|
127
|
+
RejectionExpectation,
|
|
128
|
+
DependencyExpectation,
|
|
129
|
+
LifecycleExpectation,
|
|
130
|
+
} from './nexus/index.js';
|
|
131
|
+
|
|
132
|
+
// Database Testing
|
|
133
|
+
export {
|
|
134
|
+
DatabaseTestingModule,
|
|
135
|
+
DatabaseTestingService,
|
|
136
|
+
createTestDatabase,
|
|
137
|
+
withTestDatabase,
|
|
138
|
+
createTestDatabaseConfigs,
|
|
139
|
+
cleanupTestDatabaseConfigs,
|
|
140
|
+
getRecommendedTestDatabase,
|
|
141
|
+
isDockerAvailable,
|
|
142
|
+
} from './database/index.js';
|
|
143
|
+
export type {
|
|
144
|
+
DatabaseTestingOptions,
|
|
145
|
+
DatabaseTestOptions,
|
|
146
|
+
DatabaseTestContext,
|
|
147
|
+
} from './database/index.js';
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { ProviderDefinition, InjectionToken as Token } from '@omnitron-dev/titan/nexus';
|
|
2
|
+
|
|
3
|
+
// Jest type declaration (for build compatibility)
|
|
4
|
+
declare const jest: any;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Mock provider configuration
|
|
8
|
+
*/
|
|
9
|
+
export interface MockProviderConfig<T = any> {
|
|
10
|
+
token: Token<T>;
|
|
11
|
+
value?: T;
|
|
12
|
+
factory?: (...args: any[]) => T;
|
|
13
|
+
spy?: boolean;
|
|
14
|
+
autoMock?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Mock provider for testing (original implementation for DI)
|
|
19
|
+
*/
|
|
20
|
+
export class MockProviderDI<T> {
|
|
21
|
+
private provider: ProviderDefinition<T>;
|
|
22
|
+
|
|
23
|
+
constructor(private config: MockProviderConfig<T>) {
|
|
24
|
+
if (config.value !== undefined) {
|
|
25
|
+
this.provider = { useValue: config.value };
|
|
26
|
+
} else if (config.factory) {
|
|
27
|
+
this.provider = { useFactory: config.factory };
|
|
28
|
+
} else if (config.autoMock) {
|
|
29
|
+
this.provider = { useFactory: () => this.createAutoMock() };
|
|
30
|
+
} else {
|
|
31
|
+
this.provider = { useValue: {} as T };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getProvider(): ProviderDefinition<T> {
|
|
36
|
+
return this.provider;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create an automatic mock
|
|
41
|
+
*/
|
|
42
|
+
private createAutoMock(): T {
|
|
43
|
+
// Create a proxy that returns jest mocks for all properties
|
|
44
|
+
return new Proxy({} as any, {
|
|
45
|
+
get(target, prop) {
|
|
46
|
+
if (typeof prop === 'string') {
|
|
47
|
+
if (!(prop in target)) {
|
|
48
|
+
(target as any)[prop] = jest.fn();
|
|
49
|
+
}
|
|
50
|
+
return (target as any)[prop];
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Create a spy wrapper around the mock
|
|
59
|
+
*/
|
|
60
|
+
createSpy(): T {
|
|
61
|
+
const provider = this.provider;
|
|
62
|
+
let mock: T | undefined;
|
|
63
|
+
|
|
64
|
+
if ('useValue' in provider) {
|
|
65
|
+
mock = provider.useValue;
|
|
66
|
+
} else {
|
|
67
|
+
// For factories and other cases, use automock
|
|
68
|
+
mock = this.createAutoMock();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!mock) {
|
|
72
|
+
mock = this.createAutoMock();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof mock === 'object' && mock !== null) {
|
|
76
|
+
// Wrap all functions with jest spies
|
|
77
|
+
const spy = { ...mock } as any;
|
|
78
|
+
|
|
79
|
+
for (const key in spy) {
|
|
80
|
+
if (typeof spy[key] === 'function') {
|
|
81
|
+
spy[key] = jest.fn(spy[key]);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return spy;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return mock as T;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create a mock provider
|
|
94
|
+
*/
|
|
95
|
+
export function createMockProvider<T>(config: MockProviderConfig<T>): MockProviderDI<T> {
|
|
96
|
+
return new MockProviderDI(config);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Create an auto-mock provider
|
|
101
|
+
*/
|
|
102
|
+
export function createAutoMockProvider<T>(token: Token<T>): MockProviderDI<T> {
|
|
103
|
+
return new MockProviderDI({
|
|
104
|
+
token,
|
|
105
|
+
autoMock: true,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Mock provider that wraps jest mocks (for test utilities)
|
|
111
|
+
*/
|
|
112
|
+
export class MockProvider {
|
|
113
|
+
constructor(private mockImplementation: Record<string, any>) {
|
|
114
|
+
// Copy all mock methods to the provider instance
|
|
115
|
+
for (const [key, value] of Object.entries(mockImplementation)) {
|
|
116
|
+
if (typeof value === 'function' && jest.isMockFunction && jest.isMockFunction(value)) {
|
|
117
|
+
(this as any)[key] = value;
|
|
118
|
+
} else {
|
|
119
|
+
// Wrap non-mock functions with jest.fn()
|
|
120
|
+
(this as any)[key] = typeof value === 'function' ? jest.fn(value) : value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
[key: string]: any;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Spy provider that wraps original implementation with spies
|
|
130
|
+
*/
|
|
131
|
+
export class SpyProvider {
|
|
132
|
+
private calls = new Map<string, Array<{ args: any[]; result?: any; error?: Error }>>();
|
|
133
|
+
private spies = new Map<string, any>();
|
|
134
|
+
|
|
135
|
+
constructor(
|
|
136
|
+
private original: any,
|
|
137
|
+
methods: string[]
|
|
138
|
+
) {
|
|
139
|
+
for (const method of methods) {
|
|
140
|
+
if (typeof this.original[method] === 'function') {
|
|
141
|
+
const spy = jest.spyOn(this.original, method);
|
|
142
|
+
this.spies.set(method, spy);
|
|
143
|
+
|
|
144
|
+
// Wrap to track calls manually too
|
|
145
|
+
(this as any)[method] = (...args: any[]) => {
|
|
146
|
+
try {
|
|
147
|
+
const result = this.original[method](...args);
|
|
148
|
+
this.recordCall(method, args, result);
|
|
149
|
+
return result;
|
|
150
|
+
} catch (error) {
|
|
151
|
+
this.recordCall(method, args, undefined, error as Error);
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private recordCall(method: string, args: any[], result?: any, error?: Error): void {
|
|
160
|
+
if (!this.calls.has(method)) {
|
|
161
|
+
this.calls.set(method, []);
|
|
162
|
+
}
|
|
163
|
+
this.calls.get(method)!.push({ args, result, error });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getCallCount(method: string): number {
|
|
167
|
+
return this.calls.get(method)?.length || 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
getLastCall(method: string): { args: any[]; result?: any; error?: Error } | undefined {
|
|
171
|
+
const calls = this.calls.get(method);
|
|
172
|
+
return calls?.[calls.length - 1];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getSpy(method: string): any {
|
|
176
|
+
return this.spies.get(method);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Stub provider that returns fixed values
|
|
182
|
+
*/
|
|
183
|
+
export class StubProvider {
|
|
184
|
+
constructor(private stubs: Record<string, any>) {
|
|
185
|
+
for (const [key, value] of Object.entries(stubs)) {
|
|
186
|
+
(this as any)[key] = jest.fn().mockReturnValue(value);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|