@geekmidas/testkit 10.0.0-alpha.3 → 10.0.0-alpha.31
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/README.md +74 -0
- package/dist/Factory.cjs +51 -2
- package/dist/Factory.cjs.map +1 -0
- package/dist/Factory.d.cts +143 -3
- package/dist/Factory.d.cts.map +1 -0
- package/dist/Factory.d.mts +143 -3
- package/dist/Factory.d.mts.map +1 -0
- package/dist/Factory.mjs +50 -2
- package/dist/Factory.mjs.map +1 -0
- package/dist/KyselyFactory.cjs +237 -4
- package/dist/KyselyFactory.cjs.map +1 -0
- package/dist/KyselyFactory.d.cts +224 -4
- package/dist/KyselyFactory.d.cts.map +1 -0
- package/dist/KyselyFactory.d.mts +224 -4
- package/dist/KyselyFactory.d.mts.map +1 -0
- package/dist/KyselyFactory.mjs +236 -4
- package/dist/KyselyFactory.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +265 -4
- package/dist/ObjectionFactory.cjs.map +1 -0
- package/dist/ObjectionFactory.d.cts +237 -4
- package/dist/ObjectionFactory.d.cts.map +1 -0
- package/dist/ObjectionFactory.d.mts +237 -4
- package/dist/ObjectionFactory.d.mts.map +1 -0
- package/dist/ObjectionFactory.mjs +264 -4
- package/dist/ObjectionFactory.mjs.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +94 -3
- package/dist/PostgresKyselyMigrator.cjs.map +1 -0
- package/dist/PostgresKyselyMigrator.d.cts +80 -3
- package/dist/PostgresKyselyMigrator.d.cts.map +1 -0
- package/dist/PostgresKyselyMigrator.d.mts +80 -3
- package/dist/PostgresKyselyMigrator.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.mjs +93 -3
- package/dist/PostgresKyselyMigrator.mjs.map +1 -0
- package/dist/{PostgresMigrator-C_QQ6q35.d.mts → PostgresMigrator-CDPnnwMJ.d.cts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
- package/dist/{PostgresMigrator-CeYy-eHF.d.cts → PostgresMigrator-CDPnnwMJ.d.mts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +178 -2
- package/dist/PostgresMigrator.cjs.map +1 -0
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +175 -2
- package/dist/PostgresMigrator.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +118 -3
- package/dist/PostgresObjectionMigrator.cjs.map +1 -0
- package/dist/PostgresObjectionMigrator.d.cts +75 -3
- package/dist/PostgresObjectionMigrator.d.cts.map +1 -0
- package/dist/PostgresObjectionMigrator.d.mts +75 -3
- package/dist/PostgresObjectionMigrator.d.mts.map +1 -0
- package/dist/PostgresObjectionMigrator.mjs +117 -3
- package/dist/PostgresObjectionMigrator.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +66 -3
- package/dist/VitestKyselyTransactionIsolator.cjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +65 -3
- package/dist/VitestKyselyTransactionIsolator.mjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +62 -3
- package/dist/VitestObjectionTransactionIsolator.cjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +61 -3
- package/dist/VitestObjectionTransactionIsolator.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +187 -4
- package/dist/VitestTransactionIsolator.cjs.map +1 -0
- package/dist/VitestTransactionIsolator.d.cts +219 -2
- package/dist/VitestTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.d.mts +219 -2
- package/dist/VitestTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator.mjs +184 -2
- package/dist/VitestTransactionIsolator.mjs.map +1 -0
- package/dist/aws.cjs +3 -4
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +3 -8
- package/dist/aws.d.cts.map +1 -1
- package/dist/aws.d.mts +3 -8
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +1 -2
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs +2 -2
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +4 -6
- package/dist/benchmark.d.cts.map +1 -1
- package/dist/benchmark.d.mts +4 -6
- package/dist/benchmark.d.mts.map +1 -1
- package/dist/benchmark.mjs +1 -1
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +31 -15
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +13142 -4
- package/dist/better-auth.d.cts.map +1 -1
- package/dist/better-auth.d.mts +13142 -4
- package/dist/better-auth.d.mts.map +1 -1
- package/dist/better-auth.mjs +29 -13
- package/dist/better-auth.mjs.map +1 -1
- package/dist/browser.cjs +127 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +47 -0
- package/dist/browser.d.cts.map +1 -0
- package/dist/browser.d.mts +47 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +125 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/context.cjs +80 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +55 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.mts +55 -0
- package/dist/context.d.mts.map +1 -0
- package/dist/context.mjs +73 -0
- package/dist/context.mjs.map +1 -0
- package/dist/faker.cjs +262 -8
- package/dist/faker.cjs.map +1 -0
- package/dist/faker.d.cts +162 -2
- package/dist/faker.d.cts.map +1 -0
- package/dist/faker.d.mts +162 -2
- package/dist/faker.d.mts.map +1 -0
- package/dist/faker.mjs +255 -2
- package/dist/faker.mjs.map +1 -0
- package/dist/helpers.cjs +6 -5
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -5
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +1 -5
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +1 -2
- package/dist/helpers.mjs.map +1 -1
- package/dist/initScript.cjs +15 -15
- package/dist/initScript.cjs.map +1 -1
- package/dist/initScript.d.cts +2 -4
- package/dist/initScript.d.cts.map +1 -1
- package/dist/initScript.d.mts +2 -4
- package/dist/initScript.d.mts.map +1 -1
- package/dist/initScript.mjs +10 -12
- package/dist/initScript.mjs.map +1 -1
- package/dist/kysely.cjs +10 -13
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +10 -14
- package/dist/kysely.d.cts.map +1 -1
- package/dist/kysely.d.mts +10 -14
- package/dist/kysely.d.mts.map +1 -1
- package/dist/kysely.mjs +10 -14
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -5
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.mts +1 -5
- package/dist/logger.d.mts.map +1 -1
- package/dist/logger.mjs +1 -2
- package/dist/logger.mjs.map +1 -1
- package/dist/mailbox.cjs +95 -0
- package/dist/mailbox.cjs.map +1 -0
- package/dist/mailbox.d.cts +58 -0
- package/dist/mailbox.d.cts.map +1 -0
- package/dist/mailbox.d.mts +58 -0
- package/dist/mailbox.d.mts.map +1 -0
- package/dist/mailbox.mjs +92 -0
- package/dist/mailbox.mjs.map +1 -0
- package/dist/objection.cjs +10 -13
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +10 -14
- package/dist/objection.d.cts.map +1 -1
- package/dist/objection.d.mts +10 -14
- package/dist/objection.d.mts.map +1 -1
- package/dist/objection.mjs +10 -14
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.cjs +31 -2
- package/dist/os/directory.cjs.map +1 -0
- package/dist/os/directory.d.cts +9 -2
- package/dist/os/directory.d.cts.map +1 -0
- package/dist/os/directory.d.mts +9 -2
- package/dist/os/directory.d.mts.map +1 -0
- package/dist/os/directory.mjs +25 -2
- package/dist/os/directory.mjs.map +1 -0
- package/dist/os/index.cjs +3 -3
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/os/index.mjs +2 -3
- package/dist/requestContext.cjs +10 -11
- package/dist/requestContext.cjs.map +1 -1
- package/dist/requestContext.d.cts +4 -8
- package/dist/requestContext.d.cts.map +1 -1
- package/dist/requestContext.d.mts +4 -8
- package/dist/requestContext.d.mts.map +1 -1
- package/dist/requestContext.mjs +1 -2
- package/dist/requestContext.mjs.map +1 -1
- package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-VH7oDXx4.cjs} +8 -10
- package/dist/timer.cjs +2 -2
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +1 -4
- package/dist/timer.d.cts.map +1 -1
- package/dist/timer.d.mts +1 -4
- package/dist/timer.d.mts.map +1 -1
- package/dist/timer.mjs +1 -1
- package/dist/timer.mjs.map +1 -1
- package/dist/transactions.cjs +123 -0
- package/dist/transactions.cjs.map +1 -0
- package/dist/transactions.d.cts +44 -0
- package/dist/transactions.d.cts.map +1 -0
- package/dist/transactions.d.mts +44 -0
- package/dist/transactions.d.mts.map +1 -0
- package/dist/transactions.mjs +118 -0
- package/dist/transactions.mjs.map +1 -0
- package/package.json +59 -16
- package/dist/Factory-BFVnMMCC.mjs +0 -51
- package/dist/Factory-BFVnMMCC.mjs.map +0 -1
- package/dist/Factory-BOX312yd.d.cts +0 -147
- package/dist/Factory-BOX312yd.d.cts.map +0 -1
- package/dist/Factory-BhjUOBWN.cjs +0 -57
- package/dist/Factory-BhjUOBWN.cjs.map +0 -1
- package/dist/Factory-C6W78ulZ.d.mts +0 -147
- package/dist/Factory-C6W78ulZ.d.mts.map +0 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs +0 -246
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +0 -1
- package/dist/KyselyFactory-Cc2UmOJk.d.mts +0 -228
- package/dist/KyselyFactory-Cc2UmOJk.d.mts.map +0 -1
- package/dist/KyselyFactory-DMswpwji.mjs +0 -241
- package/dist/KyselyFactory-DMswpwji.mjs.map +0 -1
- package/dist/KyselyFactory-Dy5zzV4B.d.cts +0 -228
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +0 -1
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts +0 -241
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts.map +0 -1
- package/dist/ObjectionFactory-BeFBYcan.cjs +0 -272
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +0 -1
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts +0 -241
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +0 -1
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs +0 -267
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +0 -1
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts +0 -84
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts.map +0 -1
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs +0 -101
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts +0 -84
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts.map +0 -1
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs +0 -95
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs.map +0 -1
- package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +0 -1
- package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +0 -1
- package/dist/PostgresMigrator-DVAY04qN.mjs +0 -166
- package/dist/PostgresMigrator-DVAY04qN.mjs.map +0 -1
- package/dist/PostgresMigrator-M9jpzOvN.cjs +0 -172
- package/dist/PostgresMigrator-M9jpzOvN.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts +0 -79
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts.map +0 -1
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts +0 -79
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts.map +0 -1
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs +0 -123
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs +0 -118
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs +0 -67
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs +0 -72
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts +0 -64
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts +0 -64
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs +0 -63
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs +0 -68
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts +0 -223
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs +0 -188
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs +0 -206
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts +0 -223
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +0 -1
- package/dist/directory-BxjqmtCT.mjs +0 -27
- package/dist/directory-BxjqmtCT.mjs.map +0 -1
- package/dist/directory-DD_xQoSh.cjs +0 -33
- package/dist/directory-DD_xQoSh.cjs.map +0 -1
- package/dist/directory-DGOcVlKD.d.cts +0 -13
- package/dist/directory-DGOcVlKD.d.cts.map +0 -1
- package/dist/directory-DiQ_DkaF.d.mts +0 -13
- package/dist/directory-DiQ_DkaF.d.mts.map +0 -1
- package/dist/faker-B14IEMIN.cjs +0 -304
- package/dist/faker-B14IEMIN.cjs.map +0 -1
- package/dist/faker-BGKYFoCT.mjs +0 -262
- package/dist/faker-BGKYFoCT.mjs.map +0 -1
- package/dist/faker-Cg76aFNO.d.cts +0 -166
- package/dist/faker-Cg76aFNO.d.cts.map +0 -1
- package/dist/faker-D9gz7KjY.d.mts +0 -166
- package/dist/faker-D9gz7KjY.d.mts.map +0 -1
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { EnvironmentParser } from "@geekmidas/envkit";
|
|
2
|
-
import { serviceContext } from "@geekmidas/services";
|
|
3
|
-
|
|
4
|
-
//#region src/VitestTransactionIsolator.ts
|
|
5
|
-
/**
|
|
6
|
-
* PostgreSQL transaction isolation levels.
|
|
7
|
-
* Controls the visibility of concurrent transactions.
|
|
8
|
-
*
|
|
9
|
-
* @see https://www.postgresql.org/docs/current/transaction-iso.html
|
|
10
|
-
*/
|
|
11
|
-
let IsolationLevel = /* @__PURE__ */ function(IsolationLevel$1) {
|
|
12
|
-
/**
|
|
13
|
-
* Lowest isolation level. Allows dirty reads.
|
|
14
|
-
* Not recommended for testing.
|
|
15
|
-
*/
|
|
16
|
-
IsolationLevel$1["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
17
|
-
/**
|
|
18
|
-
* Default PostgreSQL isolation level.
|
|
19
|
-
* Prevents dirty reads but allows non-repeatable reads.
|
|
20
|
-
*/
|
|
21
|
-
IsolationLevel$1["READ_COMMITTED"] = "READ COMMITTED";
|
|
22
|
-
/**
|
|
23
|
-
* Prevents dirty reads and non-repeatable reads.
|
|
24
|
-
* Recommended for most test scenarios.
|
|
25
|
-
*/
|
|
26
|
-
IsolationLevel$1["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
27
|
-
/**
|
|
28
|
-
* Highest isolation level. Prevents all phenomena.
|
|
29
|
-
* May cause performance overhead in tests.
|
|
30
|
-
*/
|
|
31
|
-
IsolationLevel$1["SERIALIZABLE"] = "SERIALIZABLE";
|
|
32
|
-
return IsolationLevel$1;
|
|
33
|
-
}({});
|
|
34
|
-
/**
|
|
35
|
-
* Abstract base class for implementing database transaction isolation in Vitest tests.
|
|
36
|
-
* Provides automatic transaction rollback after each test to maintain test isolation.
|
|
37
|
-
* Subclasses must implement the transact() method for their specific database driver.
|
|
38
|
-
*
|
|
39
|
-
* @template TConn - The database connection type
|
|
40
|
-
* @template Transaction - The transaction type
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* // Implement for your database driver
|
|
45
|
-
* class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
|
|
46
|
-
* async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
|
|
47
|
-
* await conn.transaction(level, fn);
|
|
48
|
-
* }
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* // Use in tests
|
|
52
|
-
* const isolator = new MyDatabaseIsolator(test);
|
|
53
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction(db);
|
|
54
|
-
*
|
|
55
|
-
* isolatedTest('should create user', async ({ trx }) => {
|
|
56
|
-
* await trx.insert('users', { name: 'Test' });
|
|
57
|
-
* // Data is automatically rolled back after test
|
|
58
|
-
* });
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
var VitestPostgresTransactionIsolator = class {
|
|
62
|
-
/**
|
|
63
|
-
* Creates a new VitestPostgresTransactionIsolator instance.
|
|
64
|
-
*
|
|
65
|
-
* @param api - The Vitest test API (usually the `test` export from vitest)
|
|
66
|
-
*/
|
|
67
|
-
constructor(api) {
|
|
68
|
-
this.api = api;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
72
|
-
* Each test will run within a database transaction that is automatically rolled back.
|
|
73
|
-
*
|
|
74
|
-
* @param options - Configuration options for transaction wrapping
|
|
75
|
-
* @returns A wrapped test API with transaction support
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction({
|
|
80
|
-
* connection: db,
|
|
81
|
-
* setup: async (trx) => {
|
|
82
|
-
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
83
|
-
* },
|
|
84
|
-
* fixtures: {
|
|
85
|
-
* factory: (trx) => new Factory(trx),
|
|
86
|
-
* },
|
|
87
|
-
* });
|
|
88
|
-
*
|
|
89
|
-
* isolatedTest('test with transaction', async ({ trx, factory }) => {
|
|
90
|
-
* const user = await factory.insert('user', { name: 'Test' });
|
|
91
|
-
* expect(user).toBeDefined();
|
|
92
|
-
* });
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
|
-
wrapVitestWithTransaction(options) {
|
|
96
|
-
const { connection, setup, isolationLevel = IsolationLevel.REPEATABLE_READ, fixtures } = options;
|
|
97
|
-
let resolved;
|
|
98
|
-
const resolveConnection = async () => {
|
|
99
|
-
if (resolved === void 0) resolved = await (isConstruct(connection) ? connection.service.register({
|
|
100
|
-
envParser: new EnvironmentParser({ ...process.env }),
|
|
101
|
-
context: serviceContext
|
|
102
|
-
}) : connection());
|
|
103
|
-
return resolved;
|
|
104
|
-
};
|
|
105
|
-
const additionalFixtures = {};
|
|
106
|
-
if (fixtures) for (const [key, creator] of Object.entries(fixtures)) additionalFixtures[key] = async ({ trx }, use) => {
|
|
107
|
-
const value = await creator(trx);
|
|
108
|
-
await use(value);
|
|
109
|
-
};
|
|
110
|
-
const api = this.api;
|
|
111
|
-
return api.extend({
|
|
112
|
-
trx: async ({}, use) => {
|
|
113
|
-
class TestRollback extends Error {
|
|
114
|
-
constructor() {
|
|
115
|
-
super("Test rollback");
|
|
116
|
-
this.name = "TestRollback";
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
let testError;
|
|
120
|
-
const conn = await resolveConnection();
|
|
121
|
-
try {
|
|
122
|
-
await this.transact(conn, isolationLevel, async (transaction) => {
|
|
123
|
-
try {
|
|
124
|
-
await setup?.(transaction);
|
|
125
|
-
await use(transaction);
|
|
126
|
-
} catch (error) {
|
|
127
|
-
testError = error;
|
|
128
|
-
}
|
|
129
|
-
throw new TestRollback();
|
|
130
|
-
});
|
|
131
|
-
} catch (error) {
|
|
132
|
-
if (!(error instanceof TestRollback)) throw error;
|
|
133
|
-
if (testError) throw testError;
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
...additionalFixtures
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
/** A construct is distinguished from a factory by owning a service. */
|
|
141
|
-
function isConstruct(connection) {
|
|
142
|
-
return typeof connection !== "function";
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Extends a wrapped test API with additional fixtures that depend on the transaction.
|
|
146
|
-
* This allows composing test context with factories, repositories, or other helpers.
|
|
147
|
-
*
|
|
148
|
-
* @template Transaction - The transaction type
|
|
149
|
-
* @template Extended - The type of additional context to provide
|
|
150
|
-
* @param wrappedTest - The base wrapped test from wrapVitestWithTransaction
|
|
151
|
-
* @param fixtures - Object mapping fixture names to creator functions
|
|
152
|
-
* @returns An extended test API with both trx and the additional fixtures
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
|
|
157
|
-
*
|
|
158
|
-
* // Create base wrapped test
|
|
159
|
-
* const baseTest = wrapVitestKyselyTransaction(test, {
|
|
160
|
-
* connection: db,
|
|
161
|
-
* setup: createTestTables,
|
|
162
|
-
* });
|
|
163
|
-
*
|
|
164
|
-
* // Extend with fixtures
|
|
165
|
-
* const it = extendWithFixtures(baseTest, {
|
|
166
|
-
* factory: (trx) => new KyselyFactory(builders, seeds, trx),
|
|
167
|
-
* userRepo: (trx) => new UserRepository(trx),
|
|
168
|
-
* });
|
|
169
|
-
*
|
|
170
|
-
* // Use in tests - trx and all fixtures are available
|
|
171
|
-
* it('should create user with factory', async ({ trx, factory, userRepo }) => {
|
|
172
|
-
* const user = await factory.insert('user', { name: 'Test' });
|
|
173
|
-
* expect(user).toBeDefined();
|
|
174
|
-
* });
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
function extendWithFixtures(wrappedTest, fixtures) {
|
|
178
|
-
const fixtureDefinitions = {};
|
|
179
|
-
for (const [key, creator] of Object.entries(fixtures)) fixtureDefinitions[key] = async ({ trx }, use) => {
|
|
180
|
-
const value = await creator(trx);
|
|
181
|
-
await use(value);
|
|
182
|
-
};
|
|
183
|
-
return wrappedTest.extend(fixtureDefinitions);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
//#endregion
|
|
187
|
-
export { IsolationLevel, VitestPostgresTransactionIsolator, extendWithFixtures };
|
|
188
|
-
//# sourceMappingURL=VitestTransactionIsolator-CvwPecpl.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestTransactionIsolator-CvwPecpl.mjs","names":["api: TestAPI","options: TransactionWrapperOptions<TConn, Transaction, Extended>","resolved: TConn | undefined","additionalFixtures: Record<string, unknown>","use: (value: unknown) => Promise<void>","use: (value: Transaction) => Promise<void>","testError: Error | undefined","connection: DatabaseConnection<Conn>","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>"],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":["import { EnvironmentParser } from '@geekmidas/envkit';\nimport { type Service, serviceContext } from '@geekmidas/services';\nimport 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\t/**\n\t * The database transaction available to the test.\n\t * All database operations should use this transaction to ensure proper rollback.\n\t */\n\ttrx: Transaction;\n}\n\n/**\n * Combined fixtures type that merges the base transaction fixture with extended context.\n */\nexport type ExtendedDatabaseFixtures<\n\tTransaction,\n\tExtended = 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\ttrx: 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\t/**\n\t * Lowest isolation level. Allows dirty reads.\n\t * Not recommended for testing.\n\t */\n\tREAD_UNCOMMITTED = 'READ UNCOMMITTED',\n\t/**\n\t * Default PostgreSQL isolation level.\n\t * Prevents dirty reads but allows non-repeatable reads.\n\t */\n\tREAD_COMMITTED = 'READ COMMITTED',\n\t/**\n\t * Prevents dirty reads and non-repeatable reads.\n\t * Recommended for most test scenarios.\n\t */\n\tREPEATABLE_READ = 'REPEATABLE READ',\n\t/**\n\t * Highest isolation level. Prevents all phenomena.\n\t * May cause performance overhead in tests.\n\t */\n\tSERIALIZABLE = '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\t/**\n\t * Abstract method to create a transaction with the specified isolation level.\n\t * Must be implemented by subclasses for specific database drivers.\n\t *\n\t * @param conn - The database connection\n\t * @param isolationLevel - The transaction isolation level\n\t * @param fn - The function to execute within the transaction\n\t * @returns Promise that resolves when the transaction completes\n\t */\n\tabstract transact(\n\t\tconn: TConn,\n\t\tisolationLevel: IsolationLevel,\n\t\tfn: (trx: Transaction) => Promise<void>,\n\t): Promise<void>;\n\n\tabstract destroy(conn: TConn): Promise<void>;\n\t/**\n\t * Creates a new VitestPostgresTransactionIsolator instance.\n\t *\n\t * @param api - The Vitest test API (usually the `test` export from vitest)\n\t */\n\tconstructor(private readonly api: TestAPI) {}\n\n\t/**\n\t * Creates a wrapped version of Vitest's test API that provides transaction isolation.\n\t * Each test will run within a database transaction that is automatically rolled back.\n\t *\n\t * @param options - Configuration options for transaction wrapping\n\t * @returns A wrapped test API with transaction support\n\t *\n\t * @example\n\t * ```typescript\n\t * const isolatedTest = isolator.wrapVitestWithTransaction({\n\t * connection: db,\n\t * setup: async (trx) => {\n\t * await trx.insert('settings', { key: 'test', value: 'true' });\n\t * },\n\t * fixtures: {\n\t * factory: (trx) => new Factory(trx),\n\t * },\n\t * });\n\t *\n\t * isolatedTest('test with transaction', async ({ trx, factory }) => {\n\t * const user = await factory.insert('user', { name: 'Test' });\n\t * expect(user).toBeDefined();\n\t * });\n\t * ```\n\t */\n\twrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(\n\t\toptions: TransactionWrapperOptions<TConn, Transaction, Extended>,\n\t) {\n\t\tconst {\n\t\t\tconnection,\n\t\t\tsetup,\n\t\t\tisolationLevel = IsolationLevel.REPEATABLE_READ,\n\t\t\tfixtures,\n\t\t} = options;\n\n\t\t// Resolved once per file, not once per test. The fixture below runs for\n\t\t// every test, and opening a pool each time leaves one connection per test\n\t\t// alive — a file of fifty tests exhausts the server.\n\t\tlet resolved: TConn | undefined;\n\t\tconst resolveConnection = async (): Promise<TConn> => {\n\t\t\tif (resolved === undefined) {\n\t\t\t\tresolved = await (isConstruct(connection)\n\t\t\t\t\t? connection.service.register({\n\t\t\t\t\t\t\t// Spread: `process.env` is a ProcessEnv, not a plain object.\n\t\t\t\t\t\t\tenvParser: new EnvironmentParser({ ...process.env }),\n\t\t\t\t\t\t\tcontext: serviceContext,\n\t\t\t\t\t\t})\n\t\t\t\t\t: connection());\n\t\t\t}\n\t\t\treturn resolved;\n\t\t};\n\n\t\t// Build fixture definitions for additional fixtures that depend on trx\n\t\tconst additionalFixtures: Record<string, unknown> = {};\n\t\tif (fixtures) {\n\t\t\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\t\t\tadditionalFixtures[key] = async (\n\t\t\t\t\t{ trx }: { trx: Transaction },\n\t\t\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t\t\t) => {\n\t\t\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\t\t\tawait use(value);\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\ttype CombinedFixtures = DatabaseFixtures<Transaction> & Extended;\n\n\t\t// Cast to bypass Vitest's strict fixture typing which can't infer\n\t\t// dynamically built fixture objects\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tconst api = this.api as TestAPI & {\n\t\t\textend: <T>(fixtures: any) => TestAPI<T>;\n\t\t};\n\n\t\treturn api.extend<CombinedFixtures>({\n\t\t\t// This fixture automatically provides a transaction to each test\n\t\t\t// biome-ignore lint/correctness/noEmptyPattern: this has to be like this to satisfy Biome\n\t\t\ttrx: async ({}: {}, use: (value: Transaction) => Promise<void>) => {\n\t\t\t\t// Create a custom error class for rollback\n\t\t\t\tclass TestRollback extends Error {\n\t\t\t\t\tconstructor() {\n\t\t\t\t\t\tsuper('Test rollback');\n\t\t\t\t\t\tthis.name = 'TestRollback';\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tlet testError: Error | undefined;\n\t\t\t\tconst conn = await resolveConnection();\n\t\t\t\ttry {\n\t\t\t\t\tawait this.transact(conn, isolationLevel, async (transaction) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// Provide the transaction to the test\n\t\t\t\t\t\t\tawait setup?.(transaction);\n\t\t\t\t\t\t\tawait use(transaction);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t// Capture any test errors\n\t\t\t\t\t\t\ttestError = error as Error;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Always throw to trigger rollback\n\t\t\t\t\t\tthrow new TestRollback();\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Only rethrow if it's not our rollback error\n\t\t\t\t\tif (!(error instanceof TestRollback)) {\n\t\t\t\t\t\tthrow error;\n\t\t\t\t\t}\n\n\t\t\t\t\t// If the test had an error, throw it now\n\t\t\t\t\tif (testError) {\n\t\t\t\t\t\tthrow testError;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t...additionalFixtures,\n\t\t});\n\t}\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\n\n/**\n * Where a connection comes from: a construct, or a function returning one.\n *\n * A construct is preferred, because it builds the client through the same\n * `connect()` production uses. A hand-rolled client missing `CamelCasePlugin`\n * writes `createdAt` where the app writes `created_at`, and the test passes\n * while production does not. The URL is read from `process.env`.\n */\nexport type DatabaseConnection<Conn> =\n\t| DatabaseConnectionFn<Conn>\n\t| ConnectableConstruct<Conn>;\n\n/** A construct is distinguished from a factory by owning a service. */\nfunction isConstruct<Conn>(\n\tconnection: DatabaseConnection<Conn>,\n): connection is ConnectableConstruct<Conn> {\n\treturn typeof connection !== 'function';\n}\n\n/**\n * Options for wrapping Vitest tests with database transaction isolation.\n */\n/**\n * Anything that owns a service handing back a connection — a database\n * construct, structurally, so testkit depends on no construct package.\n */\nexport interface ConnectableConstruct<TConn> {\n\treadonly service: Service<string, TConn>;\n}\n\nexport interface TransactionWrapperOptions<\n\tTConn,\n\tTransaction,\n\tExtended extends Record<string, unknown> = {},\n> {\n\t/** Where the connection comes from — a construct, or a function returning one */\n\tconnection: DatabaseConnection<TConn>;\n\t/** Optional setup function to run within the transaction before each test */\n\tsetup?: (trx: Transaction) => Promise<void>;\n\t/** Transaction isolation level (defaults to REPEATABLE_READ) */\n\tisolationLevel?: IsolationLevel;\n\t/** Additional fixtures that depend on the transaction */\n\tfixtures?: 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\tTransaction,\n\tExtended extends Record<string, unknown>,\n> = {\n\t[K in keyof Extended]: (\n\t\ttrx: Transaction,\n\t) => 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\tTransaction,\n\tExtended extends Record<string, unknown>,\n\tBaseTest extends ReturnType<TestAPI['extend']> = ReturnType<\n\t\tTestAPI['extend']\n\t>,\n> = BaseTest & {\n\t<C extends object>(\n\t\tname: string,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): void;\n\t<C extends object>(\n\t\tname: string,\n\t\toptions: object,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): 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\tTransaction,\n\tExtended extends Record<string, unknown>,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends ReturnType<TestAPI['extend']> = any,\n>(\n\twrappedTest: T,\n\tfixtures: FixtureCreators<Transaction, Extended>,\n): TestWithExtendedFixtures<Transaction, Extended, T> {\n\t// Build fixture definitions for Vitest's extend API\n\tconst fixtureDefinitions: Record<string, any> = {};\n\n\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\tfixtureDefinitions[key] = async (\n\t\t\t{ trx }: { trx: Transaction },\n\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t) => {\n\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\tawait use(value);\n\t\t};\n\t}\n\n\treturn (wrappedTest as any).extend(fixtureDefinitions);\n}\n"],"mappings":";;;;;;;;;;AAkDA,IAAY,4DAAL;;;;;AAKN;;;;;AAKA;;;;;AAKA;;;;;AAKA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BD,IAAsB,oCAAtB,MAA4E;;;;;;CAsB3E,YAA6BA,KAAc;EAAd;CAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B7C,0BACCC,SACC;EACD,MAAM,EACL,YACA,OACA,iBAAiB,eAAe,iBAChC,UACA,GAAG;EAKJ,IAAIC;EACJ,MAAM,oBAAoB,YAA4B;AACrD,OAAI,oBACH,YAAW,OAAO,YAAY,WAAW,GACtC,WAAW,QAAQ,SAAS;IAE5B,WAAW,IAAI,kBAAkB,EAAE,GAAG,QAAQ,IAAK;IACnD,SAAS;GACT,EAAC,GACD,YAAY;AAEhB,UAAO;EACP;EAGD,MAAMC,qBAA8C,CAAE;AACtD,MAAI,SACH,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BC,QACI;GACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,SAAM,IAAI,MAAM;EAChB;EASH,MAAM,MAAM,KAAK;AAIjB,SAAO,IAAI,OAAyB;GAGnC,KAAK,OAAO,EAAM,EAAEC,QAA+C;IAElE,MAAM,qBAAqB,MAAM;KAChC,cAAc;AACb,YAAM,gBAAgB;AACtB,WAAK,OAAO;KACZ;IACD;IAED,IAAIC;IACJ,MAAM,OAAO,MAAM,mBAAmB;AACtC,QAAI;AACH,WAAM,KAAK,SAAS,MAAM,gBAAgB,OAAO,gBAAgB;AAChE,UAAI;AAEH,aAAM,QAAQ,YAAY;AAC1B,aAAM,IAAI,YAAY;MACtB,SAAQ,OAAO;AAEf,mBAAY;MACZ;AAGD,YAAM,IAAI;KACV,EAAC;IACF,SAAQ,OAAO;AAEf,WAAM,iBAAiB,cACtB,OAAM;AAIP,SAAI,UACH,OAAM;IAEP;GACD;GACD,GAAG;EACH,EAAC;CACF;AACD;;AAiBD,SAAS,YACRC,YAC2C;AAC3C,eAAc,eAAe;AAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwGD,SAAgB,mBAMfC,aACAC,UACqD;CAErD,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BN,QACI;EACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CAChB;AAGF,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACtD"}
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const __geekmidas_envkit = require_chunk.__toESM(require("@geekmidas/envkit"));
|
|
3
|
-
const __geekmidas_services = require_chunk.__toESM(require("@geekmidas/services"));
|
|
4
|
-
|
|
5
|
-
//#region src/VitestTransactionIsolator.ts
|
|
6
|
-
/**
|
|
7
|
-
* PostgreSQL transaction isolation levels.
|
|
8
|
-
* Controls the visibility of concurrent transactions.
|
|
9
|
-
*
|
|
10
|
-
* @see https://www.postgresql.org/docs/current/transaction-iso.html
|
|
11
|
-
*/
|
|
12
|
-
let IsolationLevel = /* @__PURE__ */ function(IsolationLevel$1) {
|
|
13
|
-
/**
|
|
14
|
-
* Lowest isolation level. Allows dirty reads.
|
|
15
|
-
* Not recommended for testing.
|
|
16
|
-
*/
|
|
17
|
-
IsolationLevel$1["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
18
|
-
/**
|
|
19
|
-
* Default PostgreSQL isolation level.
|
|
20
|
-
* Prevents dirty reads but allows non-repeatable reads.
|
|
21
|
-
*/
|
|
22
|
-
IsolationLevel$1["READ_COMMITTED"] = "READ COMMITTED";
|
|
23
|
-
/**
|
|
24
|
-
* Prevents dirty reads and non-repeatable reads.
|
|
25
|
-
* Recommended for most test scenarios.
|
|
26
|
-
*/
|
|
27
|
-
IsolationLevel$1["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
28
|
-
/**
|
|
29
|
-
* Highest isolation level. Prevents all phenomena.
|
|
30
|
-
* May cause performance overhead in tests.
|
|
31
|
-
*/
|
|
32
|
-
IsolationLevel$1["SERIALIZABLE"] = "SERIALIZABLE";
|
|
33
|
-
return IsolationLevel$1;
|
|
34
|
-
}({});
|
|
35
|
-
/**
|
|
36
|
-
* Abstract base class for implementing database transaction isolation in Vitest tests.
|
|
37
|
-
* Provides automatic transaction rollback after each test to maintain test isolation.
|
|
38
|
-
* Subclasses must implement the transact() method for their specific database driver.
|
|
39
|
-
*
|
|
40
|
-
* @template TConn - The database connection type
|
|
41
|
-
* @template Transaction - The transaction type
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* // Implement for your database driver
|
|
46
|
-
* class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
|
|
47
|
-
* async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
|
|
48
|
-
* await conn.transaction(level, fn);
|
|
49
|
-
* }
|
|
50
|
-
* }
|
|
51
|
-
*
|
|
52
|
-
* // Use in tests
|
|
53
|
-
* const isolator = new MyDatabaseIsolator(test);
|
|
54
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction(db);
|
|
55
|
-
*
|
|
56
|
-
* isolatedTest('should create user', async ({ trx }) => {
|
|
57
|
-
* await trx.insert('users', { name: 'Test' });
|
|
58
|
-
* // Data is automatically rolled back after test
|
|
59
|
-
* });
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
var VitestPostgresTransactionIsolator = class {
|
|
63
|
-
/**
|
|
64
|
-
* Creates a new VitestPostgresTransactionIsolator instance.
|
|
65
|
-
*
|
|
66
|
-
* @param api - The Vitest test API (usually the `test` export from vitest)
|
|
67
|
-
*/
|
|
68
|
-
constructor(api) {
|
|
69
|
-
this.api = api;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
73
|
-
* Each test will run within a database transaction that is automatically rolled back.
|
|
74
|
-
*
|
|
75
|
-
* @param options - Configuration options for transaction wrapping
|
|
76
|
-
* @returns A wrapped test API with transaction support
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```typescript
|
|
80
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction({
|
|
81
|
-
* connection: db,
|
|
82
|
-
* setup: async (trx) => {
|
|
83
|
-
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
84
|
-
* },
|
|
85
|
-
* fixtures: {
|
|
86
|
-
* factory: (trx) => new Factory(trx),
|
|
87
|
-
* },
|
|
88
|
-
* });
|
|
89
|
-
*
|
|
90
|
-
* isolatedTest('test with transaction', async ({ trx, factory }) => {
|
|
91
|
-
* const user = await factory.insert('user', { name: 'Test' });
|
|
92
|
-
* expect(user).toBeDefined();
|
|
93
|
-
* });
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
wrapVitestWithTransaction(options) {
|
|
97
|
-
const { connection, setup, isolationLevel = IsolationLevel.REPEATABLE_READ, fixtures } = options;
|
|
98
|
-
let resolved;
|
|
99
|
-
const resolveConnection = async () => {
|
|
100
|
-
if (resolved === void 0) resolved = await (isConstruct(connection) ? connection.service.register({
|
|
101
|
-
envParser: new __geekmidas_envkit.EnvironmentParser({ ...process.env }),
|
|
102
|
-
context: __geekmidas_services.serviceContext
|
|
103
|
-
}) : connection());
|
|
104
|
-
return resolved;
|
|
105
|
-
};
|
|
106
|
-
const additionalFixtures = {};
|
|
107
|
-
if (fixtures) for (const [key, creator] of Object.entries(fixtures)) additionalFixtures[key] = async ({ trx }, use) => {
|
|
108
|
-
const value = await creator(trx);
|
|
109
|
-
await use(value);
|
|
110
|
-
};
|
|
111
|
-
const api = this.api;
|
|
112
|
-
return api.extend({
|
|
113
|
-
trx: async ({}, use) => {
|
|
114
|
-
class TestRollback extends Error {
|
|
115
|
-
constructor() {
|
|
116
|
-
super("Test rollback");
|
|
117
|
-
this.name = "TestRollback";
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
let testError;
|
|
121
|
-
const conn = await resolveConnection();
|
|
122
|
-
try {
|
|
123
|
-
await this.transact(conn, isolationLevel, async (transaction) => {
|
|
124
|
-
try {
|
|
125
|
-
await setup?.(transaction);
|
|
126
|
-
await use(transaction);
|
|
127
|
-
} catch (error) {
|
|
128
|
-
testError = error;
|
|
129
|
-
}
|
|
130
|
-
throw new TestRollback();
|
|
131
|
-
});
|
|
132
|
-
} catch (error) {
|
|
133
|
-
if (!(error instanceof TestRollback)) throw error;
|
|
134
|
-
if (testError) throw testError;
|
|
135
|
-
}
|
|
136
|
-
},
|
|
137
|
-
...additionalFixtures
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
/** A construct is distinguished from a factory by owning a service. */
|
|
142
|
-
function isConstruct(connection) {
|
|
143
|
-
return typeof connection !== "function";
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Extends a wrapped test API with additional fixtures that depend on the transaction.
|
|
147
|
-
* This allows composing test context with factories, repositories, or other helpers.
|
|
148
|
-
*
|
|
149
|
-
* @template Transaction - The transaction type
|
|
150
|
-
* @template Extended - The type of additional context to provide
|
|
151
|
-
* @param wrappedTest - The base wrapped test from wrapVitestWithTransaction
|
|
152
|
-
* @param fixtures - Object mapping fixture names to creator functions
|
|
153
|
-
* @returns An extended test API with both trx and the additional fixtures
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```typescript
|
|
157
|
-
* import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
|
|
158
|
-
*
|
|
159
|
-
* // Create base wrapped test
|
|
160
|
-
* const baseTest = wrapVitestKyselyTransaction(test, {
|
|
161
|
-
* connection: db,
|
|
162
|
-
* setup: createTestTables,
|
|
163
|
-
* });
|
|
164
|
-
*
|
|
165
|
-
* // Extend with fixtures
|
|
166
|
-
* const it = extendWithFixtures(baseTest, {
|
|
167
|
-
* factory: (trx) => new KyselyFactory(builders, seeds, trx),
|
|
168
|
-
* userRepo: (trx) => new UserRepository(trx),
|
|
169
|
-
* });
|
|
170
|
-
*
|
|
171
|
-
* // Use in tests - trx and all fixtures are available
|
|
172
|
-
* it('should create user with factory', async ({ trx, factory, userRepo }) => {
|
|
173
|
-
* const user = await factory.insert('user', { name: 'Test' });
|
|
174
|
-
* expect(user).toBeDefined();
|
|
175
|
-
* });
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
function extendWithFixtures(wrappedTest, fixtures) {
|
|
179
|
-
const fixtureDefinitions = {};
|
|
180
|
-
for (const [key, creator] of Object.entries(fixtures)) fixtureDefinitions[key] = async ({ trx }, use) => {
|
|
181
|
-
const value = await creator(trx);
|
|
182
|
-
await use(value);
|
|
183
|
-
};
|
|
184
|
-
return wrappedTest.extend(fixtureDefinitions);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
//#endregion
|
|
188
|
-
Object.defineProperty(exports, 'IsolationLevel', {
|
|
189
|
-
enumerable: true,
|
|
190
|
-
get: function () {
|
|
191
|
-
return IsolationLevel;
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
Object.defineProperty(exports, 'VitestPostgresTransactionIsolator', {
|
|
195
|
-
enumerable: true,
|
|
196
|
-
get: function () {
|
|
197
|
-
return VitestPostgresTransactionIsolator;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
Object.defineProperty(exports, 'extendWithFixtures', {
|
|
201
|
-
enumerable: true,
|
|
202
|
-
get: function () {
|
|
203
|
-
return extendWithFixtures;
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
//# sourceMappingURL=VitestTransactionIsolator-DyiX-QtK.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestTransactionIsolator-DyiX-QtK.cjs","names":["api: TestAPI","options: TransactionWrapperOptions<TConn, Transaction, Extended>","resolved: TConn | undefined","EnvironmentParser","serviceContext","additionalFixtures: Record<string, unknown>","use: (value: unknown) => Promise<void>","use: (value: Transaction) => Promise<void>","testError: Error | undefined","connection: DatabaseConnection<Conn>","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>"],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":["import { EnvironmentParser } from '@geekmidas/envkit';\nimport { type Service, serviceContext } from '@geekmidas/services';\nimport 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\t/**\n\t * The database transaction available to the test.\n\t * All database operations should use this transaction to ensure proper rollback.\n\t */\n\ttrx: Transaction;\n}\n\n/**\n * Combined fixtures type that merges the base transaction fixture with extended context.\n */\nexport type ExtendedDatabaseFixtures<\n\tTransaction,\n\tExtended = 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\ttrx: 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\t/**\n\t * Lowest isolation level. Allows dirty reads.\n\t * Not recommended for testing.\n\t */\n\tREAD_UNCOMMITTED = 'READ UNCOMMITTED',\n\t/**\n\t * Default PostgreSQL isolation level.\n\t * Prevents dirty reads but allows non-repeatable reads.\n\t */\n\tREAD_COMMITTED = 'READ COMMITTED',\n\t/**\n\t * Prevents dirty reads and non-repeatable reads.\n\t * Recommended for most test scenarios.\n\t */\n\tREPEATABLE_READ = 'REPEATABLE READ',\n\t/**\n\t * Highest isolation level. Prevents all phenomena.\n\t * May cause performance overhead in tests.\n\t */\n\tSERIALIZABLE = '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\t/**\n\t * Abstract method to create a transaction with the specified isolation level.\n\t * Must be implemented by subclasses for specific database drivers.\n\t *\n\t * @param conn - The database connection\n\t * @param isolationLevel - The transaction isolation level\n\t * @param fn - The function to execute within the transaction\n\t * @returns Promise that resolves when the transaction completes\n\t */\n\tabstract transact(\n\t\tconn: TConn,\n\t\tisolationLevel: IsolationLevel,\n\t\tfn: (trx: Transaction) => Promise<void>,\n\t): Promise<void>;\n\n\tabstract destroy(conn: TConn): Promise<void>;\n\t/**\n\t * Creates a new VitestPostgresTransactionIsolator instance.\n\t *\n\t * @param api - The Vitest test API (usually the `test` export from vitest)\n\t */\n\tconstructor(private readonly api: TestAPI) {}\n\n\t/**\n\t * Creates a wrapped version of Vitest's test API that provides transaction isolation.\n\t * Each test will run within a database transaction that is automatically rolled back.\n\t *\n\t * @param options - Configuration options for transaction wrapping\n\t * @returns A wrapped test API with transaction support\n\t *\n\t * @example\n\t * ```typescript\n\t * const isolatedTest = isolator.wrapVitestWithTransaction({\n\t * connection: db,\n\t * setup: async (trx) => {\n\t * await trx.insert('settings', { key: 'test', value: 'true' });\n\t * },\n\t * fixtures: {\n\t * factory: (trx) => new Factory(trx),\n\t * },\n\t * });\n\t *\n\t * isolatedTest('test with transaction', async ({ trx, factory }) => {\n\t * const user = await factory.insert('user', { name: 'Test' });\n\t * expect(user).toBeDefined();\n\t * });\n\t * ```\n\t */\n\twrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(\n\t\toptions: TransactionWrapperOptions<TConn, Transaction, Extended>,\n\t) {\n\t\tconst {\n\t\t\tconnection,\n\t\t\tsetup,\n\t\t\tisolationLevel = IsolationLevel.REPEATABLE_READ,\n\t\t\tfixtures,\n\t\t} = options;\n\n\t\t// Resolved once per file, not once per test. The fixture below runs for\n\t\t// every test, and opening a pool each time leaves one connection per test\n\t\t// alive — a file of fifty tests exhausts the server.\n\t\tlet resolved: TConn | undefined;\n\t\tconst resolveConnection = async (): Promise<TConn> => {\n\t\t\tif (resolved === undefined) {\n\t\t\t\tresolved = await (isConstruct(connection)\n\t\t\t\t\t? connection.service.register({\n\t\t\t\t\t\t\t// Spread: `process.env` is a ProcessEnv, not a plain object.\n\t\t\t\t\t\t\tenvParser: new EnvironmentParser({ ...process.env }),\n\t\t\t\t\t\t\tcontext: serviceContext,\n\t\t\t\t\t\t})\n\t\t\t\t\t: connection());\n\t\t\t}\n\t\t\treturn resolved;\n\t\t};\n\n\t\t// Build fixture definitions for additional fixtures that depend on trx\n\t\tconst additionalFixtures: Record<string, unknown> = {};\n\t\tif (fixtures) {\n\t\t\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\t\t\tadditionalFixtures[key] = async (\n\t\t\t\t\t{ trx }: { trx: Transaction },\n\t\t\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t\t\t) => {\n\t\t\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\t\t\tawait use(value);\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\ttype CombinedFixtures = DatabaseFixtures<Transaction> & Extended;\n\n\t\t// Cast to bypass Vitest's strict fixture typing which can't infer\n\t\t// dynamically built fixture objects\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tconst api = this.api as TestAPI & {\n\t\t\textend: <T>(fixtures: any) => TestAPI<T>;\n\t\t};\n\n\t\treturn api.extend<CombinedFixtures>({\n\t\t\t// This fixture automatically provides a transaction to each test\n\t\t\t// biome-ignore lint/correctness/noEmptyPattern: this has to be like this to satisfy Biome\n\t\t\ttrx: async ({}: {}, use: (value: Transaction) => Promise<void>) => {\n\t\t\t\t// Create a custom error class for rollback\n\t\t\t\tclass TestRollback extends Error {\n\t\t\t\t\tconstructor() {\n\t\t\t\t\t\tsuper('Test rollback');\n\t\t\t\t\t\tthis.name = 'TestRollback';\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tlet testError: Error | undefined;\n\t\t\t\tconst conn = await resolveConnection();\n\t\t\t\ttry {\n\t\t\t\t\tawait this.transact(conn, isolationLevel, async (transaction) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// Provide the transaction to the test\n\t\t\t\t\t\t\tawait setup?.(transaction);\n\t\t\t\t\t\t\tawait use(transaction);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t// Capture any test errors\n\t\t\t\t\t\t\ttestError = error as Error;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Always throw to trigger rollback\n\t\t\t\t\t\tthrow new TestRollback();\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Only rethrow if it's not our rollback error\n\t\t\t\t\tif (!(error instanceof TestRollback)) {\n\t\t\t\t\t\tthrow error;\n\t\t\t\t\t}\n\n\t\t\t\t\t// If the test had an error, throw it now\n\t\t\t\t\tif (testError) {\n\t\t\t\t\t\tthrow testError;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t...additionalFixtures,\n\t\t});\n\t}\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\n\n/**\n * Where a connection comes from: a construct, or a function returning one.\n *\n * A construct is preferred, because it builds the client through the same\n * `connect()` production uses. A hand-rolled client missing `CamelCasePlugin`\n * writes `createdAt` where the app writes `created_at`, and the test passes\n * while production does not. The URL is read from `process.env`.\n */\nexport type DatabaseConnection<Conn> =\n\t| DatabaseConnectionFn<Conn>\n\t| ConnectableConstruct<Conn>;\n\n/** A construct is distinguished from a factory by owning a service. */\nfunction isConstruct<Conn>(\n\tconnection: DatabaseConnection<Conn>,\n): connection is ConnectableConstruct<Conn> {\n\treturn typeof connection !== 'function';\n}\n\n/**\n * Options for wrapping Vitest tests with database transaction isolation.\n */\n/**\n * Anything that owns a service handing back a connection — a database\n * construct, structurally, so testkit depends on no construct package.\n */\nexport interface ConnectableConstruct<TConn> {\n\treadonly service: Service<string, TConn>;\n}\n\nexport interface TransactionWrapperOptions<\n\tTConn,\n\tTransaction,\n\tExtended extends Record<string, unknown> = {},\n> {\n\t/** Where the connection comes from — a construct, or a function returning one */\n\tconnection: DatabaseConnection<TConn>;\n\t/** Optional setup function to run within the transaction before each test */\n\tsetup?: (trx: Transaction) => Promise<void>;\n\t/** Transaction isolation level (defaults to REPEATABLE_READ) */\n\tisolationLevel?: IsolationLevel;\n\t/** Additional fixtures that depend on the transaction */\n\tfixtures?: 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\tTransaction,\n\tExtended extends Record<string, unknown>,\n> = {\n\t[K in keyof Extended]: (\n\t\ttrx: Transaction,\n\t) => 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\tTransaction,\n\tExtended extends Record<string, unknown>,\n\tBaseTest extends ReturnType<TestAPI['extend']> = ReturnType<\n\t\tTestAPI['extend']\n\t>,\n> = BaseTest & {\n\t<C extends object>(\n\t\tname: string,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): void;\n\t<C extends object>(\n\t\tname: string,\n\t\toptions: object,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): 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\tTransaction,\n\tExtended extends Record<string, unknown>,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends ReturnType<TestAPI['extend']> = any,\n>(\n\twrappedTest: T,\n\tfixtures: FixtureCreators<Transaction, Extended>,\n): TestWithExtendedFixtures<Transaction, Extended, T> {\n\t// Build fixture definitions for Vitest's extend API\n\tconst fixtureDefinitions: Record<string, any> = {};\n\n\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\tfixtureDefinitions[key] = async (\n\t\t\t{ trx }: { trx: Transaction },\n\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t) => {\n\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\tawait use(value);\n\t\t};\n\t}\n\n\treturn (wrappedTest as any).extend(fixtureDefinitions);\n}\n"],"mappings":";;;;;;;;;;;AAkDA,IAAY,4DAAL;;;;;AAKN;;;;;AAKA;;;;;AAKA;;;;;AAKA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BD,IAAsB,oCAAtB,MAA4E;;;;;;CAsB3E,YAA6BA,KAAc;EAAd;CAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B7C,0BACCC,SACC;EACD,MAAM,EACL,YACA,OACA,iBAAiB,eAAe,iBAChC,UACA,GAAG;EAKJ,IAAIC;EACJ,MAAM,oBAAoB,YAA4B;AACrD,OAAI,oBACH,YAAW,OAAO,YAAY,WAAW,GACtC,WAAW,QAAQ,SAAS;IAE5B,WAAW,IAAIC,qCAAkB,EAAE,GAAG,QAAQ,IAAK;IACnD,SAASC;GACT,EAAC,GACD,YAAY;AAEhB,UAAO;EACP;EAGD,MAAMC,qBAA8C,CAAE;AACtD,MAAI,SACH,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BC,QACI;GACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,SAAM,IAAI,MAAM;EAChB;EASH,MAAM,MAAM,KAAK;AAIjB,SAAO,IAAI,OAAyB;GAGnC,KAAK,OAAO,EAAM,EAAEC,QAA+C;IAElE,MAAM,qBAAqB,MAAM;KAChC,cAAc;AACb,YAAM,gBAAgB;AACtB,WAAK,OAAO;KACZ;IACD;IAED,IAAIC;IACJ,MAAM,OAAO,MAAM,mBAAmB;AACtC,QAAI;AACH,WAAM,KAAK,SAAS,MAAM,gBAAgB,OAAO,gBAAgB;AAChE,UAAI;AAEH,aAAM,QAAQ,YAAY;AAC1B,aAAM,IAAI,YAAY;MACtB,SAAQ,OAAO;AAEf,mBAAY;MACZ;AAGD,YAAM,IAAI;KACV,EAAC;IACF,SAAQ,OAAO;AAEf,WAAM,iBAAiB,cACtB,OAAM;AAIP,SAAI,UACH,OAAM;IAEP;GACD;GACD,GAAG;EACH,EAAC;CACF;AACD;;AAiBD,SAAS,YACRC,YAC2C;AAC3C,eAAc,eAAe;AAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwGD,SAAgB,mBAMfC,aACAC,UACqD;CAErD,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BN,QACI;EACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CAChB;AAGF,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACtD"}
|