@geekmidas/testkit 0.1.1 → 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 (76) hide show
  1. package/dist/{Factory-DE3hE0WO.d.mts → Factory-Cmr3s3-s.d.mts} +2 -2
  2. package/dist/{Factory-pNV7ZQ7-.d.cts → Factory-D7oHtI2D.d.cts} +2 -2
  3. package/dist/Factory.d.cts +2 -2
  4. package/dist/Factory.d.mts +2 -2
  5. package/dist/{KyselyFactory-CPZTUuMB.d.cts → KyselyFactory-BTdygZ-i.d.mts} +3 -3
  6. package/dist/{KyselyFactory-RAyvZ8Cj.d.mts → KyselyFactory-dYdviox2.d.cts} +3 -3
  7. package/dist/KyselyFactory.d.cts +3 -3
  8. package/dist/KyselyFactory.d.mts +3 -3
  9. package/dist/{ObjectionFactory-BlBicEia.d.mts → ObjectionFactory-0gUdx8bB.d.cts} +3 -3
  10. package/dist/{ObjectionFactory-BwpN4gMX.d.cts → ObjectionFactory-BagGjikT.d.mts} +3 -3
  11. package/dist/ObjectionFactory.d.cts +3 -3
  12. package/dist/ObjectionFactory.d.mts +3 -3
  13. package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs → VitestKyselyTransactionIsolator-CNURW8y6.mjs} +2 -2
  14. package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map → VitestKyselyTransactionIsolator-CNURW8y6.mjs.map} +1 -1
  15. package/dist/{VitestKyselyTransactionIsolator-BqrZDeaT.d.cts → VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts} +2 -2
  16. package/dist/{VitestKyselyTransactionIsolator-DiskaURs.d.mts → VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts} +2 -2
  17. package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs → VitestKyselyTransactionIsolator-EvDLk5zg.cjs} +2 -2
  18. package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map → VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map} +1 -1
  19. package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
  20. package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
  21. package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
  22. package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
  23. package/dist/{VitestObjectionTransactionIsolator-CD2ucJpH.d.cts → VitestObjectionTransactionIsolator-1TpsPqfG.d.cts} +2 -2
  24. package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs → VitestObjectionTransactionIsolator-CM5KTAFA.cjs} +2 -2
  25. package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map → VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map} +1 -1
  26. package/dist/{VitestObjectionTransactionIsolator-DhQ8XGva.d.mts → VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts} +2 -2
  27. package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs → VitestObjectionTransactionIsolator-jQFaCz0u.mjs} +2 -2
  28. package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map → VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map} +1 -1
  29. package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
  30. package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
  31. package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
  32. package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
  33. package/dist/{VitestTransactionIsolator-DLdQlfZ5.d.mts → VitestTransactionIsolator-BvR19bYn.d.mts} +31 -14
  34. package/dist/{VitestTransactionIsolator-BKIrj3Uy.cjs → VitestTransactionIsolator-CMfJXZP8.cjs} +52 -36
  35. package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -0
  36. package/dist/{VitestTransactionIsolator-DfA80g2M.d.cts → VitestTransactionIsolator-CwQaxZLP.d.cts} +31 -14
  37. package/dist/{VitestTransactionIsolator-BIaMs4c2.mjs → VitestTransactionIsolator-DQ7tLqgV.mjs} +52 -36
  38. package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -0
  39. package/dist/VitestTransactionIsolator.cjs +1 -1
  40. package/dist/VitestTransactionIsolator.d.cts +2 -2
  41. package/dist/VitestTransactionIsolator.d.mts +2 -2
  42. package/dist/VitestTransactionIsolator.mjs +1 -1
  43. package/dist/better-auth.d.cts +2 -2
  44. package/dist/better-auth.d.mts +2 -2
  45. package/dist/{directory-Mi7tdOuD.d.cts → directory-BUcnztHI.d.cts} +3 -3
  46. package/dist/{directory-Q178x53k.d.mts → directory-BXavAeJZ.d.mts} +3 -3
  47. package/dist/{faker-D9gz7KjY.d.mts → faker-DHh7xs4u.d.mts} +3 -3
  48. package/dist/{faker-BSH1EMtg.d.cts → faker-Dg3trU4a.d.cts} +3 -3
  49. package/dist/faker.d.cts +1 -1
  50. package/dist/faker.d.mts +1 -1
  51. package/dist/kysely.cjs +23 -29
  52. package/dist/kysely.cjs.map +1 -1
  53. package/dist/kysely.d.cts +39 -34
  54. package/dist/kysely.d.mts +39 -34
  55. package/dist/kysely.mjs +23 -29
  56. package/dist/kysely.mjs.map +1 -1
  57. package/dist/objection.cjs +23 -47
  58. package/dist/objection.cjs.map +1 -1
  59. package/dist/objection.d.cts +39 -52
  60. package/dist/objection.d.mts +39 -52
  61. package/dist/objection.mjs +23 -47
  62. package/dist/objection.mjs.map +1 -1
  63. package/dist/os/directory.d.cts +1 -1
  64. package/dist/os/directory.d.mts +1 -1
  65. package/dist/os/index.d.cts +1 -1
  66. package/dist/os/index.d.mts +1 -1
  67. package/package.json +1 -1
  68. package/src/VitestTransactionIsolator.ts +67 -16
  69. package/src/__tests__/KyselyFactory.spec.ts +4 -5
  70. package/src/__tests__/ObjectionFactory.spec.ts +4 -5
  71. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +6 -3
  72. package/src/__tests__/integration.spec.ts +8 -10
  73. package/src/kysely.ts +43 -33
  74. package/src/objection.ts +41 -51
  75. package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +0 -1
  76. package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +0 -1
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
 
@@ -22,8 +22,26 @@ export type {
22
22
  ExtendedDatabaseFixtures,
23
23
  FixtureCreators,
24
24
  TestWithExtendedFixtures,
25
+ TransactionWrapperOptions,
25
26
  } from './VitestTransactionIsolator';
26
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
+ }
44
+
27
45
  // Re-export faker and FakerFactory for type portability in declaration files
28
46
  export { faker, type FakerFactory } from './faker';
29
47
 
@@ -33,10 +51,9 @@ export { faker, type FakerFactory } from './faker';
33
51
  * This ensures tests don't affect each other's data and run faster than truncating tables.
34
52
  *
35
53
  * @template Database - The database schema type
54
+ * @template Extended - Additional fixtures to provide
36
55
  * @param api - The Vitest test API (usually `test` from vitest)
37
- * @param db - The Kysely database instance
38
- * @param setup - Optional setup function to run before each test in the transaction
39
- * @param level - Transaction isolation level (defaults to REPEATABLE_READ)
56
+ * @param options - Configuration options for transaction wrapping
40
57
  * @returns A wrapped test API that provides transaction isolation
41
58
  *
42
59
  * @example
@@ -46,7 +63,9 @@ export { faker, type FakerFactory } from './faker';
46
63
  * import { db } from './database';
47
64
  *
48
65
  * // Create isolated test with automatic rollback
49
- * const isolatedTest = wrapVitestKyselyTransaction(test, db);
66
+ * const isolatedTest = wrapVitestKyselyTransaction(test, {
67
+ * connection: db,
68
+ * });
50
69
  *
51
70
  * // Use in tests - each test gets its own transaction
52
71
  * isolatedTest('should create user', async ({ trx }) => {
@@ -57,41 +76,29 @@ export { faker, type FakerFactory } from './faker';
57
76
  * .executeTakeFirst();
58
77
  *
59
78
  * expect(user).toBeDefined();
60
- * // User is automatically rolled back after test
61
79
  * });
62
80
  *
63
- * // With setup function for common test data
64
- * const testWithSetup = wrapVitestKyselyTransaction(
65
- * test,
66
- * db,
67
- * async (trx) => {
68
- * // Create common test data
69
- * await trx.insertInto('settings')
70
- * .values({ key: 'test_mode', value: 'true' })
71
- * .execute();
72
- * }
73
- * );
74
- *
75
- * testWithSetup('should have test settings', async ({ trx }) => {
76
- * const setting = await trx
77
- * .selectFrom('settings')
78
- * .where('key', '=', 'test_mode')
79
- * .selectAll()
80
- * .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
+ * });
81
88
  *
82
- * 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();
83
92
  * });
84
93
  * ```
85
94
  */
86
- export function wrapVitestKyselyTransaction<Database>(
87
- api: TestAPI,
88
- connection: DatabaseConnection<Kysely<Database>>,
89
- setup?: (trx: Transaction<Database>) => Promise<void>,
90
- level: IsolationLevel = IsolationLevel.REPEATABLE_READ,
91
- ) {
95
+ export function wrapVitestKyselyTransaction<
96
+ Database,
97
+ Extended extends Record<string, unknown> = {},
98
+ >(api: TestAPI, options: KyselyTransactionOptions<Database, Extended>) {
92
99
  const wrapper = new VitestKyselyTransactionIsolator<Database>(api);
93
100
 
94
- return wrapper.wrapVitestWithTransaction(connection, setup, level);
101
+ return wrapper.wrapVitestWithTransaction(options);
95
102
  }
96
103
 
97
104
  /**
@@ -118,7 +125,10 @@ export function wrapVitestKyselyTransaction<Database>(
118
125
  * };
119
126
  *
120
127
  * // Create base wrapped test
121
- * const baseTest = wrapVitestKyselyTransaction<DB>(test, db, createTestTables);
128
+ * const baseTest = wrapVitestKyselyTransaction<DB>(test, {
129
+ * connection: db,
130
+ * setup: createTestTables,
131
+ * });
122
132
  *
123
133
  * // Extend with fixtures - each fixture receives the transaction
124
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
 
@@ -23,8 +23,25 @@ export type {
23
23
  ExtendedDatabaseFixtures,
24
24
  FixtureCreators,
25
25
  TestWithExtendedFixtures,
26
+ TransactionWrapperOptions,
26
27
  } from './VitestTransactionIsolator';
27
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
+ }
44
+
28
45
  // Re-export faker and FakerFactory for type portability in declaration files
29
46
  export { faker, type FakerFactory } from './faker';
30
47
 
@@ -33,10 +50,9 @@ export { faker, type FakerFactory } from './faker';
33
50
  * Each test runs in an isolated database transaction that is rolled back after completion.
34
51
  * This ensures tests don't affect each other's data and run faster than truncating tables.
35
52
  *
53
+ * @template Extended - Additional fixtures to provide
36
54
  * @param api - The Vitest test API (usually `test` from vitest)
37
- * @param conn - The Knex database connection instance
38
- * @param setup - Optional setup function to run before each test in the transaction
39
- * @param level - Transaction isolation level (defaults to REPEATABLE_READ)
55
+ * @param options - Configuration options for transaction wrapping
40
56
  * @returns A wrapped test API that provides transaction isolation
41
57
  *
42
58
  * @example
@@ -44,10 +60,12 @@ export { faker, type FakerFactory } from './faker';
44
60
  * import { test } from 'vitest';
45
61
  * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
46
62
  * import { knex } from './database';
47
- * import { User, Post } from './models';
63
+ * import { User } from './models';
48
64
  *
49
65
  * // Create isolated test with automatic rollback
50
- * const isolatedTest = wrapVitestObjectionTransaction(test, knex);
66
+ * const isolatedTest = wrapVitestObjectionTransaction(test, {
67
+ * connection: knex,
68
+ * });
51
69
  *
52
70
  * // Use in tests - each test gets its own transaction
53
71
  * isolatedTest('should create user', async ({ trx }) => {
@@ -55,59 +73,28 @@ export { faker, type FakerFactory } from './faker';
55
73
  * .insert({ name: 'Test User', email: 'test@example.com' });
56
74
  *
57
75
  * expect(user).toBeDefined();
58
- * // User is automatically rolled back after test
59
76
  * });
60
77
  *
61
- * // With setup function for common test data
62
- * const testWithSetup = wrapVitestObjectionTransaction(
63
- * test,
64
- * knex,
65
- * async (trx) => {
66
- * // Create common test data
67
- * await knex('settings')
68
- * .transacting(trx)
69
- * .insert({ key: 'test_mode', value: 'true' });
70
- * }
71
- * );
72
- *
73
- * testWithSetup('should have test settings', async ({ trx }) => {
74
- * const setting = await knex('settings')
75
- * .transacting(trx)
76
- * .where('key', 'test_mode')
77
- * .first();
78
- *
79
- * 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
+ * },
80
84
  * });
81
85
  *
82
- * // Example with factory and transaction
83
- * const isolatedTest = wrapVitestObjectionTransaction(test, knex);
84
- * const factory = new ObjectionFactory(builders, seeds, knex);
85
- *
86
- * isolatedTest('creates related data', async ({ trx }) => {
87
- * // Factory can use the transaction
88
- * const user = await User.query(trx).insert({ name: 'Author' });
89
- * const posts = await Post.query(trx).insert([
90
- * { title: 'Post 1', userId: user.id },
91
- * { title: 'Post 2', userId: user.id }
92
- * ]);
93
- *
94
- * const userWithPosts = await User.query(trx)
95
- * .findById(user.id)
96
- * .withGraphFetched('posts');
97
- *
98
- * 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();
99
89
  * });
100
90
  * ```
101
91
  */
102
- export function wrapVitestObjectionTransaction(
103
- api: TestAPI,
104
- conn: DatabaseConnection<Knex>,
105
- setup?: (trx: Knex.Transaction) => Promise<void>,
106
- level: IsolationLevel = IsolationLevel.REPEATABLE_READ,
107
- ) {
92
+ export function wrapVitestObjectionTransaction<
93
+ Extended extends Record<string, unknown> = {},
94
+ >(api: TestAPI, options: ObjectionTransactionOptions<Extended>) {
108
95
  const wrapper = new VitestObjectionTransactionIsolator(api);
109
96
 
110
- return wrapper.wrapVitestWithTransaction(conn, setup, level);
97
+ return wrapper.wrapVitestWithTransaction(options);
111
98
  }
112
99
 
113
100
  /**
@@ -134,7 +121,10 @@ export function wrapVitestObjectionTransaction(
134
121
  * };
135
122
  *
136
123
  * // Create base wrapped test
137
- * const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
124
+ * const baseTest = wrapVitestObjectionTransaction(test, {
125
+ * connection: knex,
126
+ * setup: createTestTables,
127
+ * });
138
128
  *
139
129
  * // Extend with fixtures - each fixture receives the transaction
140
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 * The test API returned by extendWithFixtures.\n * Provides access to both the transaction (trx) and all extended fixtures.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional fixtures provided\n * @template BaseTest - The base wrapped test type\n */\nexport type TestWithExtendedFixtures<\n Transaction,\n Extended extends Record<string, unknown>,\n BaseTest extends ReturnType<TestAPI['extend']> = ReturnType<\n TestAPI['extend']\n >,\n> = BaseTest & {\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\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): TestWithExtendedFixtures<Transaction, Extended, T> {\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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8ED,SAAgB,mBAMdC,aACAC,UACoD;CAEpD,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 * The test API returned by extendWithFixtures.\n * Provides access to both the transaction (trx) and all extended fixtures.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional fixtures provided\n * @template BaseTest - The base wrapped test type\n */\nexport type TestWithExtendedFixtures<\n Transaction,\n Extended extends Record<string, unknown>,\n BaseTest extends ReturnType<TestAPI['extend']> = ReturnType<\n TestAPI['extend']\n >,\n> = BaseTest & {\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\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): TestWithExtendedFixtures<Transaction, Extended, T> {\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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8ED,SAAgB,mBAMdC,aACAC,UACoD;CAEpD,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"}