@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.
- package/dist/{Factory-DE3hE0WO.d.mts → Factory-Cmr3s3-s.d.mts} +2 -2
- package/dist/{Factory-pNV7ZQ7-.d.cts → Factory-D7oHtI2D.d.cts} +2 -2
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-CPZTUuMB.d.cts → KyselyFactory-BTdygZ-i.d.mts} +3 -3
- package/dist/{KyselyFactory-RAyvZ8Cj.d.mts → KyselyFactory-dYdviox2.d.cts} +3 -3
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/{ObjectionFactory-BlBicEia.d.mts → ObjectionFactory-0gUdx8bB.d.cts} +3 -3
- package/dist/{ObjectionFactory-BwpN4gMX.d.cts → ObjectionFactory-BagGjikT.d.mts} +3 -3
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs → VitestKyselyTransactionIsolator-CNURW8y6.mjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map → VitestKyselyTransactionIsolator-CNURW8y6.mjs.map} +1 -1
- package/dist/{VitestKyselyTransactionIsolator-BqrZDeaT.d.cts → VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DiskaURs.d.mts → VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs → VitestKyselyTransactionIsolator-EvDLk5zg.cjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map → VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map} +1 -1
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/{VitestObjectionTransactionIsolator-CD2ucJpH.d.cts → VitestObjectionTransactionIsolator-1TpsPqfG.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs → VitestObjectionTransactionIsolator-CM5KTAFA.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map → VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-DhQ8XGva.d.mts → VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs → VitestObjectionTransactionIsolator-jQFaCz0u.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map → VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map} +1 -1
- package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
- package/dist/{VitestTransactionIsolator-DLdQlfZ5.d.mts → VitestTransactionIsolator-BvR19bYn.d.mts} +31 -14
- package/dist/{VitestTransactionIsolator-BKIrj3Uy.cjs → VitestTransactionIsolator-CMfJXZP8.cjs} +52 -36
- package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-DfA80g2M.d.cts → VitestTransactionIsolator-CwQaxZLP.d.cts} +31 -14
- package/dist/{VitestTransactionIsolator-BIaMs4c2.mjs → VitestTransactionIsolator-DQ7tLqgV.mjs} +52 -36
- package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.mts +2 -2
- package/dist/{directory-Mi7tdOuD.d.cts → directory-BUcnztHI.d.cts} +3 -3
- package/dist/{directory-Q178x53k.d.mts → directory-BXavAeJZ.d.mts} +3 -3
- package/dist/{faker-D9gz7KjY.d.mts → faker-DHh7xs4u.d.mts} +3 -3
- package/dist/{faker-BSH1EMtg.d.cts → faker-Dg3trU4a.d.cts} +3 -3
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/kysely.cjs +23 -29
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +39 -34
- package/dist/kysely.d.mts +39 -34
- package/dist/kysely.mjs +23 -29
- package/dist/kysely.mjs.map +1 -1
- package/dist/objection.cjs +23 -47
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +39 -52
- package/dist/objection.d.mts +39 -52
- package/dist/objection.mjs +23 -47
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/directory.d.mts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/package.json +1 -1
- package/src/VitestTransactionIsolator.ts +67 -16
- package/src/__tests__/KyselyFactory.spec.ts +4 -5
- package/src/__tests__/ObjectionFactory.spec.ts +4 -5
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +6 -3
- package/src/__tests__/integration.spec.ts +8 -10
- package/src/kysely.ts +43 -33
- package/src/objection.ts +41 -51
- package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +0 -1
package/dist/{VitestTransactionIsolator-DfA80g2M.d.cts → VitestTransactionIsolator-CwQaxZLP.d.cts}
RENAMED
|
@@ -112,30 +112,44 @@ declare abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
|
|
|
112
112
|
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
113
113
|
* Each test will run within a database transaction that is automatically rolled back.
|
|
114
114
|
*
|
|
115
|
-
* @param
|
|
116
|
-
* @param setup - Optional setup function to run within the transaction before each test
|
|
117
|
-
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
115
|
+
* @param options - Configuration options for transaction wrapping
|
|
118
116
|
* @returns A wrapped test API with transaction support
|
|
119
117
|
*
|
|
120
118
|
* @example
|
|
121
119
|
* ```typescript
|
|
122
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction(
|
|
123
|
-
*
|
|
124
|
-
*
|
|
120
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction({
|
|
121
|
+
* connection: db,
|
|
122
|
+
* setup: async (trx) => {
|
|
123
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
124
|
+
* },
|
|
125
|
+
* fixtures: {
|
|
126
|
+
* factory: (trx) => new Factory(trx),
|
|
127
|
+
* },
|
|
125
128
|
* });
|
|
126
129
|
*
|
|
127
|
-
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
128
|
-
* const user = await
|
|
130
|
+
* isolatedTest('test with transaction', async ({ trx, factory }) => {
|
|
131
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
129
132
|
* expect(user).toBeDefined();
|
|
130
133
|
* });
|
|
131
134
|
* ```
|
|
132
135
|
*/
|
|
133
|
-
wrapVitestWithTransaction
|
|
134
|
-
trx: Transaction;
|
|
135
|
-
}>;
|
|
136
|
+
wrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(options: TransactionWrapperOptions<TConn, Transaction, Extended>): TestAPI<DatabaseFixtures<Transaction, object> & Extended>;
|
|
136
137
|
}
|
|
137
138
|
type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;
|
|
138
139
|
type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;
|
|
140
|
+
/**
|
|
141
|
+
* Options for wrapping Vitest tests with database transaction isolation.
|
|
142
|
+
*/
|
|
143
|
+
interface TransactionWrapperOptions<TConn, Transaction, Extended extends Record<string, unknown> = {}> {
|
|
144
|
+
/** Function that creates or returns a database connection */
|
|
145
|
+
connection: DatabaseConnection<TConn>;
|
|
146
|
+
/** Optional setup function to run within the transaction before each test */
|
|
147
|
+
setup?: (trx: Transaction) => Promise<void>;
|
|
148
|
+
/** Transaction isolation level (defaults to REPEATABLE_READ) */
|
|
149
|
+
isolationLevel?: IsolationLevel;
|
|
150
|
+
/** Additional fixtures that depend on the transaction */
|
|
151
|
+
fixtures?: FixtureCreators<Transaction, Extended>;
|
|
152
|
+
}
|
|
139
153
|
/**
|
|
140
154
|
* Type for fixture creator functions that depend on the transaction.
|
|
141
155
|
* Each function receives the transaction and returns the fixture value.
|
|
@@ -168,7 +182,10 @@ type TestWithExtendedFixtures<Transaction, Extended extends Record<string, unkno
|
|
|
168
182
|
* import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
|
|
169
183
|
*
|
|
170
184
|
* // Create base wrapped test
|
|
171
|
-
* const baseTest = wrapVitestKyselyTransaction(test,
|
|
185
|
+
* const baseTest = wrapVitestKyselyTransaction(test, {
|
|
186
|
+
* connection: db,
|
|
187
|
+
* setup: createTestTables,
|
|
188
|
+
* });
|
|
172
189
|
*
|
|
173
190
|
* // Extend with fixtures
|
|
174
191
|
* const it = extendWithFixtures(baseTest, {
|
|
@@ -185,5 +202,5 @@ type TestWithExtendedFixtures<Transaction, Extended extends Record<string, unkno
|
|
|
185
202
|
*/
|
|
186
203
|
declare function extendWithFixtures<Transaction, Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = any>(wrappedTest: T, fixtures: FixtureCreators<Transaction, Extended>): TestWithExtendedFixtures<Transaction, Extended, T>;
|
|
187
204
|
//#endregion
|
|
188
|
-
export { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
189
|
-
//# sourceMappingURL=VitestTransactionIsolator-
|
|
205
|
+
export { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
206
|
+
//# sourceMappingURL=VitestTransactionIsolator-CwQaxZLP.d.cts.map
|
package/dist/{VitestTransactionIsolator-BIaMs4c2.mjs → VitestTransactionIsolator-DQ7tLqgV.mjs}
RENAMED
|
@@ -68,51 +68,64 @@ var VitestPostgresTransactionIsolator = class {
|
|
|
68
68
|
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
69
69
|
* Each test will run within a database transaction that is automatically rolled back.
|
|
70
70
|
*
|
|
71
|
-
* @param
|
|
72
|
-
* @param setup - Optional setup function to run within the transaction before each test
|
|
73
|
-
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
71
|
+
* @param options - Configuration options for transaction wrapping
|
|
74
72
|
* @returns A wrapped test API with transaction support
|
|
75
73
|
*
|
|
76
74
|
* @example
|
|
77
75
|
* ```typescript
|
|
78
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction(
|
|
79
|
-
*
|
|
80
|
-
*
|
|
76
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction({
|
|
77
|
+
* connection: db,
|
|
78
|
+
* setup: async (trx) => {
|
|
79
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
80
|
+
* },
|
|
81
|
+
* fixtures: {
|
|
82
|
+
* factory: (trx) => new Factory(trx),
|
|
83
|
+
* },
|
|
81
84
|
* });
|
|
82
85
|
*
|
|
83
|
-
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
84
|
-
* const user = await
|
|
86
|
+
* isolatedTest('test with transaction', async ({ trx, factory }) => {
|
|
87
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
85
88
|
* expect(user).toBeDefined();
|
|
86
89
|
* });
|
|
87
90
|
* ```
|
|
88
91
|
*/
|
|
89
|
-
wrapVitestWithTransaction(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
await use(transaction);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
testError = error;
|
|
92
|
+
wrapVitestWithTransaction(options) {
|
|
93
|
+
const { connection, setup, isolationLevel = IsolationLevel.REPEATABLE_READ, fixtures } = options;
|
|
94
|
+
const additionalFixtures = {};
|
|
95
|
+
if (fixtures) for (const [key, creator] of Object.entries(fixtures)) additionalFixtures[key] = async ({ trx }, use) => {
|
|
96
|
+
const value = await creator(trx);
|
|
97
|
+
await use(value);
|
|
98
|
+
};
|
|
99
|
+
const extendFn = this.api.extend;
|
|
100
|
+
return extendFn({
|
|
101
|
+
trx: async ({}, use) => {
|
|
102
|
+
class TestRollback extends Error {
|
|
103
|
+
constructor() {
|
|
104
|
+
super("Test rollback");
|
|
105
|
+
this.name = "TestRollback";
|
|
106
106
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
107
|
+
}
|
|
108
|
+
let testError;
|
|
109
|
+
const conn = await connection();
|
|
110
|
+
try {
|
|
111
|
+
await this.transact(conn, isolationLevel, async (transaction) => {
|
|
112
|
+
try {
|
|
113
|
+
await setup?.(transaction);
|
|
114
|
+
await use(transaction);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
testError = error;
|
|
117
|
+
}
|
|
118
|
+
throw new TestRollback();
|
|
119
|
+
});
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if (!(error instanceof TestRollback)) throw error;
|
|
122
|
+
if (testError) throw testError;
|
|
123
|
+
} finally {
|
|
124
|
+
await this.destroy(conn);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
...additionalFixtures
|
|
128
|
+
});
|
|
116
129
|
}
|
|
117
130
|
};
|
|
118
131
|
/**
|
|
@@ -130,7 +143,10 @@ var VitestPostgresTransactionIsolator = class {
|
|
|
130
143
|
* import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
|
|
131
144
|
*
|
|
132
145
|
* // Create base wrapped test
|
|
133
|
-
* const baseTest = wrapVitestKyselyTransaction(test,
|
|
146
|
+
* const baseTest = wrapVitestKyselyTransaction(test, {
|
|
147
|
+
* connection: db,
|
|
148
|
+
* setup: createTestTables,
|
|
149
|
+
* });
|
|
134
150
|
*
|
|
135
151
|
* // Extend with fixtures
|
|
136
152
|
* const it = extendWithFixtures(baseTest, {
|
|
@@ -156,4 +172,4 @@ function extendWithFixtures(wrappedTest, fixtures) {
|
|
|
156
172
|
|
|
157
173
|
//#endregion
|
|
158
174
|
export { IsolationLevel, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
159
|
-
//# sourceMappingURL=VitestTransactionIsolator-
|
|
175
|
+
//# sourceMappingURL=VitestTransactionIsolator-DQ7tLqgV.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VitestTransactionIsolator-DQ7tLqgV.mjs","names":["api: TestAPI","options: TransactionWrapperOptions<TConn, Transaction, Extended>","additionalFixtures: Record<string, unknown>","use: (value: unknown) => Promise<void>","use: (value: Transaction) => Promise<void>","testError: Error | undefined","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>"],"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 options - Configuration options for transaction wrapping\n * @returns A wrapped test API with transaction support\n *\n * @example\n * ```typescript\n * const isolatedTest = isolator.wrapVitestWithTransaction({\n * connection: db,\n * setup: async (trx) => {\n * await trx.insert('settings', { key: 'test', value: 'true' });\n * },\n * fixtures: {\n * factory: (trx) => new Factory(trx),\n * },\n * });\n *\n * isolatedTest('test with transaction', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\n wrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(\n options: TransactionWrapperOptions<TConn, Transaction, Extended>,\n ) {\n const {\n connection,\n setup,\n isolationLevel = IsolationLevel.REPEATABLE_READ,\n fixtures,\n } = options;\n\n // Build fixture definitions for additional fixtures that depend on trx\n const additionalFixtures: Record<string, unknown> = {};\n if (fixtures) {\n for (const [key, creator] of Object.entries(fixtures)) {\n additionalFixtures[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\n type CombinedFixtures = DatabaseFixtures<Transaction> & Extended;\n\n // Cast to bypass Vitest's strict fixture typing which can't infer\n // dynamically built fixture objects\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const extendFn = this.api.extend as <T>(fixtures: any) => TestAPI<T>;\n\n return extendFn<CombinedFixtures>({\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 connection();\n try {\n await this.transact(conn, isolationLevel, 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 ...additionalFixtures,\n });\n }\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\nexport type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;\n\n/**\n * Options for wrapping Vitest tests with database transaction isolation.\n */\nexport interface TransactionWrapperOptions<\n TConn,\n Transaction,\n Extended extends Record<string, unknown> = {},\n> {\n /** Function that creates or returns a database connection */\n connection: DatabaseConnection<TConn>;\n /** Optional setup function to run within the transaction before each test */\n setup?: (trx: Transaction) => Promise<void>;\n /** Transaction isolation level (defaults to REPEATABLE_READ) */\n isolationLevel?: IsolationLevel;\n /** Additional fixtures that depend on the transaction */\n fixtures?: FixtureCreators<Transaction, Extended>;\n}\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, {\n * connection: db,\n * setup: createTestTables,\n * });\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;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B7C,0BACEC,SACA;EACA,MAAM,EACJ,YACA,OACA,iBAAiB,eAAe,iBAChC,UACD,GAAG;EAGJ,MAAMC,qBAA8C,CAAE;AACtD,MAAI,SACF,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACnD,oBAAmB,OAAO,OACxB,EAAE,KAA2B,EAC7BC,QACG;GACH,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,SAAM,IAAI,MAAM;EACjB;EASL,MAAM,WAAW,KAAK,IAAI;AAE1B,SAAO,SAA2B;GAEhC,KAAK,OAAO,EAAE,EAAEC,QAA+C;IAE7D,MAAM,qBAAqB,MAAM;KAC/B,cAAc;AACZ,YAAM,gBAAgB;AACtB,WAAK,OAAO;KACb;IACF;IAED,IAAIC;IACJ,MAAM,OAAO,MAAM,YAAY;AAC/B,QAAI;AACF,WAAM,KAAK,SAAS,MAAM,gBAAgB,OAAO,gBAAgB;AAC/D,UAAI;AAEF,aAAM,QAAQ,YAAY;AAC1B,aAAM,IAAI,YAAY;MACvB,SAAQ,OAAO;AAEd,mBAAY;MACb;AAGD,YAAM,IAAI;KACX,EAAC;IACH,SAAQ,OAAO;AAEd,WAAM,iBAAiB,cACrB,OAAM;AAIR,SAAI,UACF,OAAM;IAET,UAAS;AACR,WAAM,KAAK,QAAQ,KAAK;IACzB;GACF;GACD,GAAG;EACJ,EAAC;CACH;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmGD,SAAgB,mBAMdC,aACAC,UACoD;CAEpD,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACnD,oBAAmB,OAAO,OACxB,EAAE,KAA2B,EAC7BL,QACG;EACH,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CACjB;AAGH,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACvD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CMfJXZP8.cjs');
|
|
2
2
|
|
|
3
3
|
exports.IsolationLevel = require_VitestTransactionIsolator.IsolationLevel;
|
|
4
4
|
exports.VitestPostgresTransactionIsolator = require_VitestTransactionIsolator.VitestPostgresTransactionIsolator;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-
|
|
2
|
-
export { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
1
|
+
import { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-CwQaxZLP.cjs";
|
|
2
|
+
export { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-
|
|
2
|
-
export { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
1
|
+
import { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-BvR19bYn.mjs";
|
|
2
|
+
export { DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { IsolationLevel, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-DQ7tLqgV.mjs";
|
|
2
2
|
|
|
3
3
|
export { IsolationLevel, VitestPostgresTransactionIsolator, extendWithFixtures };
|
package/dist/better-auth.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as better_auth_adapters3 from "better-auth/adapters";
|
|
2
2
|
import { DBAdapterDebugLogOption } from "better-auth/adapters";
|
|
3
3
|
|
|
4
4
|
//#region src/better-auth.d.ts
|
|
@@ -7,7 +7,7 @@ interface MemoryAdapterConfig {
|
|
|
7
7
|
usePlural?: boolean;
|
|
8
8
|
initialData?: Record<string, any[]>;
|
|
9
9
|
}
|
|
10
|
-
declare const memoryAdapter: (config?: MemoryAdapterConfig, store?: Map<any, any>) =>
|
|
10
|
+
declare const memoryAdapter: (config?: MemoryAdapterConfig, store?: Map<any, any>) => better_auth_adapters3.AdapterFactory & {
|
|
11
11
|
clear: () => void;
|
|
12
12
|
getAllData: () => Record<string, any[]>;
|
|
13
13
|
getStore: () => Map<any, any>;
|
package/dist/better-auth.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as better_auth_adapters0 from "better-auth/adapters";
|
|
2
2
|
import { DBAdapterDebugLogOption } from "better-auth/adapters";
|
|
3
3
|
|
|
4
4
|
//#region src/better-auth.d.ts
|
|
@@ -7,7 +7,7 @@ interface MemoryAdapterConfig {
|
|
|
7
7
|
usePlural?: boolean;
|
|
8
8
|
initialData?: Record<string, any[]>;
|
|
9
9
|
}
|
|
10
|
-
declare const memoryAdapter: (config?: MemoryAdapterConfig, store?: Map<any, any>) =>
|
|
10
|
+
declare const memoryAdapter: (config?: MemoryAdapterConfig, store?: Map<any, any>) => better_auth_adapters0.AdapterFactory & {
|
|
11
11
|
clear: () => void;
|
|
12
12
|
getAllData: () => Record<string, any[]>;
|
|
13
13
|
getStore: () => Map<any, any>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as vitest0 from "vitest";
|
|
2
2
|
|
|
3
3
|
//#region src/os/directory.d.ts
|
|
4
|
-
declare const itWithDir:
|
|
4
|
+
declare const itWithDir: vitest0.TestAPI<{
|
|
5
5
|
dir: string;
|
|
6
6
|
}>;
|
|
7
7
|
interface DirectoryFixtures {
|
|
@@ -9,4 +9,4 @@ interface DirectoryFixtures {
|
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
11
|
export { DirectoryFixtures, itWithDir };
|
|
12
|
-
//# sourceMappingURL=directory-
|
|
12
|
+
//# sourceMappingURL=directory-BUcnztHI.d.cts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as vitest6 from "vitest";
|
|
2
2
|
|
|
3
3
|
//#region src/os/directory.d.ts
|
|
4
|
-
declare const itWithDir:
|
|
4
|
+
declare const itWithDir: vitest6.TestAPI<{
|
|
5
5
|
dir: string;
|
|
6
6
|
}>;
|
|
7
7
|
interface DirectoryFixtures {
|
|
@@ -9,4 +9,4 @@ interface DirectoryFixtures {
|
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
11
|
export { DirectoryFixtures, itWithDir };
|
|
12
|
-
//# sourceMappingURL=directory-
|
|
12
|
+
//# sourceMappingURL=directory-BXavAeJZ.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _faker_js_faker1 from "@faker-js/faker";
|
|
2
2
|
|
|
3
3
|
//#region src/faker.d.ts
|
|
4
4
|
|
|
@@ -134,7 +134,7 @@ declare function coordinateOutsideRadius(center: Coordinate, minRadiusMeters: nu
|
|
|
134
134
|
* const productPrice = faker.price();
|
|
135
135
|
* ```
|
|
136
136
|
*/
|
|
137
|
-
declare const faker$1: Readonly<
|
|
137
|
+
declare const faker$1: Readonly<_faker_js_faker1.Faker & {
|
|
138
138
|
timestamps: typeof timestamps;
|
|
139
139
|
identifier: typeof identifier;
|
|
140
140
|
sequence: typeof sequence;
|
|
@@ -163,4 +163,4 @@ type Timestamps = {
|
|
|
163
163
|
type FakerFactory = typeof faker$1;
|
|
164
164
|
//#endregion
|
|
165
165
|
export { FakerFactory, Timestamps, coordinateInRadius, faker$1 as faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
166
|
-
//# sourceMappingURL=faker-
|
|
166
|
+
//# sourceMappingURL=faker-DHh7xs4u.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _faker_js_faker6 from "@faker-js/faker";
|
|
2
2
|
|
|
3
3
|
//#region src/faker.d.ts
|
|
4
4
|
|
|
@@ -134,7 +134,7 @@ declare function coordinateOutsideRadius(center: Coordinate, minRadiusMeters: nu
|
|
|
134
134
|
* const productPrice = faker.price();
|
|
135
135
|
* ```
|
|
136
136
|
*/
|
|
137
|
-
declare const faker: Readonly<
|
|
137
|
+
declare const faker: Readonly<_faker_js_faker6.Faker & {
|
|
138
138
|
timestamps: typeof timestamps;
|
|
139
139
|
identifier: typeof identifier;
|
|
140
140
|
sequence: typeof sequence;
|
|
@@ -163,4 +163,4 @@ type Timestamps = {
|
|
|
163
163
|
type FakerFactory = typeof faker;
|
|
164
164
|
//#endregion
|
|
165
165
|
export { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
166
|
-
//# sourceMappingURL=faker-
|
|
166
|
+
//# sourceMappingURL=faker-Dg3trU4a.d.cts.map
|
package/dist/faker.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps } from "./faker-
|
|
1
|
+
import { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps } from "./faker-Dg3trU4a.cjs";
|
|
2
2
|
export { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
package/dist/faker.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps } from "./faker-
|
|
1
|
+
import { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps } from "./faker-DHh7xs4u.mjs";
|
|
2
2
|
export { FakerFactory, Timestamps, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
package/dist/kysely.cjs
CHANGED
|
@@ -3,8 +3,8 @@ const require_faker = require('./faker-B14IEMIN.cjs');
|
|
|
3
3
|
const require_KyselyFactory = require('./KyselyFactory-DaaCykWP.cjs');
|
|
4
4
|
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
5
5
|
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-CfytARcA.cjs');
|
|
6
|
-
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-
|
|
7
|
-
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-
|
|
6
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CMfJXZP8.cjs');
|
|
7
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-EvDLk5zg.cjs');
|
|
8
8
|
|
|
9
9
|
//#region src/kysely.ts
|
|
10
10
|
/**
|
|
@@ -13,10 +13,9 @@ const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransacti
|
|
|
13
13
|
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
14
14
|
*
|
|
15
15
|
* @template Database - The database schema type
|
|
16
|
+
* @template Extended - Additional fixtures to provide
|
|
16
17
|
* @param api - The Vitest test API (usually `test` from vitest)
|
|
17
|
-
* @param
|
|
18
|
-
* @param setup - Optional setup function to run before each test in the transaction
|
|
19
|
-
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
18
|
+
* @param options - Configuration options for transaction wrapping
|
|
20
19
|
* @returns A wrapped test API that provides transaction isolation
|
|
21
20
|
*
|
|
22
21
|
* @example
|
|
@@ -26,7 +25,9 @@ const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransacti
|
|
|
26
25
|
* import { db } from './database';
|
|
27
26
|
*
|
|
28
27
|
* // Create isolated test with automatic rollback
|
|
29
|
-
* const isolatedTest = wrapVitestKyselyTransaction(test,
|
|
28
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, {
|
|
29
|
+
* connection: db,
|
|
30
|
+
* });
|
|
30
31
|
*
|
|
31
32
|
* // Use in tests - each test gets its own transaction
|
|
32
33
|
* isolatedTest('should create user', async ({ trx }) => {
|
|
@@ -37,35 +38,25 @@ const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransacti
|
|
|
37
38
|
* .executeTakeFirst();
|
|
38
39
|
*
|
|
39
40
|
* expect(user).toBeDefined();
|
|
40
|
-
* // User is automatically rolled back after test
|
|
41
41
|
* });
|
|
42
42
|
*
|
|
43
|
-
* // With
|
|
44
|
-
* const
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* .values({ key: 'test_mode', value: 'true' })
|
|
51
|
-
* .execute();
|
|
52
|
-
* }
|
|
53
|
-
* );
|
|
54
|
-
*
|
|
55
|
-
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
56
|
-
* const setting = await trx
|
|
57
|
-
* .selectFrom('settings')
|
|
58
|
-
* .where('key', '=', 'test_mode')
|
|
59
|
-
* .selectAll()
|
|
60
|
-
* .executeTakeFirst();
|
|
43
|
+
* // With fixtures for factories
|
|
44
|
+
* const it = wrapVitestKyselyTransaction<DB, { factory: Factory }>(test, {
|
|
45
|
+
* connection: db,
|
|
46
|
+
* fixtures: {
|
|
47
|
+
* factory: (trx) => new Factory(trx),
|
|
48
|
+
* },
|
|
49
|
+
* });
|
|
61
50
|
*
|
|
62
|
-
*
|
|
51
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
52
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
53
|
+
* expect(user.id).toBeDefined();
|
|
63
54
|
* });
|
|
64
55
|
* ```
|
|
65
56
|
*/
|
|
66
|
-
function wrapVitestKyselyTransaction(api,
|
|
57
|
+
function wrapVitestKyselyTransaction(api, options) {
|
|
67
58
|
const wrapper = new require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator(api);
|
|
68
|
-
return wrapper.wrapVitestWithTransaction(
|
|
59
|
+
return wrapper.wrapVitestWithTransaction(options);
|
|
69
60
|
}
|
|
70
61
|
/**
|
|
71
62
|
* Extends a Kysely transaction-wrapped test with additional fixtures.
|
|
@@ -91,7 +82,10 @@ function wrapVitestKyselyTransaction(api, connection, setup, level = require_Vit
|
|
|
91
82
|
* };
|
|
92
83
|
*
|
|
93
84
|
* // Create base wrapped test
|
|
94
|
-
* const baseTest = wrapVitestKyselyTransaction<DB>(test,
|
|
85
|
+
* const baseTest = wrapVitestKyselyTransaction<DB>(test, {
|
|
86
|
+
* connection: db,
|
|
87
|
+
* setup: createTestTables,
|
|
88
|
+
* });
|
|
95
89
|
*
|
|
96
90
|
* // Extend with fixtures - each fixture receives the transaction
|
|
97
91
|
* const it = extendWithFixtures<DB, { factory: KyselyFactory<DB, typeof builders, {}> }>(
|
package/dist/kysely.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kysely.cjs","names":["api: TestAPI","
|
|
1
|
+
{"version":3,"file":"kysely.cjs","names":["api: TestAPI","options: KyselyTransactionOptions<Database, Extended>","VitestKyselyTransactionIsolator","extendWithFixtures","wrappedTest: T","fixtures: FixtureCreators<Transaction<Database>, Extended>"],"sources":["../src/kysely.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport type { TestAPI } from 'vitest';\nimport { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';\nimport {\n type DatabaseConnection,\n type FixtureCreators,\n type IsolationLevel,\n extendWithFixtures as baseExtendWithFixtures,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific exports for test utilities.\n * Provides factories, migrators, and transaction isolators for Kysely ORM.\n */\n\nexport { KyselyFactory } from './KyselyFactory';\nexport { PostgresKyselyMigrator } from './PostgresKyselyMigrator';\nexport { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\nexport type {\n DatabaseFixtures,\n ExtendedDatabaseFixtures,\n FixtureCreators,\n TestWithExtendedFixtures,\n TransactionWrapperOptions,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific options for transaction wrapping.\n */\nexport interface KyselyTransactionOptions<\n Database,\n Extended extends Record<string, unknown> = {},\n> {\n /** Function that creates or returns a Kysely database instance */\n connection: DatabaseConnection<Kysely<Database>>;\n /** Optional setup function to run within the transaction before each test */\n setup?: (trx: Transaction<Database>) => Promise<void>;\n /** Transaction isolation level (defaults to REPEATABLE_READ) */\n isolationLevel?: IsolationLevel;\n /** Additional fixtures that depend on the transaction */\n fixtures?: FixtureCreators<Transaction<Database>, Extended>;\n}\n\n// Re-export faker and FakerFactory for type portability in declaration files\nexport { faker, type FakerFactory } from './faker';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @template Database - The database schema type\n * @template Extended - Additional fixtures to provide\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param options - Configuration options for transaction wrapping\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';\n * import { db } from './database';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestKyselyTransaction(test, {\n * connection: db,\n * });\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await trx\n * .insertInto('users')\n * .values({ name: 'Test User', email: 'test@example.com' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * });\n *\n * // With fixtures for factories\n * const it = wrapVitestKyselyTransaction<DB, { factory: Factory }>(test, {\n * connection: db,\n * fixtures: {\n * factory: (trx) => new Factory(trx),\n * },\n * });\n *\n * it('should create user with factory', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user.id).toBeDefined();\n * });\n * ```\n */\nexport function wrapVitestKyselyTransaction<\n Database,\n Extended extends Record<string, unknown> = {},\n>(api: TestAPI, options: KyselyTransactionOptions<Database, Extended>) {\n const wrapper = new VitestKyselyTransactionIsolator<Database>(api);\n\n return wrapper.wrapVitestWithTransaction(options);\n}\n\n/**\n * Extends a Kysely transaction-wrapped test with additional fixtures.\n * Each fixture receives the transaction and can create dependencies like factories or repositories.\n *\n * @template Database - The database schema type\n * @template Extended - The type of additional fixtures to provide\n * @param wrappedTest - The base wrapped test from wrapVitestKyselyTransaction\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 { test } from 'vitest';\n * import { wrapVitestKyselyTransaction, extendWithFixtures, KyselyFactory } from '@geekmidas/testkit/kysely';\n *\n * // Define your builders\n * const builders = {\n * user: KyselyFactory.createBuilder<DB, 'users'>('users', ({ faker }) => ({\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * })),\n * };\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestKyselyTransaction<DB>(test, {\n * connection: db,\n * setup: createTestTables,\n * });\n *\n * // Extend with fixtures - each fixture receives the transaction\n * const it = extendWithFixtures<DB, { factory: KyselyFactory<DB, typeof builders, {}> }>(\n * baseTest,\n * {\n * factory: (trx) => new KyselyFactory(builders, {}, trx),\n * }\n * );\n *\n * // Use in tests - both trx and factory are available\n * it('should create user with factory', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test User' });\n * expect(user.id).toBeDefined();\n *\n * // Verify in database\n * const found = await trx\n * .selectFrom('users')\n * .where('id', '=', user.id)\n * .selectAll()\n * .executeTakeFirst();\n * expect(found?.name).toBe('Test User');\n * });\n * ```\n */\nexport function extendWithFixtures<\n Database,\n Extended extends Record<string, unknown>,\n T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>,\n>(wrappedTest: T, fixtures: FixtureCreators<Transaction<Database>, Extended>) {\n return baseExtendWithFixtures<Transaction<Database>, Extended, T>(\n wrappedTest,\n fixtures,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8FA,SAAgB,4BAGdA,KAAcC,SAAuD;CACrE,MAAM,UAAU,IAAIC,wEAA0C;AAE9D,QAAO,QAAQ,0BAA0B,QAAQ;AAClD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDD,SAAgBC,qBAIdC,aAAgBC,UAA4D;AAC5E,QAAO,qDACL,aACA,SACD;AACF"}
|
package/dist/kysely.d.cts
CHANGED
|
@@ -1,25 +1,37 @@
|
|
|
1
|
-
import { FakerFactory, faker } from "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { KyselyFactory } from "./KyselyFactory-
|
|
1
|
+
import { FakerFactory, faker } from "./faker-Dg3trU4a.cjs";
|
|
2
|
+
import "./Factory-D7oHtI2D.cjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-dYdviox2.cjs";
|
|
4
4
|
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
5
|
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-CQ3aUoy_.cjs";
|
|
6
|
-
import { DatabaseConnection, DatabaseFixtures, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-
|
|
6
|
+
import { DatabaseConnection, DatabaseFixtures, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions } from "./VitestTransactionIsolator-CwQaxZLP.cjs";
|
|
7
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-D3EZZhjZ.cjs";
|
|
8
8
|
import { Kysely, Transaction } from "kysely";
|
|
9
9
|
import { TestAPI } from "vitest";
|
|
10
10
|
|
|
11
11
|
//#region src/kysely.d.ts
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Kysely-specific options for transaction wrapping.
|
|
15
|
+
*/
|
|
16
|
+
interface KyselyTransactionOptions<Database, Extended extends Record<string, unknown> = {}> {
|
|
17
|
+
/** Function that creates or returns a Kysely database instance */
|
|
18
|
+
connection: DatabaseConnection<Kysely<Database>>;
|
|
19
|
+
/** Optional setup function to run within the transaction before each test */
|
|
20
|
+
setup?: (trx: Transaction<Database>) => Promise<void>;
|
|
21
|
+
/** Transaction isolation level (defaults to REPEATABLE_READ) */
|
|
22
|
+
isolationLevel?: IsolationLevel;
|
|
23
|
+
/** Additional fixtures that depend on the transaction */
|
|
24
|
+
fixtures?: FixtureCreators<Transaction<Database>, Extended>;
|
|
25
|
+
}
|
|
13
26
|
/**
|
|
14
27
|
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
15
28
|
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
29
|
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
30
|
*
|
|
18
31
|
* @template Database - The database schema type
|
|
32
|
+
* @template Extended - Additional fixtures to provide
|
|
19
33
|
* @param api - The Vitest test API (usually `test` from vitest)
|
|
20
|
-
* @param
|
|
21
|
-
* @param setup - Optional setup function to run before each test in the transaction
|
|
22
|
-
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
34
|
+
* @param options - Configuration options for transaction wrapping
|
|
23
35
|
* @returns A wrapped test API that provides transaction isolation
|
|
24
36
|
*
|
|
25
37
|
* @example
|
|
@@ -29,7 +41,9 @@ import { TestAPI } from "vitest";
|
|
|
29
41
|
* import { db } from './database';
|
|
30
42
|
*
|
|
31
43
|
* // Create isolated test with automatic rollback
|
|
32
|
-
* const isolatedTest = wrapVitestKyselyTransaction(test,
|
|
44
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, {
|
|
45
|
+
* connection: db,
|
|
46
|
+
* });
|
|
33
47
|
*
|
|
34
48
|
* // Use in tests - each test gets its own transaction
|
|
35
49
|
* isolatedTest('should create user', async ({ trx }) => {
|
|
@@ -40,35 +54,23 @@ import { TestAPI } from "vitest";
|
|
|
40
54
|
* .executeTakeFirst();
|
|
41
55
|
*
|
|
42
56
|
* expect(user).toBeDefined();
|
|
43
|
-
* // User is automatically rolled back after test
|
|
44
57
|
* });
|
|
45
58
|
*
|
|
46
|
-
* // With
|
|
47
|
-
* const
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
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();
|
|
59
|
+
* // With fixtures for factories
|
|
60
|
+
* const it = wrapVitestKyselyTransaction<DB, { factory: Factory }>(test, {
|
|
61
|
+
* connection: db,
|
|
62
|
+
* fixtures: {
|
|
63
|
+
* factory: (trx) => new Factory(trx),
|
|
64
|
+
* },
|
|
65
|
+
* });
|
|
64
66
|
*
|
|
65
|
-
*
|
|
67
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
68
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
69
|
+
* expect(user.id).toBeDefined();
|
|
66
70
|
* });
|
|
67
71
|
* ```
|
|
68
72
|
*/
|
|
69
|
-
declare function wrapVitestKyselyTransaction<Database>(api: TestAPI,
|
|
70
|
-
trx: Transaction<Database>;
|
|
71
|
-
}>;
|
|
73
|
+
declare function wrapVitestKyselyTransaction<Database, Extended extends Record<string, unknown> = {}>(api: TestAPI, options: KyselyTransactionOptions<Database, Extended>): TestAPI<DatabaseFixtures<Transaction<Database>, object> & Extended>;
|
|
72
74
|
/**
|
|
73
75
|
* Extends a Kysely transaction-wrapped test with additional fixtures.
|
|
74
76
|
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
@@ -93,7 +95,10 @@ declare function wrapVitestKyselyTransaction<Database>(api: TestAPI, connection:
|
|
|
93
95
|
* };
|
|
94
96
|
*
|
|
95
97
|
* // Create base wrapped test
|
|
96
|
-
* const baseTest = wrapVitestKyselyTransaction<DB>(test,
|
|
98
|
+
* const baseTest = wrapVitestKyselyTransaction<DB>(test, {
|
|
99
|
+
* connection: db,
|
|
100
|
+
* setup: createTestTables,
|
|
101
|
+
* });
|
|
97
102
|
*
|
|
98
103
|
* // Extend with fixtures - each fixture receives the transaction
|
|
99
104
|
* const it = extendWithFixtures<DB, { factory: KyselyFactory<DB, typeof builders, {}> }>(
|
|
@@ -120,5 +125,5 @@ declare function wrapVitestKyselyTransaction<Database>(api: TestAPI, connection:
|
|
|
120
125
|
*/
|
|
121
126
|
declare function extendWithFixtures<Database, Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Transaction<Database>, Extended>): TestWithExtendedFixtures<Transaction<Database>, Extended, T>;
|
|
122
127
|
//#endregion
|
|
123
|
-
export { DatabaseFixtures, ExtendedDatabaseFixtures, FakerFactory, FixtureCreators, IsolationLevel, KyselyFactory, PostgresKyselyMigrator, TestWithExtendedFixtures, VitestKyselyTransactionIsolator, extendWithFixtures, faker, wrapVitestKyselyTransaction };
|
|
128
|
+
export { DatabaseFixtures, ExtendedDatabaseFixtures, FakerFactory, FixtureCreators, IsolationLevel, KyselyFactory, KyselyTransactionOptions, PostgresKyselyMigrator, TestWithExtendedFixtures, TransactionWrapperOptions, VitestKyselyTransactionIsolator, extendWithFixtures, faker, wrapVitestKyselyTransaction };
|
|
124
129
|
//# sourceMappingURL=kysely.d.cts.map
|