@geekmidas/testkit 0.0.10 → 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-CQ3aUoy_.d.cts +67 -0
- package/dist/PostgresKyselyMigrator-_6yHZigp.d.mts +67 -0
- package/dist/PostgresKyselyMigrator.d.cts +3 -0
- package/dist/PostgresKyselyMigrator.d.mts +3 -0
- package/dist/PostgresMigrator-BlvuQl7d.d.mts +84 -0
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +84 -0
- package/dist/PostgresMigrator.d.cts +2 -0
- package/dist/PostgresMigrator.d.mts +2 -0
- package/dist/PostgresObjectionMigrator-C69n7vzr.d.mts +77 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +77 -0
- package/dist/PostgresObjectionMigrator.d.cts +3 -0
- package/dist/PostgresObjectionMigrator.d.mts +3 -0
- 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.d.cts +1 -0
- package/dist/__tests__/KyselyFactory.spec.d.mts +1 -0
- 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.d.cts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +2 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +2 -1
- package/dist/__tests__/faker.spec.d.cts +1 -0
- package/dist/__tests__/faker.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.d.cts +1 -0
- package/dist/__tests__/integration.spec.d.mts +1 -0
- 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.d.cts +73 -0
- package/dist/kysely.d.mts +73 -0
- package/dist/objection.cjs +2 -1
- package/dist/objection.d.cts +88 -0
- package/dist/objection.d.mts +88 -0
- package/dist/objection.mjs +2 -1
- package/package.json +5 -5
- package/src/ObjectionFactory.ts +156 -13
- package/src/__tests__/ObjectionFactory.spec.ts +162 -0
- package/PostgresKyselyMigrator.spec +0 -471
package/dist/faker.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Kysely database instance with PostgreSQL dialect and camelCase plugin.
|
|
7
|
+
* This is a convenience function for quickly setting up a Kysely connection for testing.
|
|
8
|
+
*
|
|
9
|
+
* @template Database - The database schema type
|
|
10
|
+
* @param config - PostgreSQL connection configuration (pg.Pool config)
|
|
11
|
+
* @returns A configured Kysely instance
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* interface Database {
|
|
16
|
+
* users: UsersTable;
|
|
17
|
+
* posts: PostsTable;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Create from connection string
|
|
21
|
+
* const db = createKyselyDb<Database>({
|
|
22
|
+
* connectionString: 'postgresql://user:pass@localhost:5432/testdb'
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Create with detailed config
|
|
26
|
+
* const db = createKyselyDb<Database>({
|
|
27
|
+
* host: 'localhost',
|
|
28
|
+
* port: 5432,
|
|
29
|
+
* database: 'testdb',
|
|
30
|
+
* user: 'testuser',
|
|
31
|
+
* password: 'testpass',
|
|
32
|
+
* max: 10 // connection pool size
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Use in tests
|
|
36
|
+
* const users = await db.selectFrom('users').selectAll().execute();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function createKyselyDb<Database>(config: any): Kysely<Database>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { createKyselyDb };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Kysely database instance with PostgreSQL dialect and camelCase plugin.
|
|
7
|
+
* This is a convenience function for quickly setting up a Kysely connection for testing.
|
|
8
|
+
*
|
|
9
|
+
* @template Database - The database schema type
|
|
10
|
+
* @param config - PostgreSQL connection configuration (pg.Pool config)
|
|
11
|
+
* @returns A configured Kysely instance
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* interface Database {
|
|
16
|
+
* users: UsersTable;
|
|
17
|
+
* posts: PostsTable;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Create from connection string
|
|
21
|
+
* const db = createKyselyDb<Database>({
|
|
22
|
+
* connectionString: 'postgresql://user:pass@localhost:5432/testdb'
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Create with detailed config
|
|
26
|
+
* const db = createKyselyDb<Database>({
|
|
27
|
+
* host: 'localhost',
|
|
28
|
+
* port: 5432,
|
|
29
|
+
* database: 'testdb',
|
|
30
|
+
* user: 'testuser',
|
|
31
|
+
* password: 'testpass',
|
|
32
|
+
* max: 10 // connection pool size
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Use in tests
|
|
36
|
+
* const users = await db.selectFrom('users').selectAll().execute();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function createKyselyDb<Database>(config: any): Kysely<Database>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { createKyselyDb };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "./faker-DAiFK3T3.cjs";
|
|
2
|
+
import "./Factory-B9LP1L18.cjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-CrLWxJW4.cjs";
|
|
4
|
+
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-CQ3aUoy_.cjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
|
7
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-UE1J-UoP.cjs";
|
|
8
|
+
import { Kysely, Transaction } from "kysely";
|
|
9
|
+
import { TestAPI } from "vitest";
|
|
10
|
+
|
|
11
|
+
//#region src/kysely.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @template Database - The database schema type
|
|
19
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
20
|
+
* @param db - The Kysely database instance
|
|
21
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
22
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
23
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { test } from 'vitest';
|
|
28
|
+
* import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';
|
|
29
|
+
* import { db } from './database';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, db);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await trx
|
|
37
|
+
* .insertInto('users')
|
|
38
|
+
* .values({ name: 'Test User', email: 'test@example.com' })
|
|
39
|
+
* .returningAll()
|
|
40
|
+
* .executeTakeFirst();
|
|
41
|
+
*
|
|
42
|
+
* expect(user).toBeDefined();
|
|
43
|
+
* // User is automatically rolled back after test
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // With setup function for common test data
|
|
47
|
+
* const testWithSetup = wrapVitestKyselyTransaction(
|
|
48
|
+
* test,
|
|
49
|
+
* db,
|
|
50
|
+
* async (trx) => {
|
|
51
|
+
* // Create common test data
|
|
52
|
+
* await trx.insertInto('settings')
|
|
53
|
+
* .values({ key: 'test_mode', value: 'true' })
|
|
54
|
+
* .execute();
|
|
55
|
+
* }
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
59
|
+
* const setting = await trx
|
|
60
|
+
* .selectFrom('settings')
|
|
61
|
+
* .where('key', '=', 'test_mode')
|
|
62
|
+
* .selectAll()
|
|
63
|
+
* .executeTakeFirst();
|
|
64
|
+
*
|
|
65
|
+
* expect(setting?.value).toBe('true');
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function wrapVitestKyselyTransaction<Database>(api: TestAPI, db: Kysely<Database>, setup?: (trx: Transaction<Database>) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
70
|
+
trx: Transaction<Database>;
|
|
71
|
+
}>;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { IsolationLevel, KyselyFactory, PostgresKyselyMigrator, VitestKyselyTransactionIsolator, wrapVitestKyselyTransaction };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "./faker-nN9Ki6fn.mjs";
|
|
2
|
+
import "./Factory-DiZSNxC0.mjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-B-zlLbov.mjs";
|
|
4
|
+
import "./PostgresMigrator-BlvuQl7d.mjs";
|
|
5
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-_6yHZigp.mjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
|
7
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-ClCazkBO.mjs";
|
|
8
|
+
import { Kysely, Transaction } from "kysely";
|
|
9
|
+
import { TestAPI } from "vitest";
|
|
10
|
+
|
|
11
|
+
//#region src/kysely.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @template Database - The database schema type
|
|
19
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
20
|
+
* @param db - The Kysely database instance
|
|
21
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
22
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
23
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { test } from 'vitest';
|
|
28
|
+
* import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';
|
|
29
|
+
* import { db } from './database';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, db);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await trx
|
|
37
|
+
* .insertInto('users')
|
|
38
|
+
* .values({ name: 'Test User', email: 'test@example.com' })
|
|
39
|
+
* .returningAll()
|
|
40
|
+
* .executeTakeFirst();
|
|
41
|
+
*
|
|
42
|
+
* expect(user).toBeDefined();
|
|
43
|
+
* // User is automatically rolled back after test
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // With setup function for common test data
|
|
47
|
+
* const testWithSetup = wrapVitestKyselyTransaction(
|
|
48
|
+
* test,
|
|
49
|
+
* db,
|
|
50
|
+
* async (trx) => {
|
|
51
|
+
* // Create common test data
|
|
52
|
+
* await trx.insertInto('settings')
|
|
53
|
+
* .values({ key: 'test_mode', value: 'true' })
|
|
54
|
+
* .execute();
|
|
55
|
+
* }
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
59
|
+
* const setting = await trx
|
|
60
|
+
* .selectFrom('settings')
|
|
61
|
+
* .where('key', '=', 'test_mode')
|
|
62
|
+
* .selectAll()
|
|
63
|
+
* .executeTakeFirst();
|
|
64
|
+
*
|
|
65
|
+
* expect(setting?.value).toBe('true');
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function wrapVitestKyselyTransaction<Database>(api: TestAPI, db: Kysely<Database>, setup?: (trx: Transaction<Database>) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
70
|
+
trx: Transaction<Database>;
|
|
71
|
+
}>;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { IsolationLevel, KyselyFactory, PostgresKyselyMigrator, VitestKyselyTransactionIsolator, wrapVitestKyselyTransaction };
|
package/dist/objection.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
|
-
|
|
2
|
+
require('./faker-SMN4ira4.cjs');
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-Wq80ypMM.cjs');
|
|
3
4
|
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
4
5
|
const require_PostgresObjectionMigrator = require('./PostgresObjectionMigrator-BG6ymgnt.cjs');
|
|
5
6
|
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CruLTRRi.cjs');
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "./faker-DAiFK3T3.cjs";
|
|
2
|
+
import "./Factory-B9LP1L18.cjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-vME-wNlq.cjs";
|
|
4
|
+
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
|
+
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-CZHHcCOv.cjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-CO2nTi9r.cjs";
|
|
8
|
+
import { Knex } from "knex";
|
|
9
|
+
import { TestAPI } from "vitest";
|
|
10
|
+
|
|
11
|
+
//#region src/objection.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
19
|
+
* @param knex - The Knex database connection instance
|
|
20
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
21
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
22
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { test } from 'vitest';
|
|
27
|
+
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
28
|
+
* import { knex } from './database';
|
|
29
|
+
* import { User, Post } from './models';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await User.query(trx)
|
|
37
|
+
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
38
|
+
*
|
|
39
|
+
* expect(user).toBeDefined();
|
|
40
|
+
* // User is automatically rolled back after test
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // With setup function for common test data
|
|
44
|
+
* const testWithSetup = wrapVitestObjectionTransaction(
|
|
45
|
+
* test,
|
|
46
|
+
* knex,
|
|
47
|
+
* async (trx) => {
|
|
48
|
+
* // Create common test data
|
|
49
|
+
* await knex('settings')
|
|
50
|
+
* .transacting(trx)
|
|
51
|
+
* .insert({ key: 'test_mode', value: 'true' });
|
|
52
|
+
* }
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
56
|
+
* const setting = await knex('settings')
|
|
57
|
+
* .transacting(trx)
|
|
58
|
+
* .where('key', 'test_mode')
|
|
59
|
+
* .first();
|
|
60
|
+
*
|
|
61
|
+
* expect(setting?.value).toBe('true');
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* // Example with factory and transaction
|
|
65
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
66
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
67
|
+
*
|
|
68
|
+
* isolatedTest('creates related data', async ({ trx }) => {
|
|
69
|
+
* // Factory can use the transaction
|
|
70
|
+
* const user = await User.query(trx).insert({ name: 'Author' });
|
|
71
|
+
* const posts = await Post.query(trx).insert([
|
|
72
|
+
* { title: 'Post 1', userId: user.id },
|
|
73
|
+
* { title: 'Post 2', userId: user.id }
|
|
74
|
+
* ]);
|
|
75
|
+
*
|
|
76
|
+
* const userWithPosts = await User.query(trx)
|
|
77
|
+
* .findById(user.id)
|
|
78
|
+
* .withGraphFetched('posts');
|
|
79
|
+
*
|
|
80
|
+
* expect(userWithPosts.posts).toHaveLength(2);
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function wrapVitestObjectionTransaction(api: TestAPI, knex: Knex, setup?: (trx: Knex.Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
85
|
+
trx: Knex.Transaction<any, any[]>;
|
|
86
|
+
}>;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "./faker-nN9Ki6fn.mjs";
|
|
2
|
+
import "./Factory-DiZSNxC0.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-CJCpvwts.mjs";
|
|
4
|
+
import "./PostgresMigrator-BlvuQl7d.mjs";
|
|
5
|
+
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-C69n7vzr.mjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-D264iuPy.mjs";
|
|
8
|
+
import { TestAPI } from "vitest";
|
|
9
|
+
import { Knex } from "knex";
|
|
10
|
+
|
|
11
|
+
//#region src/objection.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
19
|
+
* @param knex - The Knex database connection instance
|
|
20
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
21
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
22
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { test } from 'vitest';
|
|
27
|
+
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
28
|
+
* import { knex } from './database';
|
|
29
|
+
* import { User, Post } from './models';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await User.query(trx)
|
|
37
|
+
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
38
|
+
*
|
|
39
|
+
* expect(user).toBeDefined();
|
|
40
|
+
* // User is automatically rolled back after test
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // With setup function for common test data
|
|
44
|
+
* const testWithSetup = wrapVitestObjectionTransaction(
|
|
45
|
+
* test,
|
|
46
|
+
* knex,
|
|
47
|
+
* async (trx) => {
|
|
48
|
+
* // Create common test data
|
|
49
|
+
* await knex('settings')
|
|
50
|
+
* .transacting(trx)
|
|
51
|
+
* .insert({ key: 'test_mode', value: 'true' });
|
|
52
|
+
* }
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
56
|
+
* const setting = await knex('settings')
|
|
57
|
+
* .transacting(trx)
|
|
58
|
+
* .where('key', 'test_mode')
|
|
59
|
+
* .first();
|
|
60
|
+
*
|
|
61
|
+
* expect(setting?.value).toBe('true');
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* // Example with factory and transaction
|
|
65
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
66
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
67
|
+
*
|
|
68
|
+
* isolatedTest('creates related data', async ({ trx }) => {
|
|
69
|
+
* // Factory can use the transaction
|
|
70
|
+
* const user = await User.query(trx).insert({ name: 'Author' });
|
|
71
|
+
* const posts = await Post.query(trx).insert([
|
|
72
|
+
* { title: 'Post 1', userId: user.id },
|
|
73
|
+
* { title: 'Post 2', userId: user.id }
|
|
74
|
+
* ]);
|
|
75
|
+
*
|
|
76
|
+
* const userWithPosts = await User.query(trx)
|
|
77
|
+
* .findById(user.id)
|
|
78
|
+
* .withGraphFetched('posts');
|
|
79
|
+
*
|
|
80
|
+
* expect(userWithPosts.posts).toHaveLength(2);
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function wrapVitestObjectionTransaction(api: TestAPI, knex: Knex, setup?: (trx: Knex.Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
85
|
+
trx: Knex.Transaction<any, any[]>;
|
|
86
|
+
}>;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
package/dist/objection.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import "./Factory-z2m01hMj.mjs";
|
|
2
|
-
import
|
|
2
|
+
import "./faker-CxKkEeYi.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-aqM0dDW7.mjs";
|
|
3
4
|
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
4
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-G4h5FLvU.mjs";
|
|
5
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-BWwK-ca6.mjs";
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./objection": {
|
|
8
|
-
"types": "./
|
|
8
|
+
"types": "./dist/objection.d.ts",
|
|
9
9
|
"import": "./dist/objection.mjs",
|
|
10
10
|
"require": "./dist/objection.cjs"
|
|
11
11
|
},
|
|
12
12
|
"./kysely": {
|
|
13
|
-
"types": "./
|
|
13
|
+
"types": "./dist/kysely.ts",
|
|
14
14
|
"import": "./dist/kysely.mjs",
|
|
15
15
|
"require": "./dist/kysely.cjs"
|
|
16
16
|
},
|
|
17
17
|
"./faker": {
|
|
18
|
-
"types": "./
|
|
18
|
+
"types": "./dist/faker.d.ts",
|
|
19
19
|
"import": "./dist/faker.mjs",
|
|
20
20
|
"require": "./dist/faker.cjs"
|
|
21
21
|
}
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"objection": "~3.1.5",
|
|
38
38
|
"db-errors": "~0.2.3",
|
|
39
39
|
"vitest": "~3.2.4",
|
|
40
|
-
"@geekmidas/envkit": "0.0.
|
|
40
|
+
"@geekmidas/envkit": "0.0.5"
|
|
41
41
|
}
|
|
42
42
|
}
|