@geekmidas/testkit 0.1.0 → 0.2.0

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.
Files changed (65) hide show
  1. package/README.md +162 -13
  2. package/dist/{Factory-BZ8uMoXl.d.cts → Factory-D7oHtI2D.d.cts} +2 -2
  3. package/dist/Factory.d.cts +2 -2
  4. package/dist/{KyselyFactory-BFygzOlO.d.cts → KyselyFactory-dYdviox2.d.cts} +3 -3
  5. package/dist/KyselyFactory.d.cts +3 -3
  6. package/dist/{ObjectionFactory-CeSIN3kZ.d.cts → ObjectionFactory-0gUdx8bB.d.cts} +3 -3
  7. package/dist/ObjectionFactory.d.cts +3 -3
  8. package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs → VitestKyselyTransactionIsolator-CNURW8y6.mjs} +2 -2
  9. package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map → VitestKyselyTransactionIsolator-CNURW8y6.mjs.map} +1 -1
  10. package/dist/{VitestKyselyTransactionIsolator-DnyZMaA-.d.mts → VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts} +2 -2
  11. package/dist/{VitestKyselyTransactionIsolator-4HOeLQ0d.d.cts → VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts} +2 -2
  12. package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs → VitestKyselyTransactionIsolator-EvDLk5zg.cjs} +2 -2
  13. package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map → VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map} +1 -1
  14. package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
  15. package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
  16. package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
  17. package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
  18. package/dist/{VitestObjectionTransactionIsolator-COVDlpEo.d.cts → VitestObjectionTransactionIsolator-1TpsPqfG.d.cts} +2 -2
  19. package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs → VitestObjectionTransactionIsolator-CM5KTAFA.cjs} +2 -2
  20. package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map → VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map} +1 -1
  21. package/dist/{VitestObjectionTransactionIsolator-lZUSz1w0.d.mts → VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts} +2 -2
  22. package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs → VitestObjectionTransactionIsolator-jQFaCz0u.mjs} +2 -2
  23. package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map → VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map} +1 -1
  24. package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
  25. package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
  26. package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
  27. package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
  28. package/dist/{VitestTransactionIsolator-DWDbnITQ.d.mts → VitestTransactionIsolator-BvR19bYn.d.mts} +44 -18
  29. package/dist/{VitestTransactionIsolator-BKIrj3Uy.cjs → VitestTransactionIsolator-CMfJXZP8.cjs} +52 -36
  30. package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -0
  31. package/dist/{VitestTransactionIsolator-CyG_i_Nj.d.cts → VitestTransactionIsolator-CwQaxZLP.d.cts} +44 -18
  32. package/dist/{VitestTransactionIsolator-BIaMs4c2.mjs → VitestTransactionIsolator-DQ7tLqgV.mjs} +52 -36
  33. package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -0
  34. package/dist/VitestTransactionIsolator.cjs +1 -1
  35. package/dist/VitestTransactionIsolator.d.cts +2 -2
  36. package/dist/VitestTransactionIsolator.d.mts +2 -2
  37. package/dist/VitestTransactionIsolator.mjs +1 -1
  38. package/dist/better-auth.d.cts +2 -2
  39. package/dist/{directory-DlkPEzL4.d.cts → directory-BUcnztHI.d.cts} +3 -3
  40. package/dist/{faker-Cg76aFNO.d.cts → faker-Dg3trU4a.d.cts} +3 -3
  41. package/dist/faker.d.cts +1 -1
  42. package/dist/kysely.cjs +23 -29
  43. package/dist/kysely.cjs.map +1 -1
  44. package/dist/kysely.d.cts +40 -38
  45. package/dist/kysely.d.mts +37 -35
  46. package/dist/kysely.mjs +23 -29
  47. package/dist/kysely.mjs.map +1 -1
  48. package/dist/objection.cjs +23 -47
  49. package/dist/objection.cjs.map +1 -1
  50. package/dist/objection.d.cts +40 -56
  51. package/dist/objection.d.mts +37 -53
  52. package/dist/objection.mjs +23 -47
  53. package/dist/objection.mjs.map +1 -1
  54. package/dist/os/directory.d.cts +1 -1
  55. package/dist/os/index.d.cts +1 -1
  56. package/package.json +1 -1
  57. package/src/VitestTransactionIsolator.ts +98 -31
  58. package/src/__tests__/KyselyFactory.spec.ts +4 -5
  59. package/src/__tests__/ObjectionFactory.spec.ts +4 -5
  60. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +6 -3
  61. package/src/__tests__/integration.spec.ts +8 -10
  62. package/src/kysely.ts +49 -34
  63. package/src/objection.ts +47 -52
  64. package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +0 -1
  65. package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +0 -1
@@ -7,11 +7,10 @@ import { createKyselyDb } from '../helpers';
7
7
  import { wrapVitestKyselyTransaction } from '../kysely';
8
8
 
9
9
  const db = () => createKyselyDb<TestDatabase>(TEST_DATABASE_CONFIG);
10
- const itWithTransaction = wrapVitestKyselyTransaction<TestDatabase>(
11
- it,
12
- db,
13
- createTestTables,
14
- );
10
+ const itWithTransaction = wrapVitestKyselyTransaction<TestDatabase>(it, {
11
+ connection: db,
12
+ setup: createTestTables,
13
+ });
15
14
 
16
15
  const int8TypeId = 20;
17
16
  pg.types.setTypeParser(int8TypeId, (val) => {
@@ -37,11 +37,10 @@ class Comment extends Model {
37
37
  user_id!: string;
38
38
  }
39
39
 
40
- const it = wrapVitestObjectionTransaction(
41
- test,
42
- createKnexDb,
43
- createTestTablesKnex,
44
- );
40
+ const it = wrapVitestObjectionTransaction(test, {
41
+ connection: createKnexDb,
42
+ setup: createTestTablesKnex,
43
+ });
45
44
  describe('ObjectionFactory', () => {
46
45
  it('should create an ObjectionFactory instance', ({ trx }) => {
47
46
  const builders = {};
@@ -108,9 +108,12 @@ class Comment extends Model {
108
108
  // Create database connection
109
109
 
110
110
  // Create wrapped test with transaction isolation
111
- const it = wrapVitestObjectionTransaction(base, createKnexDb, async (trx) => {
112
- // Create tables in the transaction
113
- await createTestTablesKnex(trx);
111
+ const it = wrapVitestObjectionTransaction(base, {
112
+ connection: createKnexDb,
113
+ setup: async (trx) => {
114
+ // Create tables in the transaction
115
+ await createTestTablesKnex(trx);
116
+ },
114
117
  });
115
118
 
116
119
  describe('VitestObjectionTransactionIsolator', () => {
@@ -6,11 +6,10 @@ import { createKyselyDb } from '../helpers';
6
6
  import { extendWithFixtures, wrapVitestKyselyTransaction } from '../kysely';
7
7
 
8
8
  const db = () => createKyselyDb<TestDatabase>(TEST_DATABASE_CONFIG);
9
- const it = wrapVitestKyselyTransaction<TestDatabase>(
10
- base,
11
- db,
12
- createTestTables,
13
- );
9
+ const it = wrapVitestKyselyTransaction<TestDatabase>(base, {
10
+ connection: db,
11
+ setup: createTestTables,
12
+ });
14
13
  describe('Testkit Integration Tests', () => {
15
14
  beforeAll(async () => {});
16
15
  describe('Complex Factory Scenarios', () => {
@@ -481,11 +480,10 @@ describe('extendWithFixtures', () => {
481
480
  };
482
481
 
483
482
  // Create base test with transaction
484
- const baseTest = wrapVitestKyselyTransaction<TestDatabase>(
485
- base,
486
- db,
487
- createTestTables,
488
- );
483
+ const baseTest = wrapVitestKyselyTransaction<TestDatabase>(base, {
484
+ connection: db,
485
+ setup: createTestTables,
486
+ });
489
487
 
490
488
  // Extend with factory fixture
491
489
  const itWithFactory = extendWithFixtures<
package/src/kysely.ts CHANGED
@@ -4,7 +4,7 @@ import { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolat
4
4
  import {
5
5
  type DatabaseConnection,
6
6
  type FixtureCreators,
7
- IsolationLevel,
7
+ type IsolationLevel,
8
8
  extendWithFixtures as baseExtendWithFixtures,
9
9
  } from './VitestTransactionIsolator';
10
10
 
@@ -17,7 +17,30 @@ export { KyselyFactory } from './KyselyFactory';
17
17
  export { PostgresKyselyMigrator } from './PostgresKyselyMigrator';
18
18
  export { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';
19
19
  export { IsolationLevel } from './VitestTransactionIsolator';
20
- export type { FixtureCreators } from './VitestTransactionIsolator';
20
+ export type {
21
+ DatabaseFixtures,
22
+ ExtendedDatabaseFixtures,
23
+ FixtureCreators,
24
+ TestWithExtendedFixtures,
25
+ TransactionWrapperOptions,
26
+ } from './VitestTransactionIsolator';
27
+
28
+ /**
29
+ * Kysely-specific options for transaction wrapping.
30
+ */
31
+ export interface KyselyTransactionOptions<
32
+ Database,
33
+ Extended extends Record<string, unknown> = {},
34
+ > {
35
+ /** Function that creates or returns a Kysely database instance */
36
+ connection: DatabaseConnection<Kysely<Database>>;
37
+ /** Optional setup function to run within the transaction before each test */
38
+ setup?: (trx: Transaction<Database>) => Promise<void>;
39
+ /** Transaction isolation level (defaults to REPEATABLE_READ) */
40
+ isolationLevel?: IsolationLevel;
41
+ /** Additional fixtures that depend on the transaction */
42
+ fixtures?: FixtureCreators<Transaction<Database>, Extended>;
43
+ }
21
44
 
22
45
  // Re-export faker and FakerFactory for type portability in declaration files
23
46
  export { faker, type FakerFactory } from './faker';
@@ -28,10 +51,9 @@ export { faker, type FakerFactory } from './faker';
28
51
  * This ensures tests don't affect each other's data and run faster than truncating tables.
29
52
  *
30
53
  * @template Database - The database schema type
54
+ * @template Extended - Additional fixtures to provide
31
55
  * @param api - The Vitest test API (usually `test` from vitest)
32
- * @param db - The Kysely database instance
33
- * @param setup - Optional setup function to run before each test in the transaction
34
- * @param level - Transaction isolation level (defaults to REPEATABLE_READ)
56
+ * @param options - Configuration options for transaction wrapping
35
57
  * @returns A wrapped test API that provides transaction isolation
36
58
  *
37
59
  * @example
@@ -41,7 +63,9 @@ export { faker, type FakerFactory } from './faker';
41
63
  * import { db } from './database';
42
64
  *
43
65
  * // Create isolated test with automatic rollback
44
- * const isolatedTest = wrapVitestKyselyTransaction(test, db);
66
+ * const isolatedTest = wrapVitestKyselyTransaction(test, {
67
+ * connection: db,
68
+ * });
45
69
  *
46
70
  * // Use in tests - each test gets its own transaction
47
71
  * isolatedTest('should create user', async ({ trx }) => {
@@ -52,41 +76,29 @@ export { faker, type FakerFactory } from './faker';
52
76
  * .executeTakeFirst();
53
77
  *
54
78
  * expect(user).toBeDefined();
55
- * // User is automatically rolled back after test
56
79
  * });
57
80
  *
58
- * // With setup function for common test data
59
- * const testWithSetup = wrapVitestKyselyTransaction(
60
- * test,
61
- * db,
62
- * async (trx) => {
63
- * // Create common test data
64
- * await trx.insertInto('settings')
65
- * .values({ key: 'test_mode', value: 'true' })
66
- * .execute();
67
- * }
68
- * );
69
- *
70
- * testWithSetup('should have test settings', async ({ trx }) => {
71
- * const setting = await trx
72
- * .selectFrom('settings')
73
- * .where('key', '=', 'test_mode')
74
- * .selectAll()
75
- * .executeTakeFirst();
81
+ * // With fixtures for factories
82
+ * const it = wrapVitestKyselyTransaction<DB, { factory: Factory }>(test, {
83
+ * connection: db,
84
+ * fixtures: {
85
+ * factory: (trx) => new Factory(trx),
86
+ * },
87
+ * });
76
88
  *
77
- * expect(setting?.value).toBe('true');
89
+ * it('should create user with factory', async ({ trx, factory }) => {
90
+ * const user = await factory.insert('user', { name: 'Test' });
91
+ * expect(user.id).toBeDefined();
78
92
  * });
79
93
  * ```
80
94
  */
81
- export function wrapVitestKyselyTransaction<Database>(
82
- api: TestAPI,
83
- connection: DatabaseConnection<Kysely<Database>>,
84
- setup?: (trx: Transaction<Database>) => Promise<void>,
85
- level: IsolationLevel = IsolationLevel.REPEATABLE_READ,
86
- ) {
95
+ export function wrapVitestKyselyTransaction<
96
+ Database,
97
+ Extended extends Record<string, unknown> = {},
98
+ >(api: TestAPI, options: KyselyTransactionOptions<Database, Extended>) {
87
99
  const wrapper = new VitestKyselyTransactionIsolator<Database>(api);
88
100
 
89
- return wrapper.wrapVitestWithTransaction(connection, setup, level);
101
+ return wrapper.wrapVitestWithTransaction(options);
90
102
  }
91
103
 
92
104
  /**
@@ -113,7 +125,10 @@ export function wrapVitestKyselyTransaction<Database>(
113
125
  * };
114
126
  *
115
127
  * // Create base wrapped test
116
- * const baseTest = wrapVitestKyselyTransaction<DB>(test, db, createTestTables);
128
+ * const baseTest = wrapVitestKyselyTransaction<DB>(test, {
129
+ * connection: db,
130
+ * setup: createTestTables,
131
+ * });
117
132
  *
118
133
  * // Extend with fixtures - each fixture receives the transaction
119
134
  * const it = extendWithFixtures<DB, { factory: KyselyFactory<DB, typeof builders, {}> }>(
package/src/objection.ts CHANGED
@@ -4,7 +4,7 @@ import { VitestObjectionTransactionIsolator } from './VitestObjectionTransaction
4
4
  import {
5
5
  type DatabaseConnection,
6
6
  type FixtureCreators,
7
- IsolationLevel,
7
+ type IsolationLevel,
8
8
  extendWithFixtures as baseExtendWithFixtures,
9
9
  } from './VitestTransactionIsolator';
10
10
 
@@ -18,7 +18,29 @@ export { ObjectionFactory } from './ObjectionFactory';
18
18
  export { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';
19
19
  export { IsolationLevel } from './VitestTransactionIsolator';
20
20
  export { PostgresObjectionMigrator } from './PostgresObjectionMigrator';
21
- export type { FixtureCreators } from './VitestTransactionIsolator';
21
+ export type {
22
+ DatabaseFixtures,
23
+ ExtendedDatabaseFixtures,
24
+ FixtureCreators,
25
+ TestWithExtendedFixtures,
26
+ TransactionWrapperOptions,
27
+ } from './VitestTransactionIsolator';
28
+
29
+ /**
30
+ * Objection.js-specific options for transaction wrapping.
31
+ */
32
+ export interface ObjectionTransactionOptions<
33
+ Extended extends Record<string, unknown> = {},
34
+ > {
35
+ /** Function that creates or returns a Knex database connection */
36
+ connection: DatabaseConnection<Knex>;
37
+ /** Optional setup function to run within the transaction before each test */
38
+ setup?: (trx: Knex.Transaction) => Promise<void>;
39
+ /** Transaction isolation level (defaults to REPEATABLE_READ) */
40
+ isolationLevel?: IsolationLevel;
41
+ /** Additional fixtures that depend on the transaction */
42
+ fixtures?: FixtureCreators<Knex.Transaction, Extended>;
43
+ }
22
44
 
23
45
  // Re-export faker and FakerFactory for type portability in declaration files
24
46
  export { faker, type FakerFactory } from './faker';
@@ -28,10 +50,9 @@ export { faker, type FakerFactory } from './faker';
28
50
  * Each test runs in an isolated database transaction that is rolled back after completion.
29
51
  * This ensures tests don't affect each other's data and run faster than truncating tables.
30
52
  *
53
+ * @template Extended - Additional fixtures to provide
31
54
  * @param api - The Vitest test API (usually `test` from vitest)
32
- * @param conn - The Knex database connection instance
33
- * @param setup - Optional setup function to run before each test in the transaction
34
- * @param level - Transaction isolation level (defaults to REPEATABLE_READ)
55
+ * @param options - Configuration options for transaction wrapping
35
56
  * @returns A wrapped test API that provides transaction isolation
36
57
  *
37
58
  * @example
@@ -39,10 +60,12 @@ export { faker, type FakerFactory } from './faker';
39
60
  * import { test } from 'vitest';
40
61
  * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
41
62
  * import { knex } from './database';
42
- * import { User, Post } from './models';
63
+ * import { User } from './models';
43
64
  *
44
65
  * // Create isolated test with automatic rollback
45
- * const isolatedTest = wrapVitestObjectionTransaction(test, knex);
66
+ * const isolatedTest = wrapVitestObjectionTransaction(test, {
67
+ * connection: knex,
68
+ * });
46
69
  *
47
70
  * // Use in tests - each test gets its own transaction
48
71
  * isolatedTest('should create user', async ({ trx }) => {
@@ -50,59 +73,28 @@ export { faker, type FakerFactory } from './faker';
50
73
  * .insert({ name: 'Test User', email: 'test@example.com' });
51
74
  *
52
75
  * expect(user).toBeDefined();
53
- * // User is automatically rolled back after test
54
76
  * });
55
77
  *
56
- * // With setup function for common test data
57
- * const testWithSetup = wrapVitestObjectionTransaction(
58
- * test,
59
- * knex,
60
- * async (trx) => {
61
- * // Create common test data
62
- * await knex('settings')
63
- * .transacting(trx)
64
- * .insert({ key: 'test_mode', value: 'true' });
65
- * }
66
- * );
67
- *
68
- * testWithSetup('should have test settings', async ({ trx }) => {
69
- * const setting = await knex('settings')
70
- * .transacting(trx)
71
- * .where('key', 'test_mode')
72
- * .first();
73
- *
74
- * expect(setting?.value).toBe('true');
78
+ * // With fixtures for factories
79
+ * const it = wrapVitestObjectionTransaction<{ factory: Factory }>(test, {
80
+ * connection: knex,
81
+ * fixtures: {
82
+ * factory: (trx) => new Factory(trx),
83
+ * },
75
84
  * });
76
85
  *
77
- * // Example with factory and transaction
78
- * const isolatedTest = wrapVitestObjectionTransaction(test, knex);
79
- * const factory = new ObjectionFactory(builders, seeds, knex);
80
- *
81
- * isolatedTest('creates related data', async ({ trx }) => {
82
- * // Factory can use the transaction
83
- * const user = await User.query(trx).insert({ name: 'Author' });
84
- * const posts = await Post.query(trx).insert([
85
- * { title: 'Post 1', userId: user.id },
86
- * { title: 'Post 2', userId: user.id }
87
- * ]);
88
- *
89
- * const userWithPosts = await User.query(trx)
90
- * .findById(user.id)
91
- * .withGraphFetched('posts');
92
- *
93
- * expect(userWithPosts.posts).toHaveLength(2);
86
+ * it('should create user with factory', async ({ trx, factory }) => {
87
+ * const user = await factory.insert('user', { name: 'Test' });
88
+ * expect(user.id).toBeDefined();
94
89
  * });
95
90
  * ```
96
91
  */
97
- export function wrapVitestObjectionTransaction(
98
- api: TestAPI,
99
- conn: DatabaseConnection<Knex>,
100
- setup?: (trx: Knex.Transaction) => Promise<void>,
101
- level: IsolationLevel = IsolationLevel.REPEATABLE_READ,
102
- ) {
92
+ export function wrapVitestObjectionTransaction<
93
+ Extended extends Record<string, unknown> = {},
94
+ >(api: TestAPI, options: ObjectionTransactionOptions<Extended>) {
103
95
  const wrapper = new VitestObjectionTransactionIsolator(api);
104
96
 
105
- return wrapper.wrapVitestWithTransaction(conn, setup, level);
97
+ return wrapper.wrapVitestWithTransaction(options);
106
98
  }
107
99
 
108
100
  /**
@@ -129,7 +121,10 @@ export function wrapVitestObjectionTransaction(
129
121
  * };
130
122
  *
131
123
  * // Create base wrapped test
132
- * const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
124
+ * const baseTest = wrapVitestObjectionTransaction(test, {
125
+ * connection: knex,
126
+ * setup: createTestTables,
127
+ * });
133
128
  *
134
129
  * // Extend with fixtures - each fixture receives the transaction
135
130
  * const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
@@ -1 +0,0 @@
1
- {"version":3,"file":"VitestTransactionIsolator-BIaMs4c2.mjs","names":["api: TestAPI","createConnection: DatabaseConnection<TConn>","setup?: (trx: Transaction) => Promise<void>","level: IsolationLevel","use: (value: Transaction) => Promise<void>","testError: Error | undefined","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>","use: (value: unknown) => Promise<void>"],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":["import type { TestAPI } from 'vitest';\n\n/**\n * Type definition for test fixtures that provide transaction access.\n * Used with Vitest's test.extend() API to inject transactions into tests.\n *\n * @template Transaction - The transaction type specific to the database driver\n * @template Extended - Additional context properties provided by the extend function\n */\nexport interface DatabaseFixtures<Transaction, Extended = object> {\n /**\n * The database transaction available to the test.\n * All database operations should use this transaction to ensure proper rollback.\n */\n trx: Transaction;\n}\n\n/**\n * Combined fixtures type that merges the base transaction fixture with extended context.\n */\nexport type ExtendedDatabaseFixtures<\n Transaction,\n Extended = object,\n> = DatabaseFixtures<Transaction> & Extended;\n\n/**\n * Function type for extending test context with additional properties.\n * Receives the transaction and returns additional context to be merged with { trx }.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n *\n * @example\n * ```typescript\n * const extendContext: ExtendContextFn<Transaction<DB>, { factory: KyselyFactory }> =\n * (trx) => ({ factory: new KyselyFactory(builders, seeds, trx) });\n * ```\n */\nexport type ExtendContextFn<Transaction, Extended> = (\n trx: Transaction,\n) => Extended | Promise<Extended>;\n\n/**\n * PostgreSQL transaction isolation levels.\n * Controls the visibility of concurrent transactions.\n *\n * @see https://www.postgresql.org/docs/current/transaction-iso.html\n */\nexport enum IsolationLevel {\n /**\n * Lowest isolation level. Allows dirty reads.\n * Not recommended for testing.\n */\n READ_UNCOMMITTED = 'READ UNCOMMITTED',\n /**\n * Default PostgreSQL isolation level.\n * Prevents dirty reads but allows non-repeatable reads.\n */\n READ_COMMITTED = 'READ COMMITTED',\n /**\n * Prevents dirty reads and non-repeatable reads.\n * Recommended for most test scenarios.\n */\n REPEATABLE_READ = 'REPEATABLE READ',\n /**\n * Highest isolation level. Prevents all phenomena.\n * May cause performance overhead in tests.\n */\n SERIALIZABLE = 'SERIALIZABLE',\n}\n\n/**\n * Abstract base class for implementing database transaction isolation in Vitest tests.\n * Provides automatic transaction rollback after each test to maintain test isolation.\n * Subclasses must implement the transact() method for their specific database driver.\n *\n * @template TConn - The database connection type\n * @template Transaction - The transaction type\n *\n * @example\n * ```typescript\n * // Implement for your database driver\n * class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {\n * async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {\n * await conn.transaction(level, fn);\n * }\n * }\n *\n * // Use in tests\n * const isolator = new MyDatabaseIsolator(test);\n * const isolatedTest = isolator.wrapVitestWithTransaction(db);\n *\n * isolatedTest('should create user', async ({ trx }) => {\n * await trx.insert('users', { name: 'Test' });\n * // Data is automatically rolled back after test\n * });\n * ```\n */\nexport abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {\n /**\n * Abstract method to create a transaction with the specified isolation level.\n * Must be implemented by subclasses for specific database drivers.\n *\n * @param conn - The database connection\n * @param isolationLevel - The transaction isolation level\n * @param fn - The function to execute within the transaction\n * @returns Promise that resolves when the transaction completes\n */\n abstract transact(\n conn: TConn,\n isolationLevel: IsolationLevel,\n fn: (trx: Transaction) => Promise<void>,\n ): Promise<void>;\n\n abstract destroy(conn: TConn): Promise<void>;\n /**\n * Creates a new VitestPostgresTransactionIsolator instance.\n *\n * @param api - The Vitest test API (usually the `test` export from vitest)\n */\n constructor(private readonly api: TestAPI) {}\n\n /**\n * Creates a wrapped version of Vitest's test API that provides transaction isolation.\n * Each test will run within a database transaction that is automatically rolled back.\n *\n * @param conn - The database connection to use\n * @param setup - Optional setup function to run within the transaction before each test\n * @param level - The transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API with transaction support\n *\n * @example\n * ```typescript\n * const isolatedTest = isolator.wrapVitestWithTransaction(db, async (trx) => {\n * // Optional setup: create common test data\n * await trx.insert('settings', { key: 'test', value: 'true' });\n * });\n *\n * isolatedTest('test with transaction', async ({ trx }) => {\n * const user = await trx.insert('users', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\n wrapVitestWithTransaction(\n createConnection: DatabaseConnection<TConn>,\n setup?: (trx: Transaction) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n ) {\n return this.api.extend<DatabaseFixtures<Transaction>>({\n // This fixture automatically provides a transaction to each test\n trx: async ({}, use: (value: Transaction) => Promise<void>) => {\n // Create a custom error class for rollback\n class TestRollback extends Error {\n constructor() {\n super('Test rollback');\n this.name = 'TestRollback';\n }\n }\n\n let testError: Error | undefined;\n const conn = await createConnection();\n try {\n await this.transact(conn, level, async (transaction) => {\n try {\n // Provide the transaction to the test\n await setup?.(transaction);\n await use(transaction);\n } catch (error) {\n // Capture any test errors\n testError = error as Error;\n }\n\n // Always throw to trigger rollback\n throw new TestRollback();\n });\n } catch (error) {\n // Only rethrow if it's not our rollback error\n if (!(error instanceof TestRollback)) {\n throw error;\n }\n\n // If the test had an error, throw it now\n if (testError) {\n throw testError;\n }\n } finally {\n await this.destroy(conn);\n }\n },\n });\n }\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\nexport type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;\n\n/**\n * Type for fixture creator functions that depend on the transaction.\n * Each function receives the transaction and returns the fixture value.\n */\nexport type FixtureCreators<\n Transaction,\n Extended extends Record<string, unknown>,\n> = {\n [K in keyof Extended]: (\n trx: Transaction,\n ) => Extended[K] | Promise<Extended[K]>;\n};\n\n/**\n * Extends a wrapped test API with additional fixtures that depend on the transaction.\n * This allows composing test context with factories, repositories, or other helpers.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestKyselyTransaction(test, db, createTestTables);\n *\n * // Extend with fixtures\n * const it = extendWithFixtures(baseTest, {\n * factory: (trx) => new KyselyFactory(builders, seeds, trx),\n * userRepo: (trx) => new UserRepository(trx),\n * });\n *\n * // Use in tests - trx and all fixtures are available\n * it('should create user with factory', async ({ trx, factory, userRepo }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\nexport function extendWithFixtures<\n Transaction,\n Extended extends Record<string, unknown>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n T extends ReturnType<TestAPI['extend']> = any,\n>(\n wrappedTest: T,\n fixtures: FixtureCreators<Transaction, Extended>,\n): T & {\n <C extends object>(\n name: string,\n fn: (\n context: DatabaseFixtures<Transaction> & Extended & C,\n ) => Promise<void>,\n ): void;\n <C extends object>(\n name: string,\n options: object,\n fn: (\n context: DatabaseFixtures<Transaction> & Extended & C,\n ) => Promise<void>,\n ): void;\n} {\n // Build fixture definitions for Vitest's extend API\n const fixtureDefinitions: Record<string, any> = {};\n\n for (const [key, creator] of Object.entries(fixtures)) {\n fixtureDefinitions[key] = async (\n { trx }: { trx: Transaction },\n use: (value: unknown) => Promise<void>,\n ) => {\n const value = await (creator as (trx: Transaction) => unknown)(trx);\n await use(value);\n };\n }\n\n return (wrappedTest as any).extend(fixtureDefinitions);\n}\n"],"mappings":";;;;;;;AAgDA,IAAY,4DAAL;;;;;AAKL;;;;;AAKA;;;;;AAKA;;;;;AAKA;;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BD,IAAsB,oCAAtB,MAA4E;;;;;;CAsB1E,YAA6BA,KAAc;EAAd;CAAgB;;;;;;;;;;;;;;;;;;;;;;;CAwB7C,0BACEC,kBACAC,OACAC,QAAwB,eAAe,iBACvC;AACA,SAAO,KAAK,IAAI,OAAsC,EAEpD,KAAK,OAAO,EAAE,EAAEC,QAA+C;GAE7D,MAAM,qBAAqB,MAAM;IAC/B,cAAc;AACZ,WAAM,gBAAgB;AACtB,UAAK,OAAO;IACb;GACF;GAED,IAAIC;GACJ,MAAM,OAAO,MAAM,kBAAkB;AACrC,OAAI;AACF,UAAM,KAAK,SAAS,MAAM,OAAO,OAAO,gBAAgB;AACtD,SAAI;AAEF,YAAM,QAAQ,YAAY;AAC1B,YAAM,IAAI,YAAY;KACvB,SAAQ,OAAO;AAEd,kBAAY;KACb;AAGD,WAAM,IAAI;IACX,EAAC;GACH,SAAQ,OAAO;AAEd,UAAM,iBAAiB,cACrB,OAAM;AAIR,QAAI,UACF,OAAM;GAET,UAAS;AACR,UAAM,KAAK,QAAQ,KAAK;GACzB;EACF,EACF,EAAC;CACH;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDD,SAAgB,mBAMdC,aACAC,UAeA;CAEA,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACnD,oBAAmB,OAAO,OACxB,EAAE,KAA2B,EAC7BC,QACG;EACH,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CACjB;AAGH,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACvD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"VitestTransactionIsolator-BKIrj3Uy.cjs","names":["api: TestAPI","createConnection: DatabaseConnection<TConn>","setup?: (trx: Transaction) => Promise<void>","level: IsolationLevel","use: (value: Transaction) => Promise<void>","testError: Error | undefined","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>","use: (value: unknown) => Promise<void>"],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":["import type { TestAPI } from 'vitest';\n\n/**\n * Type definition for test fixtures that provide transaction access.\n * Used with Vitest's test.extend() API to inject transactions into tests.\n *\n * @template Transaction - The transaction type specific to the database driver\n * @template Extended - Additional context properties provided by the extend function\n */\nexport interface DatabaseFixtures<Transaction, Extended = object> {\n /**\n * The database transaction available to the test.\n * All database operations should use this transaction to ensure proper rollback.\n */\n trx: Transaction;\n}\n\n/**\n * Combined fixtures type that merges the base transaction fixture with extended context.\n */\nexport type ExtendedDatabaseFixtures<\n Transaction,\n Extended = object,\n> = DatabaseFixtures<Transaction> & Extended;\n\n/**\n * Function type for extending test context with additional properties.\n * Receives the transaction and returns additional context to be merged with { trx }.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n *\n * @example\n * ```typescript\n * const extendContext: ExtendContextFn<Transaction<DB>, { factory: KyselyFactory }> =\n * (trx) => ({ factory: new KyselyFactory(builders, seeds, trx) });\n * ```\n */\nexport type ExtendContextFn<Transaction, Extended> = (\n trx: Transaction,\n) => Extended | Promise<Extended>;\n\n/**\n * PostgreSQL transaction isolation levels.\n * Controls the visibility of concurrent transactions.\n *\n * @see https://www.postgresql.org/docs/current/transaction-iso.html\n */\nexport enum IsolationLevel {\n /**\n * Lowest isolation level. Allows dirty reads.\n * Not recommended for testing.\n */\n READ_UNCOMMITTED = 'READ UNCOMMITTED',\n /**\n * Default PostgreSQL isolation level.\n * Prevents dirty reads but allows non-repeatable reads.\n */\n READ_COMMITTED = 'READ COMMITTED',\n /**\n * Prevents dirty reads and non-repeatable reads.\n * Recommended for most test scenarios.\n */\n REPEATABLE_READ = 'REPEATABLE READ',\n /**\n * Highest isolation level. Prevents all phenomena.\n * May cause performance overhead in tests.\n */\n SERIALIZABLE = 'SERIALIZABLE',\n}\n\n/**\n * Abstract base class for implementing database transaction isolation in Vitest tests.\n * Provides automatic transaction rollback after each test to maintain test isolation.\n * Subclasses must implement the transact() method for their specific database driver.\n *\n * @template TConn - The database connection type\n * @template Transaction - The transaction type\n *\n * @example\n * ```typescript\n * // Implement for your database driver\n * class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {\n * async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {\n * await conn.transaction(level, fn);\n * }\n * }\n *\n * // Use in tests\n * const isolator = new MyDatabaseIsolator(test);\n * const isolatedTest = isolator.wrapVitestWithTransaction(db);\n *\n * isolatedTest('should create user', async ({ trx }) => {\n * await trx.insert('users', { name: 'Test' });\n * // Data is automatically rolled back after test\n * });\n * ```\n */\nexport abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {\n /**\n * Abstract method to create a transaction with the specified isolation level.\n * Must be implemented by subclasses for specific database drivers.\n *\n * @param conn - The database connection\n * @param isolationLevel - The transaction isolation level\n * @param fn - The function to execute within the transaction\n * @returns Promise that resolves when the transaction completes\n */\n abstract transact(\n conn: TConn,\n isolationLevel: IsolationLevel,\n fn: (trx: Transaction) => Promise<void>,\n ): Promise<void>;\n\n abstract destroy(conn: TConn): Promise<void>;\n /**\n * Creates a new VitestPostgresTransactionIsolator instance.\n *\n * @param api - The Vitest test API (usually the `test` export from vitest)\n */\n constructor(private readonly api: TestAPI) {}\n\n /**\n * Creates a wrapped version of Vitest's test API that provides transaction isolation.\n * Each test will run within a database transaction that is automatically rolled back.\n *\n * @param conn - The database connection to use\n * @param setup - Optional setup function to run within the transaction before each test\n * @param level - The transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API with transaction support\n *\n * @example\n * ```typescript\n * const isolatedTest = isolator.wrapVitestWithTransaction(db, async (trx) => {\n * // Optional setup: create common test data\n * await trx.insert('settings', { key: 'test', value: 'true' });\n * });\n *\n * isolatedTest('test with transaction', async ({ trx }) => {\n * const user = await trx.insert('users', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\n wrapVitestWithTransaction(\n createConnection: DatabaseConnection<TConn>,\n setup?: (trx: Transaction) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n ) {\n return this.api.extend<DatabaseFixtures<Transaction>>({\n // This fixture automatically provides a transaction to each test\n trx: async ({}, use: (value: Transaction) => Promise<void>) => {\n // Create a custom error class for rollback\n class TestRollback extends Error {\n constructor() {\n super('Test rollback');\n this.name = 'TestRollback';\n }\n }\n\n let testError: Error | undefined;\n const conn = await createConnection();\n try {\n await this.transact(conn, level, async (transaction) => {\n try {\n // Provide the transaction to the test\n await setup?.(transaction);\n await use(transaction);\n } catch (error) {\n // Capture any test errors\n testError = error as Error;\n }\n\n // Always throw to trigger rollback\n throw new TestRollback();\n });\n } catch (error) {\n // Only rethrow if it's not our rollback error\n if (!(error instanceof TestRollback)) {\n throw error;\n }\n\n // If the test had an error, throw it now\n if (testError) {\n throw testError;\n }\n } finally {\n await this.destroy(conn);\n }\n },\n });\n }\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\nexport type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;\n\n/**\n * Type for fixture creator functions that depend on the transaction.\n * Each function receives the transaction and returns the fixture value.\n */\nexport type FixtureCreators<\n Transaction,\n Extended extends Record<string, unknown>,\n> = {\n [K in keyof Extended]: (\n trx: Transaction,\n ) => Extended[K] | Promise<Extended[K]>;\n};\n\n/**\n * Extends a wrapped test API with additional fixtures that depend on the transaction.\n * This allows composing test context with factories, repositories, or other helpers.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestKyselyTransaction(test, db, createTestTables);\n *\n * // Extend with fixtures\n * const it = extendWithFixtures(baseTest, {\n * factory: (trx) => new KyselyFactory(builders, seeds, trx),\n * userRepo: (trx) => new UserRepository(trx),\n * });\n *\n * // Use in tests - trx and all fixtures are available\n * it('should create user with factory', async ({ trx, factory, userRepo }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\nexport function extendWithFixtures<\n Transaction,\n Extended extends Record<string, unknown>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n T extends ReturnType<TestAPI['extend']> = any,\n>(\n wrappedTest: T,\n fixtures: FixtureCreators<Transaction, Extended>,\n): T & {\n <C extends object>(\n name: string,\n fn: (\n context: DatabaseFixtures<Transaction> & Extended & C,\n ) => Promise<void>,\n ): void;\n <C extends object>(\n name: string,\n options: object,\n fn: (\n context: DatabaseFixtures<Transaction> & Extended & C,\n ) => Promise<void>,\n ): void;\n} {\n // Build fixture definitions for Vitest's extend API\n const fixtureDefinitions: Record<string, any> = {};\n\n for (const [key, creator] of Object.entries(fixtures)) {\n fixtureDefinitions[key] = async (\n { trx }: { trx: Transaction },\n use: (value: unknown) => Promise<void>,\n ) => {\n const value = await (creator as (trx: Transaction) => unknown)(trx);\n await use(value);\n };\n }\n\n return (wrappedTest as any).extend(fixtureDefinitions);\n}\n"],"mappings":";;;;;;;;AAgDA,IAAY,4DAAL;;;;;AAKL;;;;;AAKA;;;;;AAKA;;;;;AAKA;;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BD,IAAsB,oCAAtB,MAA4E;;;;;;CAsB1E,YAA6BA,KAAc;EAAd;CAAgB;;;;;;;;;;;;;;;;;;;;;;;CAwB7C,0BACEC,kBACAC,OACAC,QAAwB,eAAe,iBACvC;AACA,SAAO,KAAK,IAAI,OAAsC,EAEpD,KAAK,OAAO,EAAE,EAAEC,QAA+C;GAE7D,MAAM,qBAAqB,MAAM;IAC/B,cAAc;AACZ,WAAM,gBAAgB;AACtB,UAAK,OAAO;IACb;GACF;GAED,IAAIC;GACJ,MAAM,OAAO,MAAM,kBAAkB;AACrC,OAAI;AACF,UAAM,KAAK,SAAS,MAAM,OAAO,OAAO,gBAAgB;AACtD,SAAI;AAEF,YAAM,QAAQ,YAAY;AAC1B,YAAM,IAAI,YAAY;KACvB,SAAQ,OAAO;AAEd,kBAAY;KACb;AAGD,WAAM,IAAI;IACX,EAAC;GACH,SAAQ,OAAO;AAEd,UAAM,iBAAiB,cACrB,OAAM;AAIR,QAAI,UACF,OAAM;GAET,UAAS;AACR,UAAM,KAAK,QAAQ,KAAK;GACzB;EACF,EACF,EAAC;CACH;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDD,SAAgB,mBAMdC,aACAC,UAeA;CAEA,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACnD,oBAAmB,OAAO,OACxB,EAAE,KAA2B,EAC7BC,QACG;EACH,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CACjB;AAGH,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACvD"}