@omnitron-dev/testing 0.4.5 → 0.4.6
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/README.md +43 -0
- package/dist/async/events.js.map +1 -1
- package/dist/async/retry.js.map +1 -1
- package/dist/async/wait.js.map +1 -1
- package/dist/docker/database-manager.js +13 -15
- package/dist/docker/database-manager.js.map +1 -1
- package/dist/docker/docker-test-manager.d.ts +12 -3
- package/dist/docker/docker-test-manager.js +286 -255
- package/dist/docker/docker-test-manager.js.map +1 -1
- package/dist/docker/redis-manager.js +68 -94
- package/dist/docker/redis-manager.js.map +1 -1
- package/dist/docker/types.d.ts +7 -2
- package/dist/env.d.ts +11 -0
- package/dist/env.js +21 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.js.map +1 -1
- package/dist/helpers/assertions.js +3 -3
- package/dist/helpers/assertions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/runtime/bun-adapter.d.ts +23 -1
- package/dist/runtime/bun-adapter.js +14 -1
- package/dist/runtime/bun-adapter.js.map +1 -1
- package/dist/runtime/deno-adapter.d.ts +72 -15
- package/dist/runtime/deno-adapter.js +93 -78
- package/dist/runtime/deno-adapter.js.map +1 -1
- package/dist/runtime/node-adapter.d.ts +15 -3
- package/dist/runtime/node-adapter.js +1 -1
- package/dist/runtime/node-adapter.js.map +1 -1
- package/dist/runtime/test-adapter.d.ts +1 -1
- package/dist/runtime/test-adapter.js +12 -14
- package/dist/runtime/test-adapter.js.map +1 -1
- package/dist/titan/database/database-testing.module.d.ts +7 -6
- package/dist/titan/database/database-testing.module.js +39 -18
- package/dist/titan/database/database-testing.module.js.map +1 -1
- package/dist/titan/database/index.d.ts +1 -1
- package/dist/titan/database/index.js +1 -1
- package/dist/titan/database/index.js.map +1 -1
- package/dist/titan/database/test-utilities.d.ts +2 -2
- package/dist/titan/database/test-utilities.js +3 -2
- package/dist/titan/database/test-utilities.js.map +1 -1
- package/dist/titan/index.d.ts +4 -4
- package/dist/titan/index.js +3 -3
- package/dist/titan/index.js.map +1 -1
- package/dist/titan/nexus/mock-provider.js +6 -6
- package/dist/titan/nexus/mock-provider.js.map +1 -1
- package/dist/titan/nexus/test-container.js +10 -10
- package/dist/titan/nexus/test-container.js.map +1 -1
- package/dist/titan/nexus/test-utilities.js +9 -9
- package/dist/titan/nexus/test-utilities.js.map +1 -1
- package/dist/titan/pm/mock-process-spawner.d.ts +4 -3
- package/dist/titan/pm/mock-process-spawner.js +33 -14
- package/dist/titan/pm/mock-process-spawner.js.map +1 -1
- package/dist/titan/pm/mock-spawner.d.ts +2 -1
- package/dist/titan/pm/mock-spawner.js +3 -1
- package/dist/titan/pm/mock-spawner.js.map +1 -1
- package/dist/titan/pm/test-process-manager.d.ts +4 -4
- package/dist/titan/pm/test-process-manager.js +7 -3
- package/dist/titan/pm/test-process-manager.js.map +1 -1
- package/dist/titan/redis-test-utils.d.ts +8 -2
- package/dist/titan/redis-test-utils.js +147 -92
- package/dist/titan/redis-test-utils.js.map +1 -1
- package/dist/titan/test-application.d.ts +1 -1
- package/dist/titan/test-application.js +1 -1
- package/dist/titan/test-application.js.map +1 -1
- package/dist/titan/test-fixtures.js.map +1 -1
- package/dist/titan/test-mocks.d.ts +12 -11
- package/dist/titan/test-mocks.js +49 -49
- package/dist/titan/test-mocks.js.map +1 -1
- package/dist/titan/test-module.js.map +1 -1
- package/package.json +66 -40
- package/src/async/events.ts +1 -1
- package/src/docker/a-port-the-network-could-reach.spec.ts +66 -0
- package/src/docker/database-manager.ts +13 -15
- package/src/docker/docker-orphan-reaper.spec.ts +175 -0
- package/src/docker/docker-port-collision.spec.ts +97 -0
- package/src/docker/docker-test-manager.spec.ts +122 -0
- package/src/docker/docker-test-manager.ts +420 -326
- package/src/docker/docker-volume-cleanup.spec.ts +113 -0
- package/src/docker/redis-cluster-creation.spec.ts +52 -0
- package/src/docker/redis-manager.ts +80 -115
- package/src/docker/types.ts +11 -4
- package/src/env.spec.ts +20 -0
- package/src/env.ts +33 -0
- package/src/globals.d.ts +2 -2
- package/src/helpers/assertions.ts +3 -3
- package/src/index.ts +1 -1
- package/src/runtime/adapter-surface.spec.ts +83 -0
- package/src/runtime/bun-adapter.ts +36 -5
- package/src/runtime/deno-adapter.ts +167 -108
- package/src/runtime/node-adapter.ts +5 -5
- package/src/runtime/test-adapter.test.ts +1 -1
- package/src/runtime/test-adapter.ts +15 -17
- package/src/titan/database/database-testing.module.ts +63 -22
- package/src/titan/database/index.ts +2 -2
- package/src/titan/database/test-utilities.ts +6 -3
- package/src/titan/index.ts +8 -1
- package/src/titan/nexus/mock-provider.ts +12 -12
- package/src/titan/nexus/test-container.ts +12 -12
- package/src/titan/nexus/test-utilities.ts +13 -11
- package/src/titan/pm/mock-process-spawner.ts +74 -26
- package/src/titan/pm/mock-spawner.ts +10 -4
- package/src/titan/pm/test-process-manager.ts +26 -9
- package/src/titan/redis-test-utils.test.ts +102 -0
- package/src/titan/redis-test-utils.ts +191 -109
- package/src/titan/test-application.ts +2 -2
- package/src/titan/test-fixtures.ts +0 -1
- package/src/titan/test-mocks.ts +50 -50
- package/runtime/bun.d.ts +0 -2
- package/runtime/bun.js +0 -5
- package/runtime/bun.ts +0 -7
- package/runtime/deno.d.ts +0 -2
- package/runtime/deno.js +0 -4
- package/runtime/jest.d.ts +0 -2
- package/runtime/jest.js +0 -16
- package/runtime/jest.ts +0 -7
|
@@ -8,10 +8,9 @@
|
|
|
8
8
|
import { DynamicModule, IModule } from '@omnitron-dev/titan/nexus';
|
|
9
9
|
import { Module, Injectable, Inject } from '@omnitron-dev/titan/decorators';
|
|
10
10
|
import { Kysely, Transaction, sql } from 'kysely';
|
|
11
|
-
import { TitanDatabaseModule } from '@omnitron-dev/titan
|
|
12
|
-
import { DatabaseManager } from '@omnitron-dev/titan
|
|
13
|
-
import { MigrationRunner } from '@omnitron-dev/titan
|
|
14
|
-
import { TransactionManager } from '@omnitron-dev/titan/module/database';
|
|
11
|
+
import { TitanDatabaseModule } from '@omnitron-dev/titan-database';
|
|
12
|
+
import { DatabaseManager } from '@omnitron-dev/titan-database';
|
|
13
|
+
import { MigrationRunner } from '@omnitron-dev/titan-database';
|
|
15
14
|
import { Errors } from '@omnitron-dev/titan/errors';
|
|
16
15
|
// @kysera/testing utilities (moved from @kysera/core in 0.7.0)
|
|
17
16
|
import {
|
|
@@ -29,11 +28,15 @@ import {
|
|
|
29
28
|
} from '@kysera/testing';
|
|
30
29
|
import {
|
|
31
30
|
DATABASE_MANAGER,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
} from '@omnitron-dev/titan-database';
|
|
32
|
+
|
|
33
|
+
// These tokens were removed from titan's public exports but are still needed
|
|
34
|
+
// for optional DI injection in the testing module. Using Symbol.for() ensures
|
|
35
|
+
// they match any other code that creates the same symbol.
|
|
36
|
+
const DATABASE_MIGRATION_SERVICE = Symbol.for('titan:DATABASE_MIGRATION_SERVICE');
|
|
37
|
+
const DATABASE_TRANSACTION_MANAGER = Symbol.for('titan:DATABASE_TRANSACTION_MANAGER');
|
|
38
|
+
export const DATABASE_TESTING_SERVICE = Symbol.for('titan:DATABASE_TESTING_SERVICE');
|
|
39
|
+
import type { DatabaseModuleOptions } from '@omnitron-dev/titan-database';
|
|
37
40
|
|
|
38
41
|
export interface DatabaseTestingOptions extends Partial<DatabaseModuleOptions> {
|
|
39
42
|
/**
|
|
@@ -97,7 +100,7 @@ export class DatabaseTestingService {
|
|
|
97
100
|
@Inject(DATABASE_MANAGER) private manager: DatabaseManager,
|
|
98
101
|
@Inject('DATABASE_TESTING_OPTIONS') private options: DatabaseTestingOptions,
|
|
99
102
|
@Inject(DATABASE_MIGRATION_SERVICE) private migrationRunner?: MigrationRunner,
|
|
100
|
-
@Inject(DATABASE_TRANSACTION_MANAGER) private transactionManager?:
|
|
103
|
+
@Inject(DATABASE_TRANSACTION_MANAGER) private transactionManager?: unknown
|
|
101
104
|
) {}
|
|
102
105
|
|
|
103
106
|
/**
|
|
@@ -115,7 +118,7 @@ export class DatabaseTestingService {
|
|
|
115
118
|
|
|
116
119
|
// Run migrations if enabled
|
|
117
120
|
if (this.options.autoMigrate && this.migrationRunner) {
|
|
118
|
-
await this.migrationRunner.
|
|
121
|
+
await this.migrationRunner.up();
|
|
119
122
|
}
|
|
120
123
|
|
|
121
124
|
// Seed database if enabled
|
|
@@ -130,7 +133,7 @@ export class DatabaseTestingService {
|
|
|
130
133
|
* Setup before each test
|
|
131
134
|
*/
|
|
132
135
|
async beforeEach(): Promise<void> {
|
|
133
|
-
const
|
|
136
|
+
const _db = await this.manager.getConnection();
|
|
134
137
|
|
|
135
138
|
// Clean database if enabled
|
|
136
139
|
if (this.options.autoClean) {
|
|
@@ -225,6 +228,25 @@ export class DatabaseTestingService {
|
|
|
225
228
|
}
|
|
226
229
|
}
|
|
227
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Convert booleans to integers for SQLite compatibility
|
|
233
|
+
*/
|
|
234
|
+
private convertBooleansForSqlite(
|
|
235
|
+
db: Kysely<Record<string, unknown>> | Transaction<unknown>,
|
|
236
|
+
data: Record<string, unknown>
|
|
237
|
+
): Record<string, unknown> {
|
|
238
|
+
const dialect = this.getDialect(db as Kysely<Record<string, unknown>>);
|
|
239
|
+
if (dialect !== 'sqlite') {
|
|
240
|
+
return data;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const result: Record<string, unknown> = {};
|
|
244
|
+
for (const [key, value] of Object.entries(data)) {
|
|
245
|
+
result[key] = typeof value === 'boolean' ? (value ? 1 : 0) : value;
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
|
|
228
250
|
/**
|
|
229
251
|
* Insert seed data
|
|
230
252
|
*/
|
|
@@ -238,7 +260,11 @@ export class DatabaseTestingService {
|
|
|
238
260
|
const records = Array.isArray(seed.data) ? seed.data : [seed.data];
|
|
239
261
|
|
|
240
262
|
for (const record of records) {
|
|
241
|
-
|
|
263
|
+
const convertedRecord = this.convertBooleansForSqlite(
|
|
264
|
+
db as Kysely<Record<string, unknown>>,
|
|
265
|
+
record as Record<string, unknown>
|
|
266
|
+
);
|
|
267
|
+
await (db as Kysely<Record<string, unknown>>).insertInto(seed.table).values(convertedRecord).execute();
|
|
242
268
|
}
|
|
243
269
|
} catch (error) {
|
|
244
270
|
if (this.options.verbose) {
|
|
@@ -321,7 +347,13 @@ export class DatabaseTestingService {
|
|
|
321
347
|
* Get database dialect
|
|
322
348
|
*/
|
|
323
349
|
private getDialect(db: Kysely<Record<string, unknown>>): string {
|
|
324
|
-
//
|
|
350
|
+
// First, try to get dialect from the DatabaseManager (most reliable)
|
|
351
|
+
const managerDialect = this.manager.getDialect();
|
|
352
|
+
if (managerDialect) {
|
|
353
|
+
return managerDialect;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Fallback: Try to determine dialect from the Kysely instance structure
|
|
325
357
|
const dbWithDialect = db as unknown as {
|
|
326
358
|
dialect?: { name?: string };
|
|
327
359
|
_dialect?: { name?: string };
|
|
@@ -336,7 +368,8 @@ export class DatabaseTestingService {
|
|
|
336
368
|
if (dbWithDialect.driver?.postgres) return 'postgres';
|
|
337
369
|
if (dbWithDialect.driver?.mysql) return 'mysql';
|
|
338
370
|
|
|
339
|
-
|
|
371
|
+
// If we can't determine the dialect, throw an error instead of defaulting to sqlite
|
|
372
|
+
throw Errors.conflict('Unable to determine database dialect. Ensure DatabaseManager is properly configured.');
|
|
340
373
|
}
|
|
341
374
|
|
|
342
375
|
/**
|
|
@@ -372,8 +405,8 @@ export class DatabaseTestingService {
|
|
|
372
405
|
/**
|
|
373
406
|
* Get test database connection
|
|
374
407
|
*/
|
|
375
|
-
getTestConnection(): Kysely<Record<string, unknown>> | Transaction<unknown
|
|
376
|
-
return (this.currentTransaction || this.manager.getConnection()) as
|
|
408
|
+
async getTestConnection(): Promise<Kysely<Record<string, unknown>> | Transaction<unknown>> {
|
|
409
|
+
return (this.currentTransaction || (await this.manager.getConnection())) as
|
|
377
410
|
| Kysely<Record<string, unknown>>
|
|
378
411
|
| Transaction<unknown>;
|
|
379
412
|
}
|
|
@@ -386,14 +419,18 @@ export class DatabaseTestingService {
|
|
|
386
419
|
generator: () => Partial<T> | Promise<Partial<T>>,
|
|
387
420
|
count: number = 1
|
|
388
421
|
): Promise<T[]> {
|
|
389
|
-
const db = this.getTestConnection();
|
|
422
|
+
const db = await this.getTestConnection();
|
|
390
423
|
const results: T[] = [];
|
|
391
424
|
|
|
392
425
|
for (let i = 0; i < count; i++) {
|
|
393
426
|
const data = await generator();
|
|
427
|
+
const convertedData = this.convertBooleansForSqlite(
|
|
428
|
+
db as Kysely<Record<string, unknown>>,
|
|
429
|
+
data as Record<string, unknown>
|
|
430
|
+
);
|
|
394
431
|
const result = await (db as Kysely<Record<string, unknown>>)
|
|
395
432
|
.insertInto(table)
|
|
396
|
-
.values(
|
|
433
|
+
.values(convertedData)
|
|
397
434
|
.returningAll()
|
|
398
435
|
.executeTakeFirst();
|
|
399
436
|
|
|
@@ -409,7 +446,7 @@ export class DatabaseTestingService {
|
|
|
409
446
|
* Assert database state
|
|
410
447
|
*/
|
|
411
448
|
async assertDatabaseHas(table: string, data: Record<string, unknown>): Promise<boolean> {
|
|
412
|
-
const db = this.getTestConnection() as Kysely<Record<string, unknown>>;
|
|
449
|
+
const db = (await this.getTestConnection()) as Kysely<Record<string, unknown>>;
|
|
413
450
|
|
|
414
451
|
let query = db.selectFrom(table).selectAll();
|
|
415
452
|
|
|
@@ -434,7 +471,7 @@ export class DatabaseTestingService {
|
|
|
434
471
|
* Count records in table
|
|
435
472
|
*/
|
|
436
473
|
async assertDatabaseCount(table: string, count: number): Promise<boolean> {
|
|
437
|
-
const db = this.getTestConnection();
|
|
474
|
+
const db = await this.getTestConnection();
|
|
438
475
|
|
|
439
476
|
const result = await (db as Kysely<Record<string, unknown>>)
|
|
440
477
|
.selectFrom(table)
|
|
@@ -620,7 +657,7 @@ export class DatabaseTestingService {
|
|
|
620
657
|
count: number,
|
|
621
658
|
overrides?: Partial<T>
|
|
622
659
|
): Promise<T[]> {
|
|
623
|
-
const db = this.getTestConnection() as Kysely<Record<string, unknown>>;
|
|
660
|
+
const db = (await this.getTestConnection()) as Kysely<Record<string, unknown>>;
|
|
624
661
|
const results: T[] = [];
|
|
625
662
|
|
|
626
663
|
for (let i = 0; i < count; i++) {
|
|
@@ -744,6 +781,7 @@ export class DatabaseTestingModule {
|
|
|
744
781
|
const module = DatabaseTestingModule.forTest(isolatedOptions);
|
|
745
782
|
|
|
746
783
|
// Store service reference for cleanup
|
|
784
|
+
|
|
747
785
|
let serviceInstance: DatabaseTestingService | undefined;
|
|
748
786
|
|
|
749
787
|
// Return module with enhanced cleanup that properly disposes the service
|
|
@@ -752,6 +790,9 @@ export class DatabaseTestingModule {
|
|
|
752
790
|
get service() {
|
|
753
791
|
return serviceInstance;
|
|
754
792
|
},
|
|
793
|
+
set service(instance: DatabaseTestingService | undefined) {
|
|
794
|
+
serviceInstance = instance;
|
|
795
|
+
},
|
|
755
796
|
cleanup: async () => {
|
|
756
797
|
try {
|
|
757
798
|
// If service was injected, clean it up
|
|
@@ -26,5 +26,5 @@ export {
|
|
|
26
26
|
type DatabaseTestContext,
|
|
27
27
|
} from './test-utilities.js';
|
|
28
28
|
|
|
29
|
-
// Re-export DATABASE_TESTING_SERVICE
|
|
30
|
-
export { DATABASE_TESTING_SERVICE } from '
|
|
29
|
+
// Re-export DATABASE_TESTING_SERVICE from the testing module
|
|
30
|
+
export { DATABASE_TESTING_SERVICE } from './database-testing.module.js';
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* Falls back to SQLite when Docker is not available
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { DockerTestManager, DatabaseTestManager, type DockerContainer } from '
|
|
9
|
-
import type { DatabaseModuleOptions } from '@omnitron-dev/titan
|
|
8
|
+
import { DockerTestManager, DatabaseTestManager, type DockerContainer } from '../../docker/index.js';
|
|
9
|
+
import type { DatabaseModuleOptions } from '@omnitron-dev/titan-database';
|
|
10
10
|
|
|
11
11
|
export interface DatabaseTestOptions {
|
|
12
12
|
/**
|
|
@@ -206,10 +206,13 @@ export async function createTestDatabase(options: DatabaseTestOptions = {}): Pro
|
|
|
206
206
|
console.log('Using in-memory SQLite for testing (forced)');
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
+
// Create unique in-memory database for each test to avoid conflicts
|
|
210
|
+
const uniqueId = `test_${Date.now()}_${Math.random().toString(36).substring(7)}`;
|
|
211
|
+
|
|
209
212
|
return {
|
|
210
213
|
connection: {
|
|
211
214
|
dialect: 'sqlite',
|
|
212
|
-
connection:
|
|
215
|
+
connection: `file:${uniqueId}?mode=memory&cache=shared`,
|
|
213
216
|
},
|
|
214
217
|
dialect: 'sqlite',
|
|
215
218
|
isDocker: false,
|
package/src/titan/index.ts
CHANGED
|
@@ -19,6 +19,7 @@ export {
|
|
|
19
19
|
createMockLogger,
|
|
20
20
|
createMockConfigService,
|
|
21
21
|
createMockRedisManager,
|
|
22
|
+
createMockRedisClient,
|
|
22
23
|
createMockSchedulerService,
|
|
23
24
|
createMockEventBus,
|
|
24
25
|
} from './test-mocks.js';
|
|
@@ -29,9 +30,14 @@ export {
|
|
|
29
30
|
cleanupRedisTestFixture,
|
|
30
31
|
waitForRedis,
|
|
31
32
|
createMockRedis,
|
|
33
|
+
resolveTestRedisEndpoint,
|
|
32
34
|
RedisPerformanceTester,
|
|
33
35
|
} from './redis-test-utils.js';
|
|
34
|
-
export type {
|
|
36
|
+
export type {
|
|
37
|
+
RedisTestFixtureOptions,
|
|
38
|
+
RedisTestFixture,
|
|
39
|
+
TestRedisEndpoint,
|
|
40
|
+
} from './redis-test-utils.js';
|
|
35
41
|
|
|
36
42
|
// Container Utils
|
|
37
43
|
export {
|
|
@@ -115,6 +121,7 @@ export type {
|
|
|
115
121
|
// Database Testing
|
|
116
122
|
export {
|
|
117
123
|
DatabaseTestingModule,
|
|
124
|
+
DATABASE_TESTING_SERVICE,
|
|
118
125
|
DatabaseTestingService,
|
|
119
126
|
createTestDatabase,
|
|
120
127
|
withTestDatabase,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ProviderDefinition, InjectionToken as Token } from '@omnitron-dev/titan/nexus';
|
|
2
2
|
|
|
3
|
-
//
|
|
4
|
-
declare const
|
|
3
|
+
// Vitest type declaration (for build compatibility)
|
|
4
|
+
declare const vi: any;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Mock provider configuration
|
|
@@ -40,12 +40,12 @@ export class MockProviderDI<T> {
|
|
|
40
40
|
* Create an automatic mock
|
|
41
41
|
*/
|
|
42
42
|
private createAutoMock(): T {
|
|
43
|
-
// Create a proxy that returns
|
|
43
|
+
// Create a proxy that returns vi mocks for all properties
|
|
44
44
|
return new Proxy({} as any, {
|
|
45
45
|
get(target, prop) {
|
|
46
46
|
if (typeof prop === 'string') {
|
|
47
47
|
if (!(prop in target)) {
|
|
48
|
-
(target as any)[prop] =
|
|
48
|
+
(target as any)[prop] = vi.fn();
|
|
49
49
|
}
|
|
50
50
|
return (target as any)[prop];
|
|
51
51
|
}
|
|
@@ -73,12 +73,12 @@ export class MockProviderDI<T> {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
if (typeof mock === 'object' && mock !== null) {
|
|
76
|
-
// Wrap all functions with
|
|
76
|
+
// Wrap all functions with vi spies
|
|
77
77
|
const spy = { ...mock } as any;
|
|
78
78
|
|
|
79
79
|
for (const key in spy) {
|
|
80
80
|
if (typeof spy[key] === 'function') {
|
|
81
|
-
spy[key] =
|
|
81
|
+
spy[key] = vi.fn(spy[key]);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|
|
@@ -107,17 +107,17 @@ export function createAutoMockProvider<T>(token: Token<T>): MockProviderDI<T> {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
* Mock provider that wraps
|
|
110
|
+
* Mock provider that wraps vi mocks (for test utilities)
|
|
111
111
|
*/
|
|
112
112
|
export class MockProvider {
|
|
113
113
|
constructor(private mockImplementation: Record<string, any>) {
|
|
114
114
|
// Copy all mock methods to the provider instance
|
|
115
115
|
for (const [key, value] of Object.entries(mockImplementation)) {
|
|
116
|
-
if (typeof value === 'function' &&
|
|
116
|
+
if (typeof value === 'function' && vi.isMockFunction && vi.isMockFunction(value)) {
|
|
117
117
|
(this as any)[key] = value;
|
|
118
118
|
} else {
|
|
119
|
-
// Wrap non-mock functions with
|
|
120
|
-
(this as any)[key] = typeof value === 'function' ?
|
|
119
|
+
// Wrap non-mock functions with vi.fn()
|
|
120
|
+
(this as any)[key] = typeof value === 'function' ? vi.fn(value) : value;
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
}
|
|
@@ -138,7 +138,7 @@ export class SpyProvider {
|
|
|
138
138
|
) {
|
|
139
139
|
for (const method of methods) {
|
|
140
140
|
if (typeof this.original[method] === 'function') {
|
|
141
|
-
const spy =
|
|
141
|
+
const spy = vi.spyOn(this.original, method);
|
|
142
142
|
this.spies.set(method, spy);
|
|
143
143
|
|
|
144
144
|
// Wrap to track calls manually too
|
|
@@ -183,7 +183,7 @@ export class SpyProvider {
|
|
|
183
183
|
export class StubProvider {
|
|
184
184
|
constructor(private stubs: Record<string, any>) {
|
|
185
185
|
for (const [key, value] of Object.entries(stubs)) {
|
|
186
|
-
(this as any)[key] =
|
|
186
|
+
(this as any)[key] = vi.fn().mockReturnValue(value);
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
}
|
|
@@ -140,8 +140,8 @@ export class NeverCalledExpectation implements SpyExpectation {
|
|
|
140
140
|
/**
|
|
141
141
|
* Test container with testing utilities
|
|
142
142
|
*/
|
|
143
|
-
//
|
|
144
|
-
declare const
|
|
143
|
+
// Vitest types workaround for build
|
|
144
|
+
declare const vi: any;
|
|
145
145
|
|
|
146
146
|
export class TestContainer extends Container {
|
|
147
147
|
private mocks = new Map<InjectionToken<any>, any>();
|
|
@@ -236,7 +236,7 @@ export class TestContainer extends Container {
|
|
|
236
236
|
for (const key in mock) {
|
|
237
237
|
if (typeof mock[key] === 'function') {
|
|
238
238
|
const original = mock[key];
|
|
239
|
-
mock[key] =
|
|
239
|
+
mock[key] = vi.fn(original);
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
|
|
@@ -257,7 +257,7 @@ export class TestContainer extends Container {
|
|
|
257
257
|
for (const key in wrapped) {
|
|
258
258
|
if (typeof wrapped[key] === 'function') {
|
|
259
259
|
const originalFn = wrapped[key];
|
|
260
|
-
wrapped[key] =
|
|
260
|
+
wrapped[key] = vi.fn((...args: any[]) => {
|
|
261
261
|
try {
|
|
262
262
|
const result = originalFn(...args);
|
|
263
263
|
this.recordInteraction(token, {
|
|
@@ -288,7 +288,7 @@ export class TestContainer extends Container {
|
|
|
288
288
|
|
|
289
289
|
for (const key in mock as any) {
|
|
290
290
|
const value = (mock as any)[key];
|
|
291
|
-
if (typeof value === 'function' &&
|
|
291
|
+
if (typeof value === 'function' && vi.isMockFunction(value)) {
|
|
292
292
|
spyMap.set(key, value);
|
|
293
293
|
}
|
|
294
294
|
}
|
|
@@ -306,7 +306,7 @@ export class TestContainer extends Container {
|
|
|
306
306
|
throw new Error(`Cannot spy on ${String(method)} of ${String(token)}`);
|
|
307
307
|
}
|
|
308
308
|
|
|
309
|
-
const spy =
|
|
309
|
+
const spy = vi.spyOn(instance as any, method as string);
|
|
310
310
|
|
|
311
311
|
let spyMap = this.spies.get(token);
|
|
312
312
|
if (!spyMap) {
|
|
@@ -334,7 +334,7 @@ export class TestContainer extends Container {
|
|
|
334
334
|
stubObject[key] = value;
|
|
335
335
|
} else {
|
|
336
336
|
// Otherwise, create a function that returns the value
|
|
337
|
-
stubObject[key] =
|
|
337
|
+
stubObject[key] = vi.fn(() => value);
|
|
338
338
|
}
|
|
339
339
|
}
|
|
340
340
|
|
|
@@ -494,7 +494,7 @@ export class TestContainer extends Container {
|
|
|
494
494
|
if (mock && typeof mock === 'object') {
|
|
495
495
|
for (const key in mock) {
|
|
496
496
|
const value = mock[key];
|
|
497
|
-
if (value &&
|
|
497
|
+
if (value && vi.isMockFunction && vi.isMockFunction(value)) {
|
|
498
498
|
value.mockReset();
|
|
499
499
|
}
|
|
500
500
|
}
|
|
@@ -685,7 +685,7 @@ export class TestContainer extends Container {
|
|
|
685
685
|
this.resolvedTokens.add(token);
|
|
686
686
|
}
|
|
687
687
|
return result;
|
|
688
|
-
} catch (
|
|
688
|
+
} catch (_originalError) {
|
|
689
689
|
// Continue to auto-mock if enabled
|
|
690
690
|
}
|
|
691
691
|
}
|
|
@@ -705,7 +705,7 @@ export class TestContainer extends Container {
|
|
|
705
705
|
* Create automatic mock
|
|
706
706
|
*/
|
|
707
707
|
private createAutoMock<T>(token: InjectionToken<any>): T {
|
|
708
|
-
const
|
|
708
|
+
const _name =
|
|
709
709
|
typeof token === 'string'
|
|
710
710
|
? token
|
|
711
711
|
: typeof token === 'symbol'
|
|
@@ -717,7 +717,7 @@ export class TestContainer extends Container {
|
|
|
717
717
|
get: (target, prop) => {
|
|
718
718
|
if (typeof prop === 'string') {
|
|
719
719
|
// Return a mock function for methods
|
|
720
|
-
return
|
|
720
|
+
return vi.fn().mockImplementation((...args: any[]) => {
|
|
721
721
|
this.recordInteraction(token, {
|
|
722
722
|
method: prop,
|
|
723
723
|
args,
|
|
@@ -792,7 +792,7 @@ export class TestContainer extends Container {
|
|
|
792
792
|
stubObject[key] = value;
|
|
793
793
|
} else {
|
|
794
794
|
// Create a function that returns the stubbed value
|
|
795
|
-
stubObject[key] =
|
|
795
|
+
stubObject[key] = vi.fn().mockReturnValue(value);
|
|
796
796
|
}
|
|
797
797
|
}
|
|
798
798
|
|
|
@@ -16,8 +16,9 @@ import {
|
|
|
16
16
|
* Test utility functions for Nexus testing
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
//
|
|
20
|
-
|
|
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;
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* Test mock configuration
|
|
@@ -53,6 +54,7 @@ export interface TestModuleConfig {
|
|
|
53
54
|
* Test harness configuration
|
|
54
55
|
*/
|
|
55
56
|
export interface TestHarnessConfig {
|
|
57
|
+
/** NOT READ — the harness never resolves or mounts this. */
|
|
56
58
|
component?: Constructor<any>;
|
|
57
59
|
providers?: Array<[InjectionToken<any>, ProviderDefinition<any>] | TestProvider<any>>;
|
|
58
60
|
zone?: {
|
|
@@ -190,7 +192,7 @@ export class IsolatedContainer extends Container {
|
|
|
190
192
|
try {
|
|
191
193
|
const value = source.resolve(token);
|
|
192
194
|
isolated.register(token, { useValue: value });
|
|
193
|
-
} catch (
|
|
195
|
+
} catch (_error) {
|
|
194
196
|
// Token not available in source
|
|
195
197
|
}
|
|
196
198
|
}
|
|
@@ -422,11 +424,11 @@ class RejectionExpectationImpl implements RejectionExpectation {
|
|
|
422
424
|
throw new Error('Expected resolution to throw');
|
|
423
425
|
} catch (error) {
|
|
424
426
|
if (this.expectedErrorType && !(error instanceof this.expectedErrorType)) {
|
|
425
|
-
throw new Error(`Expected ${this.expectedErrorType.name}, got ${(error as Error).constructor.name}
|
|
427
|
+
throw new Error(`Expected ${this.expectedErrorType.name}, got ${(error as Error).constructor.name}`, { cause: error });
|
|
426
428
|
}
|
|
427
429
|
|
|
428
430
|
if (this.expectedMessage && !(error as Error).message.includes(this.expectedMessage)) {
|
|
429
|
-
throw new Error(`Expected error message to contain "${this.expectedMessage}"
|
|
431
|
+
throw new Error(`Expected error message to contain "${this.expectedMessage}"`, { cause: error });
|
|
430
432
|
}
|
|
431
433
|
}
|
|
432
434
|
}
|
|
@@ -456,8 +458,8 @@ class DependencyExpectationImpl implements DependencyExpectation {
|
|
|
456
458
|
if (this.expectedToken) {
|
|
457
459
|
try {
|
|
458
460
|
this.container.resolve(this.expectedToken);
|
|
459
|
-
} catch (
|
|
460
|
-
throw new Error(`Dependency ${String(this.expectedToken)} not found
|
|
461
|
+
} catch (_error) {
|
|
462
|
+
throw new Error(`Dependency ${String(this.expectedToken)} not found`, { cause: _error });
|
|
461
463
|
}
|
|
462
464
|
}
|
|
463
465
|
}
|
|
@@ -597,18 +599,18 @@ export async function assertThrows<E extends Error>(
|
|
|
597
599
|
throw new Error('Expected function to throw');
|
|
598
600
|
} catch (error) {
|
|
599
601
|
if (errorType && !(error instanceof errorType)) {
|
|
600
|
-
throw new Error(`Expected error of type ${errorType.name}, got ${(error as Error).constructor.name}
|
|
602
|
+
throw new Error(`Expected error of type ${errorType.name}, got ${(error as Error).constructor.name}`, { cause: error });
|
|
601
603
|
}
|
|
602
604
|
|
|
603
605
|
if (message) {
|
|
604
606
|
const errorMessage = (error as Error).message;
|
|
605
607
|
if (typeof message === 'string') {
|
|
606
608
|
if (!errorMessage.includes(message)) {
|
|
607
|
-
throw new Error(`Expected error message to include "${message}", got "${errorMessage}"
|
|
609
|
+
throw new Error(`Expected error message to include "${message}", got "${errorMessage}"`, { cause: error });
|
|
608
610
|
}
|
|
609
611
|
} else if (message instanceof RegExp) {
|
|
610
612
|
if (!message.test(errorMessage)) {
|
|
611
|
-
throw new Error(`Expected error message to match ${message}, got "${errorMessage}"
|
|
613
|
+
throw new Error(`Expected error message to match ${message}, got "${errorMessage}"`, { cause: error });
|
|
612
614
|
}
|
|
613
615
|
}
|
|
614
616
|
}
|
|
@@ -636,7 +638,7 @@ export function verifyContainerState(
|
|
|
636
638
|
spies?: Array<{ token: Token<any>; method: string }>;
|
|
637
639
|
}
|
|
638
640
|
): void {
|
|
639
|
-
const
|
|
641
|
+
const _metadata = container.getTestMetadata();
|
|
640
642
|
|
|
641
643
|
if (expectations.registeredTokens) {
|
|
642
644
|
for (const token of expectations.registeredTokens) {
|