@geekmidas/testkit 0.0.9 → 0.0.11
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/Factory-B9LP1L18.d.cts +131 -0
- package/dist/Factory-DiZSNxC0.d.mts +131 -0
- package/dist/Factory.d.cts +3 -0
- package/dist/Factory.d.mts +3 -0
- package/dist/KyselyFactory-B-zlLbov.d.mts +200 -0
- package/dist/KyselyFactory-CrLWxJW4.d.cts +200 -0
- package/dist/KyselyFactory.d.cts +4 -0
- package/dist/KyselyFactory.d.mts +4 -0
- package/dist/ObjectionFactory-CJCpvwts.d.mts +213 -0
- package/dist/{ObjectionFactory-C47B03Ot.cjs → ObjectionFactory-Wq80ypMM.cjs} +82 -40
- package/dist/{ObjectionFactory-89p-FFEw.mjs → ObjectionFactory-aqM0dDW7.mjs} +82 -40
- package/dist/ObjectionFactory-vME-wNlq.d.cts +213 -0
- package/dist/ObjectionFactory.cjs +2 -1
- package/dist/ObjectionFactory.d.cts +4 -0
- package/dist/ObjectionFactory.d.mts +4 -0
- package/dist/ObjectionFactory.mjs +2 -1
- package/dist/{PostgresKyselyMigrator-ChIpZFYB.mjs → PostgresKyselyMigrator-Bdhl251C.mjs} +1 -1
- package/dist/PostgresKyselyMigrator-CQ3aUoy_.d.cts +67 -0
- package/dist/{PostgresKyselyMigrator-Bs31emFd.cjs → PostgresKyselyMigrator-CfytARcA.cjs} +1 -1
- package/dist/PostgresKyselyMigrator-_6yHZigp.d.mts +67 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.d.cts +3 -0
- package/dist/PostgresKyselyMigrator.d.mts +3 -0
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-BlvuQl7d.d.mts +84 -0
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +84 -0
- package/dist/{PostgresMigrator-BtAWdLss.cjs → PostgresMigrator-DFcNdCvD.cjs} +2 -1
- package/dist/{PostgresMigrator-BzqksJcW.mjs → PostgresMigrator-DxPC_gGu.mjs} +3 -2
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.d.cts +2 -0
- package/dist/PostgresMigrator.d.mts +2 -0
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/{PostgresObjectionMigrator-B88aTT0m.cjs → PostgresObjectionMigrator-BG6ymgnt.cjs} +1 -1
- package/dist/PostgresObjectionMigrator-C69n7vzr.d.mts +77 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +77 -0
- package/dist/{PostgresObjectionMigrator-DydSgYFv.mjs → PostgresObjectionMigrator-G4h5FLvU.mjs} +1 -1
- package/dist/PostgresObjectionMigrator.cjs +2 -2
- package/dist/PostgresObjectionMigrator.d.cts +3 -0
- package/dist/PostgresObjectionMigrator.d.mts +3 -0
- package/dist/PostgresObjectionMigrator.mjs +2 -2
- package/dist/VitestKyselyTransactionIsolator-ClCazkBO.d.mts +56 -0
- package/dist/VitestKyselyTransactionIsolator-UE1J-UoP.d.cts +56 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +3 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +3 -0
- package/dist/VitestObjectionTransactionIsolator-CO2nTi9r.d.cts +57 -0
- package/dist/VitestObjectionTransactionIsolator-D264iuPy.d.mts +57 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +3 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +3 -0
- package/dist/VitestTransactionIsolator-DHf2MxmC.d.cts +118 -0
- package/dist/VitestTransactionIsolator-Xqyjlmw6.d.mts +118 -0
- package/dist/VitestTransactionIsolator.d.cts +2 -0
- package/dist/VitestTransactionIsolator.d.mts +2 -0
- package/dist/__tests__/Factory.spec.d.cts +1 -0
- package/dist/__tests__/Factory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +2 -2
- package/dist/__tests__/KyselyFactory.spec.d.cts +1 -0
- package/dist/__tests__/KyselyFactory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.mjs +2 -2
- package/dist/__tests__/ObjectionFactory.spec.cjs +117 -1
- package/dist/__tests__/ObjectionFactory.spec.d.cts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.d.mts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +117 -1
- package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +2 -2
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +2 -2
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +4 -3
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +4 -3
- package/dist/__tests__/faker.spec.d.cts +1 -0
- package/dist/__tests__/faker.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.cjs +2 -2
- package/dist/__tests__/integration.spec.d.cts +1 -0
- package/dist/__tests__/integration.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.mjs +2 -2
- package/dist/example.d.cts +26 -0
- package/dist/example.d.mts +26 -0
- package/dist/faker-DAiFK3T3.d.cts +155 -0
- package/dist/faker-nN9Ki6fn.d.mts +155 -0
- package/dist/faker.d.cts +2 -0
- package/dist/faker.d.mts +2 -0
- package/dist/helpers.d.cts +41 -0
- package/dist/helpers.d.mts +41 -0
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +73 -0
- package/dist/kysely.d.mts +73 -0
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +4 -3
- package/dist/objection.d.cts +88 -0
- package/dist/objection.d.mts +88 -0
- package/dist/objection.mjs +4 -3
- package/package.json +5 -5
- package/src/ObjectionFactory.ts +156 -13
- package/src/PostgresMigrator.ts +3 -1
- package/src/__tests__/ObjectionFactory.spec.ts +162 -0
- package/PostgresKyselyMigrator.spec +0 -471
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-BlvuQl7d.mjs";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresObjectionMigrator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL migrator implementation for Objection.js ORM with Knex.
|
|
8
|
+
* Extends PostgresMigrator to provide Knex-specific migration functionality.
|
|
9
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import knex from 'knex';
|
|
14
|
+
* import { PostgresObjectionMigrator } from '@geekmidas/testkit';
|
|
15
|
+
*
|
|
16
|
+
* // Create Knex instance
|
|
17
|
+
* const db = knex({
|
|
18
|
+
* client: 'pg',
|
|
19
|
+
* connection: uri,
|
|
20
|
+
* migrations: {
|
|
21
|
+
* directory: path.join(__dirname, 'migrations'),
|
|
22
|
+
* extension: 'ts'
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Create and use migrator
|
|
27
|
+
* const migrator = new PostgresObjectionMigrator({
|
|
28
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
29
|
+
* knex: db
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const cleanup = await migrator.start();
|
|
33
|
+
* // Run tests...
|
|
34
|
+
* await cleanup();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare class PostgresObjectionMigrator extends PostgresMigrator {
|
|
38
|
+
private options;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new PostgresObjectionMigrator instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Configuration options
|
|
43
|
+
* @param options.uri - PostgreSQL connection URI
|
|
44
|
+
* @param options.knex - Knex database instance configured with migrations
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: {
|
|
47
|
+
uri: string;
|
|
48
|
+
knex: Knex;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Executes Knex migrations to the latest version.
|
|
52
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
53
|
+
*
|
|
54
|
+
* @throws Error if migrations fail to apply
|
|
55
|
+
* @returns Promise that resolves when all migrations are applied
|
|
56
|
+
*/
|
|
57
|
+
migrate(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Rolls back the last batch of migrations.
|
|
60
|
+
* Useful for testing migration rollback scenarios.
|
|
61
|
+
*
|
|
62
|
+
* @returns Promise that resolves when rollback is complete
|
|
63
|
+
*/
|
|
64
|
+
rollback(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Gets the current migration status.
|
|
67
|
+
* Returns information about completed and pending migrations.
|
|
68
|
+
*
|
|
69
|
+
* @returns Promise with migration status information
|
|
70
|
+
*/
|
|
71
|
+
status(): Promise<{
|
|
72
|
+
completed: string[];
|
|
73
|
+
pending: string[];
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { PostgresObjectionMigrator };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-D5UkK1_K.cjs";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresObjectionMigrator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL migrator implementation for Objection.js ORM with Knex.
|
|
8
|
+
* Extends PostgresMigrator to provide Knex-specific migration functionality.
|
|
9
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import knex from 'knex';
|
|
14
|
+
* import { PostgresObjectionMigrator } from '@geekmidas/testkit';
|
|
15
|
+
*
|
|
16
|
+
* // Create Knex instance
|
|
17
|
+
* const db = knex({
|
|
18
|
+
* client: 'pg',
|
|
19
|
+
* connection: uri,
|
|
20
|
+
* migrations: {
|
|
21
|
+
* directory: path.join(__dirname, 'migrations'),
|
|
22
|
+
* extension: 'ts'
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Create and use migrator
|
|
27
|
+
* const migrator = new PostgresObjectionMigrator({
|
|
28
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
29
|
+
* knex: db
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const cleanup = await migrator.start();
|
|
33
|
+
* // Run tests...
|
|
34
|
+
* await cleanup();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare class PostgresObjectionMigrator extends PostgresMigrator {
|
|
38
|
+
private options;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new PostgresObjectionMigrator instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Configuration options
|
|
43
|
+
* @param options.uri - PostgreSQL connection URI
|
|
44
|
+
* @param options.knex - Knex database instance configured with migrations
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: {
|
|
47
|
+
uri: string;
|
|
48
|
+
knex: Knex;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Executes Knex migrations to the latest version.
|
|
52
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
53
|
+
*
|
|
54
|
+
* @throws Error if migrations fail to apply
|
|
55
|
+
* @returns Promise that resolves when all migrations are applied
|
|
56
|
+
*/
|
|
57
|
+
migrate(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Rolls back the last batch of migrations.
|
|
60
|
+
* Useful for testing migration rollback scenarios.
|
|
61
|
+
*
|
|
62
|
+
* @returns Promise that resolves when rollback is complete
|
|
63
|
+
*/
|
|
64
|
+
rollback(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Gets the current migration status.
|
|
67
|
+
* Returns information about completed and pending migrations.
|
|
68
|
+
*
|
|
69
|
+
* @returns Promise with migration status information
|
|
70
|
+
*/
|
|
71
|
+
status(): Promise<{
|
|
72
|
+
completed: string[];
|
|
73
|
+
pending: string[];
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { PostgresObjectionMigrator };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('./PostgresMigrator-
|
|
2
|
-
const require_PostgresObjectionMigrator = require('./PostgresObjectionMigrator-
|
|
1
|
+
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
2
|
+
const require_PostgresObjectionMigrator = require('./PostgresObjectionMigrator-BG6ymgnt.cjs');
|
|
3
3
|
|
|
4
4
|
exports.PostgresObjectionMigrator = require_PostgresObjectionMigrator.PostgresObjectionMigrator;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./PostgresMigrator-
|
|
2
|
-
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-
|
|
1
|
+
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
2
|
+
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-G4h5FLvU.mjs";
|
|
3
3
|
|
|
4
4
|
export { PostgresObjectionMigrator };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
|
2
|
+
import { Kysely, Transaction } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/VitestKyselyTransactionIsolator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Kysely-specific implementation of the Vitest transaction isolator.
|
|
8
|
+
* Provides automatic transaction rollback for test isolation using Kysely's transaction API.
|
|
9
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
10
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
11
|
+
*
|
|
12
|
+
* @template Database - The database schema type
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';
|
|
17
|
+
* import { db } from './database';
|
|
18
|
+
*
|
|
19
|
+
* // Create isolator instance
|
|
20
|
+
* const isolator = new VitestKyselyTransactionIsolator<Database>();
|
|
21
|
+
*
|
|
22
|
+
* // In your test setup
|
|
23
|
+
* beforeEach(async () => {
|
|
24
|
+
* await isolator.start(db);
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* afterEach(async () => {
|
|
28
|
+
* await isolator.rollback();
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Tests run in isolated transactions
|
|
32
|
+
* it('should create user', async () => {
|
|
33
|
+
* const user = await db.insertInto('users')
|
|
34
|
+
* .values({ name: 'Test User' })
|
|
35
|
+
* .returningAll()
|
|
36
|
+
* .executeTakeFirst();
|
|
37
|
+
*
|
|
38
|
+
* expect(user).toBeDefined();
|
|
39
|
+
* // This data will be rolled back after the test
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare class VitestKyselyTransactionIsolator<Database> extends VitestPostgresTransactionIsolator<Kysely<Database>, Transaction<Database>> {
|
|
44
|
+
/**
|
|
45
|
+
* Creates a Kysely transaction with the specified isolation level.
|
|
46
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
47
|
+
*
|
|
48
|
+
* @param conn - The Kysely database connection
|
|
49
|
+
* @param level - The transaction isolation level
|
|
50
|
+
* @param fn - The function to execute within the transaction
|
|
51
|
+
* @returns Promise that resolves when the transaction completes
|
|
52
|
+
*/
|
|
53
|
+
transact(conn: Kysely<Database>, level: IsolationLevel, fn: (trx: Transaction<Database>) => Promise<void>): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { VitestKyselyTransactionIsolator };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
|
2
|
+
import { Kysely, Transaction } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/VitestKyselyTransactionIsolator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Kysely-specific implementation of the Vitest transaction isolator.
|
|
8
|
+
* Provides automatic transaction rollback for test isolation using Kysely's transaction API.
|
|
9
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
10
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
11
|
+
*
|
|
12
|
+
* @template Database - The database schema type
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';
|
|
17
|
+
* import { db } from './database';
|
|
18
|
+
*
|
|
19
|
+
* // Create isolator instance
|
|
20
|
+
* const isolator = new VitestKyselyTransactionIsolator<Database>();
|
|
21
|
+
*
|
|
22
|
+
* // In your test setup
|
|
23
|
+
* beforeEach(async () => {
|
|
24
|
+
* await isolator.start(db);
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* afterEach(async () => {
|
|
28
|
+
* await isolator.rollback();
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Tests run in isolated transactions
|
|
32
|
+
* it('should create user', async () => {
|
|
33
|
+
* const user = await db.insertInto('users')
|
|
34
|
+
* .values({ name: 'Test User' })
|
|
35
|
+
* .returningAll()
|
|
36
|
+
* .executeTakeFirst();
|
|
37
|
+
*
|
|
38
|
+
* expect(user).toBeDefined();
|
|
39
|
+
* // This data will be rolled back after the test
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare class VitestKyselyTransactionIsolator<Database> extends VitestPostgresTransactionIsolator<Kysely<Database>, Transaction<Database>> {
|
|
44
|
+
/**
|
|
45
|
+
* Creates a Kysely transaction with the specified isolation level.
|
|
46
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
47
|
+
*
|
|
48
|
+
* @param conn - The Kysely database connection
|
|
49
|
+
* @param level - The transaction isolation level
|
|
50
|
+
* @param fn - The function to execute within the transaction
|
|
51
|
+
* @returns Promise that resolves when the transaction completes
|
|
52
|
+
*/
|
|
53
|
+
transact(conn: Kysely<Database>, level: IsolationLevel, fn: (trx: Transaction<Database>) => Promise<void>): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { VitestKyselyTransactionIsolator };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/VitestObjectionTransactionIsolator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Objection.js-specific implementation of the Vitest transaction isolator.
|
|
8
|
+
* Provides automatic transaction rollback for test isolation using Objection.js and Knex transaction API.
|
|
9
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
10
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { VitestObjectionTransactionIsolator } from '@geekmidas/testkit';
|
|
15
|
+
* import { knex } from './database';
|
|
16
|
+
* import { User } from './models';
|
|
17
|
+
* import { test } from 'vitest';
|
|
18
|
+
*
|
|
19
|
+
* // Create isolator instance
|
|
20
|
+
* const isolator = new VitestObjectionTransactionIsolator(test);
|
|
21
|
+
*
|
|
22
|
+
* // Use with wrapped test API
|
|
23
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(knex);
|
|
24
|
+
*
|
|
25
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
26
|
+
* const user = await User.query(trx)
|
|
27
|
+
* .insert({ name: 'Test User' });
|
|
28
|
+
*
|
|
29
|
+
* expect(user).toBeDefined();
|
|
30
|
+
* // This data will be rolled back after the test
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare class VitestObjectionTransactionIsolator extends VitestPostgresTransactionIsolator<Knex, Knex.Transaction> {
|
|
35
|
+
/**
|
|
36
|
+
* Creates a Knex transaction with the specified isolation level.
|
|
37
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
38
|
+
* This transaction can be used with Objection.js models via Model.query(trx).
|
|
39
|
+
*
|
|
40
|
+
* @param conn - The Knex database connection
|
|
41
|
+
* @param level - The transaction isolation level
|
|
42
|
+
* @param fn - The function to execute within the transaction
|
|
43
|
+
* @returns Promise that resolves when the transaction completes
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* await isolator.transact(knex, IsolationLevel.REPEATABLE_READ, async (trx) => {
|
|
48
|
+
* // Use transaction with Objection models
|
|
49
|
+
* await User.query(trx).insert({ name: 'Test' });
|
|
50
|
+
* await Post.query(trx).where('userId', user.id).delete();
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
transact(conn: Knex, level: IsolationLevel, fn: (trx: Knex.Transaction) => Promise<void>): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
export { VitestObjectionTransactionIsolator };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/VitestObjectionTransactionIsolator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Objection.js-specific implementation of the Vitest transaction isolator.
|
|
8
|
+
* Provides automatic transaction rollback for test isolation using Objection.js and Knex transaction API.
|
|
9
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
10
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { VitestObjectionTransactionIsolator } from '@geekmidas/testkit';
|
|
15
|
+
* import { knex } from './database';
|
|
16
|
+
* import { User } from './models';
|
|
17
|
+
* import { test } from 'vitest';
|
|
18
|
+
*
|
|
19
|
+
* // Create isolator instance
|
|
20
|
+
* const isolator = new VitestObjectionTransactionIsolator(test);
|
|
21
|
+
*
|
|
22
|
+
* // Use with wrapped test API
|
|
23
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(knex);
|
|
24
|
+
*
|
|
25
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
26
|
+
* const user = await User.query(trx)
|
|
27
|
+
* .insert({ name: 'Test User' });
|
|
28
|
+
*
|
|
29
|
+
* expect(user).toBeDefined();
|
|
30
|
+
* // This data will be rolled back after the test
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare class VitestObjectionTransactionIsolator extends VitestPostgresTransactionIsolator<Knex, Knex.Transaction> {
|
|
35
|
+
/**
|
|
36
|
+
* Creates a Knex transaction with the specified isolation level.
|
|
37
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
38
|
+
* This transaction can be used with Objection.js models via Model.query(trx).
|
|
39
|
+
*
|
|
40
|
+
* @param conn - The Knex database connection
|
|
41
|
+
* @param level - The transaction isolation level
|
|
42
|
+
* @param fn - The function to execute within the transaction
|
|
43
|
+
* @returns Promise that resolves when the transaction completes
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* await isolator.transact(knex, IsolationLevel.REPEATABLE_READ, async (trx) => {
|
|
48
|
+
* // Use transaction with Objection models
|
|
49
|
+
* await User.query(trx).insert({ name: 'Test' });
|
|
50
|
+
* await Post.query(trx).where('userId', user.id).delete();
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
transact(conn: Knex, level: IsolationLevel, fn: (trx: Knex.Transaction) => Promise<void>): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
export { VitestObjectionTransactionIsolator };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { TestAPI } from "vitest";
|
|
2
|
+
|
|
3
|
+
//#region src/VitestTransactionIsolator.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Type definition for test fixtures that provide transaction access.
|
|
7
|
+
* Used with Vitest's test.extend() API to inject transactions into tests.
|
|
8
|
+
*
|
|
9
|
+
* @template Transaction - The transaction type specific to the database driver
|
|
10
|
+
*/
|
|
11
|
+
interface DatabaseFixtures<Transaction> {
|
|
12
|
+
/**
|
|
13
|
+
* The database transaction available to the test.
|
|
14
|
+
* All database operations should use this transaction to ensure proper rollback.
|
|
15
|
+
*/
|
|
16
|
+
trx: Transaction;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* PostgreSQL transaction isolation levels.
|
|
20
|
+
* Controls the visibility of concurrent transactions.
|
|
21
|
+
*
|
|
22
|
+
* @see https://www.postgresql.org/docs/current/transaction-iso.html
|
|
23
|
+
*/
|
|
24
|
+
declare enum IsolationLevel {
|
|
25
|
+
/**
|
|
26
|
+
* Lowest isolation level. Allows dirty reads.
|
|
27
|
+
* Not recommended for testing.
|
|
28
|
+
*/
|
|
29
|
+
READ_UNCOMMITTED = "READ UNCOMMITTED",
|
|
30
|
+
/**
|
|
31
|
+
* Default PostgreSQL isolation level.
|
|
32
|
+
* Prevents dirty reads but allows non-repeatable reads.
|
|
33
|
+
*/
|
|
34
|
+
READ_COMMITTED = "READ COMMITTED",
|
|
35
|
+
/**
|
|
36
|
+
* Prevents dirty reads and non-repeatable reads.
|
|
37
|
+
* Recommended for most test scenarios.
|
|
38
|
+
*/
|
|
39
|
+
REPEATABLE_READ = "REPEATABLE READ",
|
|
40
|
+
/**
|
|
41
|
+
* Highest isolation level. Prevents all phenomena.
|
|
42
|
+
* May cause performance overhead in tests.
|
|
43
|
+
*/
|
|
44
|
+
SERIALIZABLE = "SERIALIZABLE",
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Abstract base class for implementing database transaction isolation in Vitest tests.
|
|
48
|
+
* Provides automatic transaction rollback after each test to maintain test isolation.
|
|
49
|
+
* Subclasses must implement the transact() method for their specific database driver.
|
|
50
|
+
*
|
|
51
|
+
* @template Connection - The database connection type
|
|
52
|
+
* @template Transaction - The transaction type
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Implement for your database driver
|
|
57
|
+
* class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
|
|
58
|
+
* async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
|
|
59
|
+
* await conn.transaction(level, fn);
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* // Use in tests
|
|
64
|
+
* const isolator = new MyDatabaseIsolator(test);
|
|
65
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db);
|
|
66
|
+
*
|
|
67
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
68
|
+
* await trx.insert('users', { name: 'Test' });
|
|
69
|
+
* // Data is automatically rolled back after test
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare abstract class VitestPostgresTransactionIsolator<Connection, Transaction> {
|
|
74
|
+
private readonly api;
|
|
75
|
+
/**
|
|
76
|
+
* Abstract method to create a transaction with the specified isolation level.
|
|
77
|
+
* Must be implemented by subclasses for specific database drivers.
|
|
78
|
+
*
|
|
79
|
+
* @param conn - The database connection
|
|
80
|
+
* @param isolationLevel - The transaction isolation level
|
|
81
|
+
* @param fn - The function to execute within the transaction
|
|
82
|
+
* @returns Promise that resolves when the transaction completes
|
|
83
|
+
*/
|
|
84
|
+
abstract transact(conn: Connection, isolationLevel: IsolationLevel, fn: (trx: Transaction) => Promise<void>): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a new VitestPostgresTransactionIsolator instance.
|
|
87
|
+
*
|
|
88
|
+
* @param api - The Vitest test API (usually the `test` export from vitest)
|
|
89
|
+
*/
|
|
90
|
+
constructor(api: TestAPI);
|
|
91
|
+
/**
|
|
92
|
+
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
93
|
+
* Each test will run within a database transaction that is automatically rolled back.
|
|
94
|
+
*
|
|
95
|
+
* @param conn - The database connection to use
|
|
96
|
+
* @param setup - Optional setup function to run within the transaction before each test
|
|
97
|
+
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
98
|
+
* @returns A wrapped test API with transaction support
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db, async (trx) => {
|
|
103
|
+
* // Optional setup: create common test data
|
|
104
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
108
|
+
* const user = await trx.insert('users', { name: 'Test' });
|
|
109
|
+
* expect(user).toBeDefined();
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
wrapVitestWithTransaction(conn: Connection, setup?: (trx: Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
114
|
+
trx: Transaction;
|
|
115
|
+
}>;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { DatabaseFixtures, IsolationLevel, VitestPostgresTransactionIsolator };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { TestAPI } from "vitest";
|
|
2
|
+
|
|
3
|
+
//#region src/VitestTransactionIsolator.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Type definition for test fixtures that provide transaction access.
|
|
7
|
+
* Used with Vitest's test.extend() API to inject transactions into tests.
|
|
8
|
+
*
|
|
9
|
+
* @template Transaction - The transaction type specific to the database driver
|
|
10
|
+
*/
|
|
11
|
+
interface DatabaseFixtures<Transaction> {
|
|
12
|
+
/**
|
|
13
|
+
* The database transaction available to the test.
|
|
14
|
+
* All database operations should use this transaction to ensure proper rollback.
|
|
15
|
+
*/
|
|
16
|
+
trx: Transaction;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* PostgreSQL transaction isolation levels.
|
|
20
|
+
* Controls the visibility of concurrent transactions.
|
|
21
|
+
*
|
|
22
|
+
* @see https://www.postgresql.org/docs/current/transaction-iso.html
|
|
23
|
+
*/
|
|
24
|
+
declare enum IsolationLevel {
|
|
25
|
+
/**
|
|
26
|
+
* Lowest isolation level. Allows dirty reads.
|
|
27
|
+
* Not recommended for testing.
|
|
28
|
+
*/
|
|
29
|
+
READ_UNCOMMITTED = "READ UNCOMMITTED",
|
|
30
|
+
/**
|
|
31
|
+
* Default PostgreSQL isolation level.
|
|
32
|
+
* Prevents dirty reads but allows non-repeatable reads.
|
|
33
|
+
*/
|
|
34
|
+
READ_COMMITTED = "READ COMMITTED",
|
|
35
|
+
/**
|
|
36
|
+
* Prevents dirty reads and non-repeatable reads.
|
|
37
|
+
* Recommended for most test scenarios.
|
|
38
|
+
*/
|
|
39
|
+
REPEATABLE_READ = "REPEATABLE READ",
|
|
40
|
+
/**
|
|
41
|
+
* Highest isolation level. Prevents all phenomena.
|
|
42
|
+
* May cause performance overhead in tests.
|
|
43
|
+
*/
|
|
44
|
+
SERIALIZABLE = "SERIALIZABLE",
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Abstract base class for implementing database transaction isolation in Vitest tests.
|
|
48
|
+
* Provides automatic transaction rollback after each test to maintain test isolation.
|
|
49
|
+
* Subclasses must implement the transact() method for their specific database driver.
|
|
50
|
+
*
|
|
51
|
+
* @template Connection - The database connection type
|
|
52
|
+
* @template Transaction - The transaction type
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Implement for your database driver
|
|
57
|
+
* class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
|
|
58
|
+
* async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
|
|
59
|
+
* await conn.transaction(level, fn);
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* // Use in tests
|
|
64
|
+
* const isolator = new MyDatabaseIsolator(test);
|
|
65
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db);
|
|
66
|
+
*
|
|
67
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
68
|
+
* await trx.insert('users', { name: 'Test' });
|
|
69
|
+
* // Data is automatically rolled back after test
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare abstract class VitestPostgresTransactionIsolator<Connection, Transaction> {
|
|
74
|
+
private readonly api;
|
|
75
|
+
/**
|
|
76
|
+
* Abstract method to create a transaction with the specified isolation level.
|
|
77
|
+
* Must be implemented by subclasses for specific database drivers.
|
|
78
|
+
*
|
|
79
|
+
* @param conn - The database connection
|
|
80
|
+
* @param isolationLevel - The transaction isolation level
|
|
81
|
+
* @param fn - The function to execute within the transaction
|
|
82
|
+
* @returns Promise that resolves when the transaction completes
|
|
83
|
+
*/
|
|
84
|
+
abstract transact(conn: Connection, isolationLevel: IsolationLevel, fn: (trx: Transaction) => Promise<void>): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a new VitestPostgresTransactionIsolator instance.
|
|
87
|
+
*
|
|
88
|
+
* @param api - The Vitest test API (usually the `test` export from vitest)
|
|
89
|
+
*/
|
|
90
|
+
constructor(api: TestAPI);
|
|
91
|
+
/**
|
|
92
|
+
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
93
|
+
* Each test will run within a database transaction that is automatically rolled back.
|
|
94
|
+
*
|
|
95
|
+
* @param conn - The database connection to use
|
|
96
|
+
* @param setup - Optional setup function to run within the transaction before each test
|
|
97
|
+
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
98
|
+
* @returns A wrapped test API with transaction support
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db, async (trx) => {
|
|
103
|
+
* // Optional setup: create common test data
|
|
104
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
108
|
+
* const user = await trx.insert('users', { name: 'Test' });
|
|
109
|
+
* expect(user).toBeDefined();
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
wrapVitestWithTransaction(conn: Connection, setup?: (trx: Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
114
|
+
trx: Transaction;
|
|
115
|
+
}>;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { DatabaseFixtures, IsolationLevel, VitestPostgresTransactionIsolator };
|