@geekmidas/testkit 0.0.5 → 0.0.7
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-WMhTNZ9S.cjs +56 -0
- package/dist/Factory-z2m01hMj.mjs +50 -0
- package/dist/Factory.cjs +1 -1
- package/dist/Factory.mjs +1 -1
- package/dist/KyselyFactory-Bdq1s1Go.cjs +215 -0
- package/dist/KyselyFactory-ELiHgHVv.mjs +210 -0
- package/dist/KyselyFactory.cjs +3 -3
- package/dist/KyselyFactory.mjs +3 -3
- package/dist/ObjectionFactory-89p-FFEw.mjs +178 -0
- package/dist/ObjectionFactory-C47B03Ot.cjs +183 -0
- package/dist/ObjectionFactory.cjs +2 -2
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/PostgresKyselyMigrator-Bs31emFd.cjs +87 -0
- package/dist/PostgresKyselyMigrator-ChIpZFYB.mjs +81 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-BtAWdLss.cjs +151 -0
- package/dist/PostgresMigrator-BzqksJcW.mjs +145 -0
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-AfxPJEwR.mjs +58 -0
- package/dist/VitestKyselyTransactionIsolator-YWnSJiIH.cjs +63 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/VitestObjectionTransactionIsolator-0uX6DW5G.cjs +66 -0
- package/dist/VitestObjectionTransactionIsolator-BZRYy8iW.mjs +61 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +4 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +4 -0
- package/dist/VitestTransactionIsolator-DcOz0LZF.cjs +129 -0
- package/dist/VitestTransactionIsolator-kFL36T8x.mjs +117 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/__tests__/Factory.spec.cjs +1 -1
- package/dist/__tests__/Factory.spec.mjs +1 -1
- package/dist/__tests__/KyselyFactory.spec.cjs +41 -40
- package/dist/__tests__/KyselyFactory.spec.mjs +42 -41
- package/dist/__tests__/ObjectionFactory.spec.cjs +3 -3
- package/dist/__tests__/ObjectionFactory.spec.mjs +3 -3
- package/dist/__tests__/PostgresMigrator.spec.cjs +2 -2
- package/dist/__tests__/PostgresMigrator.spec.mjs +2 -2
- package/dist/__tests__/faker.spec.cjs +1 -1
- package/dist/__tests__/faker.spec.mjs +1 -1
- package/dist/__tests__/integration.spec.cjs +11 -8
- package/dist/__tests__/integration.spec.mjs +12 -9
- package/dist/example.cjs +3 -3
- package/dist/example.mjs +3 -3
- package/dist/faker-CxKkEeYi.mjs +227 -0
- package/dist/faker-SMN4ira4.cjs +263 -0
- package/dist/faker.cjs +1 -1
- package/dist/faker.mjs +1 -1
- package/dist/helpers-CKMlwSYT.mjs +47 -0
- package/dist/helpers-H4hO5SZR.cjs +53 -0
- package/dist/helpers.cjs +2 -5
- package/dist/helpers.mjs +2 -4
- package/dist/kysely-B-GOhABm.cjs +72 -0
- package/dist/kysely-CqfoKVXs.mjs +67 -0
- package/dist/kysely.cjs +11 -15
- package/dist/kysely.mjs +9 -15
- package/dist/objection.cjs +86 -3
- package/dist/objection.mjs +83 -3
- package/package.json +2 -2
- package/src/Factory.ts +97 -0
- package/src/KyselyFactory.ts +180 -0
- package/src/ObjectionFactory.ts +145 -3
- package/src/PostgresKyselyMigrator.ts +54 -0
- package/src/PostgresMigrator.ts +90 -0
- package/src/VitestKyselyTransactionIsolator.ts +46 -0
- package/src/VitestObjectionTransactionIsolator.ts +73 -0
- package/src/VitestTransactionIsolator.ts +99 -2
- package/src/__tests__/KyselyFactory.spec.ts +4 -6
- package/src/__tests__/integration.spec.ts +8 -3
- package/src/faker.ts +158 -7
- package/src/helpers.ts +35 -18
- package/src/kysely.ts +66 -1
- package/src/objection.ts +95 -0
- package/dist/Factory-DREHoms3.cjs +0 -15
- package/dist/Factory-DlzMkMzb.mjs +0 -9
- package/dist/KyselyFactory-BX7Kv2uP.cjs +0 -65
- package/dist/KyselyFactory-pOMOFQWE.mjs +0 -60
- package/dist/ObjectionFactory-BlkzSEqo.cjs +0 -41
- package/dist/ObjectionFactory-ChuX8sZN.mjs +0 -36
- package/dist/PostgresKyselyMigrator-D8fm35-s.mjs +0 -27
- package/dist/PostgresKyselyMigrator-JTY2LfwD.cjs +0 -33
- package/dist/PostgresMigrator-Bz-tnjB6.cjs +0 -67
- package/dist/PostgresMigrator-CEoRKTdq.mjs +0 -61
- package/dist/VitestKyselyTransactionIsolator-BS3R-V0I.mjs +0 -12
- package/dist/VitestKyselyTransactionIsolator-DWSTKIe3.cjs +0 -17
- package/dist/VitestTransactionIsolator-BjVXqFs6.mjs +0 -40
- package/dist/VitestTransactionIsolator-Bx2c4OzK.cjs +0 -52
- package/dist/faker-BwaXA_RF.mjs +0 -85
- package/dist/faker-caz-8zt8.cjs +0 -121
- package/dist/helpers-B9Jdk_C7.cjs +0 -31
- package/dist/helpers-DOiGIkaU.mjs +0 -19
- /package/dist/{helpers-DKEBHABj.cjs → helpers-Bnm3Jy9X.cjs} +0 -0
- /package/dist/{helpers-BfuX-cjN.mjs → helpers-CukcFAU9.mjs} +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const pg = require_chunk.__toESM(require("pg"));
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresMigrator.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a PostgreSQL client connected to the 'postgres' database.
|
|
7
|
+
* Extracts connection details from the provided URI.
|
|
8
|
+
*
|
|
9
|
+
* @param uri - PostgreSQL connection URI
|
|
10
|
+
* @returns Object containing the target database name and client instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');
|
|
15
|
+
* // database = 'mydb'
|
|
16
|
+
* // db = Client instance connected to 'postgres' database
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
async function setupClient(uri) {
|
|
20
|
+
const url = new URL(uri);
|
|
21
|
+
const db = new pg.Client({
|
|
22
|
+
user: url.username,
|
|
23
|
+
password: url.password,
|
|
24
|
+
host: url.hostname,
|
|
25
|
+
port: parseInt(url.port),
|
|
26
|
+
database: "postgres"
|
|
27
|
+
});
|
|
28
|
+
let database = url.pathname.slice(1);
|
|
29
|
+
if (database.includes("?")) database = database.substring(0, database.indexOf("?"));
|
|
30
|
+
return {
|
|
31
|
+
database,
|
|
32
|
+
db
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Default logger instance for migration operations.
|
|
37
|
+
*/
|
|
38
|
+
const logger = console;
|
|
39
|
+
/**
|
|
40
|
+
* Abstract base class for PostgreSQL database migration utilities.
|
|
41
|
+
* Provides database creation, migration, and cleanup functionality for testing.
|
|
42
|
+
* Subclasses must implement the migrate() method to define migration logic.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* class MyMigrator extends PostgresMigrator {
|
|
47
|
+
* async migrate(): Promise<void> {
|
|
48
|
+
* // Run your migrations here
|
|
49
|
+
* await this.runMigrations();
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* // Use in tests
|
|
54
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
55
|
+
* const cleanup = await migrator.start();
|
|
56
|
+
*
|
|
57
|
+
* // Run tests...
|
|
58
|
+
*
|
|
59
|
+
* // Clean up
|
|
60
|
+
* await cleanup();
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
var PostgresMigrator = class PostgresMigrator {
|
|
64
|
+
/**
|
|
65
|
+
* Creates a new PostgresMigrator instance.
|
|
66
|
+
*
|
|
67
|
+
* @param uri - PostgreSQL connection URI
|
|
68
|
+
*/
|
|
69
|
+
constructor(uri) {
|
|
70
|
+
this.uri = uri;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Creates a PostgreSQL database if it doesn't already exist.
|
|
74
|
+
* Connects to the 'postgres' database to check and create the target database.
|
|
75
|
+
*
|
|
76
|
+
* @param uri - PostgreSQL connection URI
|
|
77
|
+
* @returns Object indicating whether the database already existed
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
static async create(uri) {
|
|
81
|
+
const { database, db } = await setupClient(uri);
|
|
82
|
+
try {
|
|
83
|
+
await db.connect();
|
|
84
|
+
const result = await db.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`);
|
|
85
|
+
if (result.rowCount === 0) await db.query(`CREATE DATABASE "${database}"`);
|
|
86
|
+
return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
|
|
87
|
+
} finally {
|
|
88
|
+
await db.end();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Drops a PostgreSQL database.
|
|
93
|
+
* Used for cleanup after tests are complete.
|
|
94
|
+
*
|
|
95
|
+
* @param uri - PostgreSQL connection URI
|
|
96
|
+
* @throws Error if database cannot be dropped
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
static async drop(uri) {
|
|
100
|
+
const { database, db } = await setupClient(uri);
|
|
101
|
+
try {
|
|
102
|
+
await db.connect();
|
|
103
|
+
await db.query(`DROP DATABASE "${database}"`);
|
|
104
|
+
} finally {
|
|
105
|
+
await db.end();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Starts the migration process by creating the database and running migrations.
|
|
110
|
+
* Returns a cleanup function that will drop the database when called.
|
|
111
|
+
*
|
|
112
|
+
* @returns Async cleanup function that drops the created database
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
117
|
+
*
|
|
118
|
+
* // Start migrations and get cleanup function
|
|
119
|
+
* const cleanup = await migrator.start();
|
|
120
|
+
*
|
|
121
|
+
* try {
|
|
122
|
+
* // Run your tests here
|
|
123
|
+
* await runTests();
|
|
124
|
+
* } finally {
|
|
125
|
+
* // Always clean up
|
|
126
|
+
* await cleanup();
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
async start() {
|
|
131
|
+
const { database, db } = await setupClient(this.uri);
|
|
132
|
+
try {
|
|
133
|
+
await PostgresMigrator.create(this.uri);
|
|
134
|
+
await this.migrate();
|
|
135
|
+
logger.log(`Migrating database: ${database}`);
|
|
136
|
+
} finally {
|
|
137
|
+
await db.end();
|
|
138
|
+
}
|
|
139
|
+
return async () => {
|
|
140
|
+
await PostgresMigrator.drop(this.uri);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
Object.defineProperty(exports, 'PostgresMigrator', {
|
|
147
|
+
enumerable: true,
|
|
148
|
+
get: function () {
|
|
149
|
+
return PostgresMigrator;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
|
|
3
|
+
//#region src/PostgresMigrator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates a PostgreSQL client connected to the 'postgres' database.
|
|
6
|
+
* Extracts connection details from the provided URI.
|
|
7
|
+
*
|
|
8
|
+
* @param uri - PostgreSQL connection URI
|
|
9
|
+
* @returns Object containing the target database name and client instance
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');
|
|
14
|
+
* // database = 'mydb'
|
|
15
|
+
* // db = Client instance connected to 'postgres' database
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
async function setupClient(uri) {
|
|
19
|
+
const url = new URL(uri);
|
|
20
|
+
const db = new Client({
|
|
21
|
+
user: url.username,
|
|
22
|
+
password: url.password,
|
|
23
|
+
host: url.hostname,
|
|
24
|
+
port: parseInt(url.port),
|
|
25
|
+
database: "postgres"
|
|
26
|
+
});
|
|
27
|
+
let database = url.pathname.slice(1);
|
|
28
|
+
if (database.includes("?")) database = database.substring(0, database.indexOf("?"));
|
|
29
|
+
return {
|
|
30
|
+
database,
|
|
31
|
+
db
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Default logger instance for migration operations.
|
|
36
|
+
*/
|
|
37
|
+
const logger = console;
|
|
38
|
+
/**
|
|
39
|
+
* Abstract base class for PostgreSQL database migration utilities.
|
|
40
|
+
* Provides database creation, migration, and cleanup functionality for testing.
|
|
41
|
+
* Subclasses must implement the migrate() method to define migration logic.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* class MyMigrator extends PostgresMigrator {
|
|
46
|
+
* async migrate(): Promise<void> {
|
|
47
|
+
* // Run your migrations here
|
|
48
|
+
* await this.runMigrations();
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* // Use in tests
|
|
53
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
54
|
+
* const cleanup = await migrator.start();
|
|
55
|
+
*
|
|
56
|
+
* // Run tests...
|
|
57
|
+
*
|
|
58
|
+
* // Clean up
|
|
59
|
+
* await cleanup();
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
var PostgresMigrator = class PostgresMigrator {
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new PostgresMigrator instance.
|
|
65
|
+
*
|
|
66
|
+
* @param uri - PostgreSQL connection URI
|
|
67
|
+
*/
|
|
68
|
+
constructor(uri) {
|
|
69
|
+
this.uri = uri;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Creates a PostgreSQL database if it doesn't already exist.
|
|
73
|
+
* Connects to the 'postgres' database to check and create the target database.
|
|
74
|
+
*
|
|
75
|
+
* @param uri - PostgreSQL connection URI
|
|
76
|
+
* @returns Object indicating whether the database already existed
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
static async create(uri) {
|
|
80
|
+
const { database, db } = await setupClient(uri);
|
|
81
|
+
try {
|
|
82
|
+
await db.connect();
|
|
83
|
+
const result = await db.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`);
|
|
84
|
+
if (result.rowCount === 0) await db.query(`CREATE DATABASE "${database}"`);
|
|
85
|
+
return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
|
|
86
|
+
} finally {
|
|
87
|
+
await db.end();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Drops a PostgreSQL database.
|
|
92
|
+
* Used for cleanup after tests are complete.
|
|
93
|
+
*
|
|
94
|
+
* @param uri - PostgreSQL connection URI
|
|
95
|
+
* @throws Error if database cannot be dropped
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
static async drop(uri) {
|
|
99
|
+
const { database, db } = await setupClient(uri);
|
|
100
|
+
try {
|
|
101
|
+
await db.connect();
|
|
102
|
+
await db.query(`DROP DATABASE "${database}"`);
|
|
103
|
+
} finally {
|
|
104
|
+
await db.end();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Starts the migration process by creating the database and running migrations.
|
|
109
|
+
* Returns a cleanup function that will drop the database when called.
|
|
110
|
+
*
|
|
111
|
+
* @returns Async cleanup function that drops the created database
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
116
|
+
*
|
|
117
|
+
* // Start migrations and get cleanup function
|
|
118
|
+
* const cleanup = await migrator.start();
|
|
119
|
+
*
|
|
120
|
+
* try {
|
|
121
|
+
* // Run your tests here
|
|
122
|
+
* await runTests();
|
|
123
|
+
* } finally {
|
|
124
|
+
* // Always clean up
|
|
125
|
+
* await cleanup();
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
async start() {
|
|
130
|
+
const { database, db } = await setupClient(this.uri);
|
|
131
|
+
try {
|
|
132
|
+
await PostgresMigrator.create(this.uri);
|
|
133
|
+
await this.migrate();
|
|
134
|
+
logger.log(`Migrating database: ${database}`);
|
|
135
|
+
} finally {
|
|
136
|
+
await db.end();
|
|
137
|
+
}
|
|
138
|
+
return async () => {
|
|
139
|
+
await PostgresMigrator.drop(this.uri);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
export { PostgresMigrator };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-kFL36T8x.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/VitestKyselyTransactionIsolator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Kysely-specific implementation of the Vitest transaction isolator.
|
|
6
|
+
* Provides automatic transaction rollback for test isolation using Kysely's transaction API.
|
|
7
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
8
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
9
|
+
*
|
|
10
|
+
* @template Database - The database schema type
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';
|
|
15
|
+
* import { db } from './database';
|
|
16
|
+
*
|
|
17
|
+
* // Create isolator instance
|
|
18
|
+
* const isolator = new VitestKyselyTransactionIsolator<Database>();
|
|
19
|
+
*
|
|
20
|
+
* // In your test setup
|
|
21
|
+
* beforeEach(async () => {
|
|
22
|
+
* await isolator.start(db);
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* afterEach(async () => {
|
|
26
|
+
* await isolator.rollback();
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Tests run in isolated transactions
|
|
30
|
+
* it('should create user', async () => {
|
|
31
|
+
* const user = await db.insertInto('users')
|
|
32
|
+
* .values({ name: 'Test User' })
|
|
33
|
+
* .returningAll()
|
|
34
|
+
* .executeTakeFirst();
|
|
35
|
+
*
|
|
36
|
+
* expect(user).toBeDefined();
|
|
37
|
+
* // This data will be rolled back after the test
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
var VitestKyselyTransactionIsolator = class extends VitestPostgresTransactionIsolator {
|
|
42
|
+
/**
|
|
43
|
+
* Creates a Kysely transaction with the specified isolation level.
|
|
44
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
45
|
+
*
|
|
46
|
+
* @param conn - The Kysely database connection
|
|
47
|
+
* @param level - The transaction isolation level
|
|
48
|
+
* @param fn - The function to execute within the transaction
|
|
49
|
+
* @returns Promise that resolves when the transaction completes
|
|
50
|
+
*/
|
|
51
|
+
async transact(conn, level, fn) {
|
|
52
|
+
const isolationLevel = level.toLocaleLowerCase();
|
|
53
|
+
await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
export { VitestKyselyTransactionIsolator };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/VitestKyselyTransactionIsolator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Kysely-specific implementation of the Vitest transaction isolator.
|
|
6
|
+
* Provides automatic transaction rollback for test isolation using Kysely's transaction API.
|
|
7
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
8
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
9
|
+
*
|
|
10
|
+
* @template Database - The database schema type
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';
|
|
15
|
+
* import { db } from './database';
|
|
16
|
+
*
|
|
17
|
+
* // Create isolator instance
|
|
18
|
+
* const isolator = new VitestKyselyTransactionIsolator<Database>();
|
|
19
|
+
*
|
|
20
|
+
* // In your test setup
|
|
21
|
+
* beforeEach(async () => {
|
|
22
|
+
* await isolator.start(db);
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* afterEach(async () => {
|
|
26
|
+
* await isolator.rollback();
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Tests run in isolated transactions
|
|
30
|
+
* it('should create user', async () => {
|
|
31
|
+
* const user = await db.insertInto('users')
|
|
32
|
+
* .values({ name: 'Test User' })
|
|
33
|
+
* .returningAll()
|
|
34
|
+
* .executeTakeFirst();
|
|
35
|
+
*
|
|
36
|
+
* expect(user).toBeDefined();
|
|
37
|
+
* // This data will be rolled back after the test
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
var VitestKyselyTransactionIsolator = class extends require_VitestTransactionIsolator.VitestPostgresTransactionIsolator {
|
|
42
|
+
/**
|
|
43
|
+
* Creates a Kysely transaction with the specified isolation level.
|
|
44
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
45
|
+
*
|
|
46
|
+
* @param conn - The Kysely database connection
|
|
47
|
+
* @param level - The transaction isolation level
|
|
48
|
+
* @param fn - The function to execute within the transaction
|
|
49
|
+
* @returns Promise that resolves when the transaction completes
|
|
50
|
+
*/
|
|
51
|
+
async transact(conn, level, fn) {
|
|
52
|
+
const isolationLevel = level.toLocaleLowerCase();
|
|
53
|
+
await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
Object.defineProperty(exports, 'VitestKyselyTransactionIsolator', {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
get: function () {
|
|
61
|
+
return VitestKyselyTransactionIsolator;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('./VitestTransactionIsolator-
|
|
2
|
-
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-
|
|
1
|
+
require('./VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
2
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-YWnSJiIH.cjs');
|
|
3
3
|
|
|
4
4
|
exports.VitestKyselyTransactionIsolator = require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./VitestTransactionIsolator-
|
|
2
|
-
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-
|
|
1
|
+
import "./VitestTransactionIsolator-kFL36T8x.mjs";
|
|
2
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-AfxPJEwR.mjs";
|
|
3
3
|
|
|
4
4
|
export { VitestKyselyTransactionIsolator };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/VitestObjectionTransactionIsolator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Objection.js-specific implementation of the Vitest transaction isolator.
|
|
6
|
+
* Provides automatic transaction rollback for test isolation using Objection.js and Knex transaction API.
|
|
7
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
8
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { VitestObjectionTransactionIsolator } from '@geekmidas/testkit';
|
|
13
|
+
* import { knex } from './database';
|
|
14
|
+
* import { User } from './models';
|
|
15
|
+
* import { test } from 'vitest';
|
|
16
|
+
*
|
|
17
|
+
* // Create isolator instance
|
|
18
|
+
* const isolator = new VitestObjectionTransactionIsolator(test);
|
|
19
|
+
*
|
|
20
|
+
* // Use with wrapped test API
|
|
21
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(knex);
|
|
22
|
+
*
|
|
23
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
24
|
+
* const user = await User.query(trx)
|
|
25
|
+
* .insert({ name: 'Test User' });
|
|
26
|
+
*
|
|
27
|
+
* expect(user).toBeDefined();
|
|
28
|
+
* // This data will be rolled back after the test
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
var VitestObjectionTransactionIsolator = class extends require_VitestTransactionIsolator.VitestPostgresTransactionIsolator {
|
|
33
|
+
/**
|
|
34
|
+
* Creates a Knex transaction with the specified isolation level.
|
|
35
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
36
|
+
* This transaction can be used with Objection.js models via Model.query(trx).
|
|
37
|
+
*
|
|
38
|
+
* @param conn - The Knex database connection
|
|
39
|
+
* @param level - The transaction isolation level
|
|
40
|
+
* @param fn - The function to execute within the transaction
|
|
41
|
+
* @returns Promise that resolves when the transaction completes
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* await isolator.transact(knex, IsolationLevel.REPEATABLE_READ, async (trx) => {
|
|
46
|
+
* // Use transaction with Objection models
|
|
47
|
+
* await User.query(trx).insert({ name: 'Test' });
|
|
48
|
+
* await Post.query(trx).where('userId', user.id).delete();
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
async transact(conn, level, fn) {
|
|
53
|
+
const isolationLevel = level.toUpperCase();
|
|
54
|
+
await conn.transaction(async (trx) => {
|
|
55
|
+
await fn(trx);
|
|
56
|
+
}, { isolationLevel });
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
Object.defineProperty(exports, 'VitestObjectionTransactionIsolator', {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
get: function () {
|
|
64
|
+
return VitestObjectionTransactionIsolator;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-kFL36T8x.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/VitestObjectionTransactionIsolator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Objection.js-specific implementation of the Vitest transaction isolator.
|
|
6
|
+
* Provides automatic transaction rollback for test isolation using Objection.js and Knex transaction API.
|
|
7
|
+
* Each test runs within a database transaction that is rolled back after completion,
|
|
8
|
+
* ensuring a clean state between tests without the overhead of recreating data.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { VitestObjectionTransactionIsolator } from '@geekmidas/testkit';
|
|
13
|
+
* import { knex } from './database';
|
|
14
|
+
* import { User } from './models';
|
|
15
|
+
* import { test } from 'vitest';
|
|
16
|
+
*
|
|
17
|
+
* // Create isolator instance
|
|
18
|
+
* const isolator = new VitestObjectionTransactionIsolator(test);
|
|
19
|
+
*
|
|
20
|
+
* // Use with wrapped test API
|
|
21
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(knex);
|
|
22
|
+
*
|
|
23
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
24
|
+
* const user = await User.query(trx)
|
|
25
|
+
* .insert({ name: 'Test User' });
|
|
26
|
+
*
|
|
27
|
+
* expect(user).toBeDefined();
|
|
28
|
+
* // This data will be rolled back after the test
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
var VitestObjectionTransactionIsolator = class extends VitestPostgresTransactionIsolator {
|
|
33
|
+
/**
|
|
34
|
+
* Creates a Knex transaction with the specified isolation level.
|
|
35
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
36
|
+
* This transaction can be used with Objection.js models via Model.query(trx).
|
|
37
|
+
*
|
|
38
|
+
* @param conn - The Knex database connection
|
|
39
|
+
* @param level - The transaction isolation level
|
|
40
|
+
* @param fn - The function to execute within the transaction
|
|
41
|
+
* @returns Promise that resolves when the transaction completes
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* await isolator.transact(knex, IsolationLevel.REPEATABLE_READ, async (trx) => {
|
|
46
|
+
* // Use transaction with Objection models
|
|
47
|
+
* await User.query(trx).insert({ name: 'Test' });
|
|
48
|
+
* await Post.query(trx).where('userId', user.id).delete();
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
async transact(conn, level, fn) {
|
|
53
|
+
const isolationLevel = level.toUpperCase();
|
|
54
|
+
await conn.transaction(async (trx) => {
|
|
55
|
+
await fn(trx);
|
|
56
|
+
}, { isolationLevel });
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { VitestObjectionTransactionIsolator };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
require('./VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
2
|
+
const require_VitestObjectionTransactionIsolator = require('./VitestObjectionTransactionIsolator-0uX6DW5G.cjs');
|
|
3
|
+
|
|
4
|
+
exports.VitestObjectionTransactionIsolator = require_VitestObjectionTransactionIsolator.VitestObjectionTransactionIsolator;
|