@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.
Files changed (295) hide show
  1. package/README.md +74 -0
  2. package/dist/Factory.cjs +51 -2
  3. package/dist/Factory.cjs.map +1 -0
  4. package/dist/Factory.d.cts +143 -3
  5. package/dist/Factory.d.cts.map +1 -0
  6. package/dist/Factory.d.mts +143 -3
  7. package/dist/Factory.d.mts.map +1 -0
  8. package/dist/Factory.mjs +50 -2
  9. package/dist/Factory.mjs.map +1 -0
  10. package/dist/KyselyFactory.cjs +237 -4
  11. package/dist/KyselyFactory.cjs.map +1 -0
  12. package/dist/KyselyFactory.d.cts +224 -4
  13. package/dist/KyselyFactory.d.cts.map +1 -0
  14. package/dist/KyselyFactory.d.mts +224 -4
  15. package/dist/KyselyFactory.d.mts.map +1 -0
  16. package/dist/KyselyFactory.mjs +236 -4
  17. package/dist/KyselyFactory.mjs.map +1 -0
  18. package/dist/ObjectionFactory.cjs +265 -4
  19. package/dist/ObjectionFactory.cjs.map +1 -0
  20. package/dist/ObjectionFactory.d.cts +237 -4
  21. package/dist/ObjectionFactory.d.cts.map +1 -0
  22. package/dist/ObjectionFactory.d.mts +237 -4
  23. package/dist/ObjectionFactory.d.mts.map +1 -0
  24. package/dist/ObjectionFactory.mjs +264 -4
  25. package/dist/ObjectionFactory.mjs.map +1 -0
  26. package/dist/PostgresKyselyMigrator.cjs +94 -3
  27. package/dist/PostgresKyselyMigrator.cjs.map +1 -0
  28. package/dist/PostgresKyselyMigrator.d.cts +80 -3
  29. package/dist/PostgresKyselyMigrator.d.cts.map +1 -0
  30. package/dist/PostgresKyselyMigrator.d.mts +80 -3
  31. package/dist/PostgresKyselyMigrator.d.mts.map +1 -0
  32. package/dist/PostgresKyselyMigrator.mjs +93 -3
  33. package/dist/PostgresKyselyMigrator.mjs.map +1 -0
  34. package/dist/{PostgresMigrator-C_QQ6q35.d.mts → PostgresMigrator-CDPnnwMJ.d.cts} +11 -5
  35. package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
  36. package/dist/{PostgresMigrator-CeYy-eHF.d.cts → PostgresMigrator-CDPnnwMJ.d.mts} +11 -5
  37. package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
  38. package/dist/PostgresMigrator.cjs +178 -2
  39. package/dist/PostgresMigrator.cjs.map +1 -0
  40. package/dist/PostgresMigrator.d.cts +1 -1
  41. package/dist/PostgresMigrator.d.mts +1 -1
  42. package/dist/PostgresMigrator.mjs +175 -2
  43. package/dist/PostgresMigrator.mjs.map +1 -0
  44. package/dist/PostgresObjectionMigrator.cjs +118 -3
  45. package/dist/PostgresObjectionMigrator.cjs.map +1 -0
  46. package/dist/PostgresObjectionMigrator.d.cts +75 -3
  47. package/dist/PostgresObjectionMigrator.d.cts.map +1 -0
  48. package/dist/PostgresObjectionMigrator.d.mts +75 -3
  49. package/dist/PostgresObjectionMigrator.d.mts.map +1 -0
  50. package/dist/PostgresObjectionMigrator.mjs +117 -3
  51. package/dist/PostgresObjectionMigrator.mjs.map +1 -0
  52. package/dist/VitestKyselyTransactionIsolator.cjs +66 -3
  53. package/dist/VitestKyselyTransactionIsolator.cjs.map +1 -0
  54. package/dist/VitestKyselyTransactionIsolator.d.cts +60 -3
  55. package/dist/VitestKyselyTransactionIsolator.d.cts.map +1 -0
  56. package/dist/VitestKyselyTransactionIsolator.d.mts +60 -3
  57. package/dist/VitestKyselyTransactionIsolator.d.mts.map +1 -0
  58. package/dist/VitestKyselyTransactionIsolator.mjs +65 -3
  59. package/dist/VitestKyselyTransactionIsolator.mjs.map +1 -0
  60. package/dist/VitestObjectionTransactionIsolator.cjs +62 -3
  61. package/dist/VitestObjectionTransactionIsolator.cjs.map +1 -0
  62. package/dist/VitestObjectionTransactionIsolator.d.cts +56 -3
  63. package/dist/VitestObjectionTransactionIsolator.d.cts.map +1 -0
  64. package/dist/VitestObjectionTransactionIsolator.d.mts +56 -3
  65. package/dist/VitestObjectionTransactionIsolator.d.mts.map +1 -0
  66. package/dist/VitestObjectionTransactionIsolator.mjs +61 -3
  67. package/dist/VitestObjectionTransactionIsolator.mjs.map +1 -0
  68. package/dist/VitestTransactionIsolator.cjs +187 -4
  69. package/dist/VitestTransactionIsolator.cjs.map +1 -0
  70. package/dist/VitestTransactionIsolator.d.cts +219 -2
  71. package/dist/VitestTransactionIsolator.d.cts.map +1 -0
  72. package/dist/VitestTransactionIsolator.d.mts +219 -2
  73. package/dist/VitestTransactionIsolator.d.mts.map +1 -0
  74. package/dist/VitestTransactionIsolator.mjs +184 -2
  75. package/dist/VitestTransactionIsolator.mjs.map +1 -0
  76. package/dist/aws.cjs +3 -4
  77. package/dist/aws.cjs.map +1 -1
  78. package/dist/aws.d.cts +3 -8
  79. package/dist/aws.d.cts.map +1 -1
  80. package/dist/aws.d.mts +3 -8
  81. package/dist/aws.d.mts.map +1 -1
  82. package/dist/aws.mjs +1 -2
  83. package/dist/aws.mjs.map +1 -1
  84. package/dist/benchmark.cjs +2 -2
  85. package/dist/benchmark.cjs.map +1 -1
  86. package/dist/benchmark.d.cts +4 -6
  87. package/dist/benchmark.d.cts.map +1 -1
  88. package/dist/benchmark.d.mts +4 -6
  89. package/dist/benchmark.d.mts.map +1 -1
  90. package/dist/benchmark.mjs +1 -1
  91. package/dist/benchmark.mjs.map +1 -1
  92. package/dist/better-auth.cjs +31 -15
  93. package/dist/better-auth.cjs.map +1 -1
  94. package/dist/better-auth.d.cts +13142 -4
  95. package/dist/better-auth.d.cts.map +1 -1
  96. package/dist/better-auth.d.mts +13142 -4
  97. package/dist/better-auth.d.mts.map +1 -1
  98. package/dist/better-auth.mjs +29 -13
  99. package/dist/better-auth.mjs.map +1 -1
  100. package/dist/browser.cjs +127 -0
  101. package/dist/browser.cjs.map +1 -0
  102. package/dist/browser.d.cts +47 -0
  103. package/dist/browser.d.cts.map +1 -0
  104. package/dist/browser.d.mts +47 -0
  105. package/dist/browser.d.mts.map +1 -0
  106. package/dist/browser.mjs +125 -0
  107. package/dist/browser.mjs.map +1 -0
  108. package/dist/context.cjs +80 -0
  109. package/dist/context.cjs.map +1 -0
  110. package/dist/context.d.cts +55 -0
  111. package/dist/context.d.cts.map +1 -0
  112. package/dist/context.d.mts +55 -0
  113. package/dist/context.d.mts.map +1 -0
  114. package/dist/context.mjs +73 -0
  115. package/dist/context.mjs.map +1 -0
  116. package/dist/faker.cjs +262 -8
  117. package/dist/faker.cjs.map +1 -0
  118. package/dist/faker.d.cts +162 -2
  119. package/dist/faker.d.cts.map +1 -0
  120. package/dist/faker.d.mts +162 -2
  121. package/dist/faker.d.mts.map +1 -0
  122. package/dist/faker.mjs +255 -2
  123. package/dist/faker.mjs.map +1 -0
  124. package/dist/helpers.cjs +6 -5
  125. package/dist/helpers.cjs.map +1 -1
  126. package/dist/helpers.d.cts +1 -5
  127. package/dist/helpers.d.cts.map +1 -1
  128. package/dist/helpers.d.mts +1 -5
  129. package/dist/helpers.d.mts.map +1 -1
  130. package/dist/helpers.mjs +1 -2
  131. package/dist/helpers.mjs.map +1 -1
  132. package/dist/initScript.cjs +15 -15
  133. package/dist/initScript.cjs.map +1 -1
  134. package/dist/initScript.d.cts +2 -4
  135. package/dist/initScript.d.cts.map +1 -1
  136. package/dist/initScript.d.mts +2 -4
  137. package/dist/initScript.d.mts.map +1 -1
  138. package/dist/initScript.mjs +10 -12
  139. package/dist/initScript.mjs.map +1 -1
  140. package/dist/kysely.cjs +10 -13
  141. package/dist/kysely.cjs.map +1 -1
  142. package/dist/kysely.d.cts +10 -14
  143. package/dist/kysely.d.cts.map +1 -1
  144. package/dist/kysely.d.mts +10 -14
  145. package/dist/kysely.d.mts.map +1 -1
  146. package/dist/kysely.mjs +10 -14
  147. package/dist/kysely.mjs.map +1 -1
  148. package/dist/logger.cjs +3 -4
  149. package/dist/logger.cjs.map +1 -1
  150. package/dist/logger.d.cts +1 -5
  151. package/dist/logger.d.cts.map +1 -1
  152. package/dist/logger.d.mts +1 -5
  153. package/dist/logger.d.mts.map +1 -1
  154. package/dist/logger.mjs +1 -2
  155. package/dist/logger.mjs.map +1 -1
  156. package/dist/mailbox.cjs +95 -0
  157. package/dist/mailbox.cjs.map +1 -0
  158. package/dist/mailbox.d.cts +58 -0
  159. package/dist/mailbox.d.cts.map +1 -0
  160. package/dist/mailbox.d.mts +58 -0
  161. package/dist/mailbox.d.mts.map +1 -0
  162. package/dist/mailbox.mjs +92 -0
  163. package/dist/mailbox.mjs.map +1 -0
  164. package/dist/objection.cjs +10 -13
  165. package/dist/objection.cjs.map +1 -1
  166. package/dist/objection.d.cts +10 -14
  167. package/dist/objection.d.cts.map +1 -1
  168. package/dist/objection.d.mts +10 -14
  169. package/dist/objection.d.mts.map +1 -1
  170. package/dist/objection.mjs +10 -14
  171. package/dist/objection.mjs.map +1 -1
  172. package/dist/os/directory.cjs +31 -2
  173. package/dist/os/directory.cjs.map +1 -0
  174. package/dist/os/directory.d.cts +9 -2
  175. package/dist/os/directory.d.cts.map +1 -0
  176. package/dist/os/directory.d.mts +9 -2
  177. package/dist/os/directory.d.mts.map +1 -0
  178. package/dist/os/directory.mjs +25 -2
  179. package/dist/os/directory.mjs.map +1 -0
  180. package/dist/os/index.cjs +3 -3
  181. package/dist/os/index.d.cts +1 -1
  182. package/dist/os/index.d.mts +1 -1
  183. package/dist/os/index.mjs +2 -3
  184. package/dist/requestContext.cjs +10 -11
  185. package/dist/requestContext.cjs.map +1 -1
  186. package/dist/requestContext.d.cts +4 -8
  187. package/dist/requestContext.d.cts.map +1 -1
  188. package/dist/requestContext.d.mts +4 -8
  189. package/dist/requestContext.d.mts.map +1 -1
  190. package/dist/requestContext.mjs +1 -2
  191. package/dist/requestContext.mjs.map +1 -1
  192. package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-VH7oDXx4.cjs} +8 -10
  193. package/dist/timer.cjs +2 -2
  194. package/dist/timer.cjs.map +1 -1
  195. package/dist/timer.d.cts +1 -4
  196. package/dist/timer.d.cts.map +1 -1
  197. package/dist/timer.d.mts +1 -4
  198. package/dist/timer.d.mts.map +1 -1
  199. package/dist/timer.mjs +1 -1
  200. package/dist/timer.mjs.map +1 -1
  201. package/dist/transactions.cjs +123 -0
  202. package/dist/transactions.cjs.map +1 -0
  203. package/dist/transactions.d.cts +44 -0
  204. package/dist/transactions.d.cts.map +1 -0
  205. package/dist/transactions.d.mts +44 -0
  206. package/dist/transactions.d.mts.map +1 -0
  207. package/dist/transactions.mjs +118 -0
  208. package/dist/transactions.mjs.map +1 -0
  209. package/package.json +59 -16
  210. package/dist/Factory-BFVnMMCC.mjs +0 -51
  211. package/dist/Factory-BFVnMMCC.mjs.map +0 -1
  212. package/dist/Factory-BOX312yd.d.cts +0 -147
  213. package/dist/Factory-BOX312yd.d.cts.map +0 -1
  214. package/dist/Factory-BhjUOBWN.cjs +0 -57
  215. package/dist/Factory-BhjUOBWN.cjs.map +0 -1
  216. package/dist/Factory-C6W78ulZ.d.mts +0 -147
  217. package/dist/Factory-C6W78ulZ.d.mts.map +0 -1
  218. package/dist/KyselyFactory-BFqVIn_0.cjs +0 -246
  219. package/dist/KyselyFactory-BFqVIn_0.cjs.map +0 -1
  220. package/dist/KyselyFactory-Cc2UmOJk.d.mts +0 -228
  221. package/dist/KyselyFactory-Cc2UmOJk.d.mts.map +0 -1
  222. package/dist/KyselyFactory-DMswpwji.mjs +0 -241
  223. package/dist/KyselyFactory-DMswpwji.mjs.map +0 -1
  224. package/dist/KyselyFactory-Dy5zzV4B.d.cts +0 -228
  225. package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +0 -1
  226. package/dist/ObjectionFactory-BYnPr9ZP.d.mts +0 -241
  227. package/dist/ObjectionFactory-BYnPr9ZP.d.mts.map +0 -1
  228. package/dist/ObjectionFactory-BeFBYcan.cjs +0 -272
  229. package/dist/ObjectionFactory-BeFBYcan.cjs.map +0 -1
  230. package/dist/ObjectionFactory-CD-WFuMJ.d.cts +0 -241
  231. package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +0 -1
  232. package/dist/ObjectionFactory-QCJ7u0Ql.mjs +0 -267
  233. package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +0 -1
  234. package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts +0 -84
  235. package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts.map +0 -1
  236. package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs +0 -101
  237. package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs.map +0 -1
  238. package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts +0 -84
  239. package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts.map +0 -1
  240. package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs +0 -95
  241. package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs.map +0 -1
  242. package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +0 -1
  243. package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +0 -1
  244. package/dist/PostgresMigrator-DVAY04qN.mjs +0 -166
  245. package/dist/PostgresMigrator-DVAY04qN.mjs.map +0 -1
  246. package/dist/PostgresMigrator-M9jpzOvN.cjs +0 -172
  247. package/dist/PostgresMigrator-M9jpzOvN.cjs.map +0 -1
  248. package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts +0 -79
  249. package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts.map +0 -1
  250. package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts +0 -79
  251. package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts.map +0 -1
  252. package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs +0 -123
  253. package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs.map +0 -1
  254. package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs +0 -118
  255. package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs.map +0 -1
  256. package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs +0 -67
  257. package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +0 -1
  258. package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs +0 -72
  259. package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +0 -1
  260. package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts +0 -64
  261. package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +0 -1
  262. package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts +0 -64
  263. package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +0 -1
  264. package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs +0 -63
  265. package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs.map +0 -1
  266. package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts +0 -60
  267. package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map +0 -1
  268. package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts +0 -60
  269. package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map +0 -1
  270. package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs +0 -68
  271. package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map +0 -1
  272. package/dist/VitestTransactionIsolator-BLaw80cx.d.mts +0 -223
  273. package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +0 -1
  274. package/dist/VitestTransactionIsolator-CvwPecpl.mjs +0 -188
  275. package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +0 -1
  276. package/dist/VitestTransactionIsolator-DyiX-QtK.cjs +0 -206
  277. package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +0 -1
  278. package/dist/VitestTransactionIsolator-glcxyS_R.d.cts +0 -223
  279. package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +0 -1
  280. package/dist/directory-BxjqmtCT.mjs +0 -27
  281. package/dist/directory-BxjqmtCT.mjs.map +0 -1
  282. package/dist/directory-DD_xQoSh.cjs +0 -33
  283. package/dist/directory-DD_xQoSh.cjs.map +0 -1
  284. package/dist/directory-DGOcVlKD.d.cts +0 -13
  285. package/dist/directory-DGOcVlKD.d.cts.map +0 -1
  286. package/dist/directory-DiQ_DkaF.d.mts +0 -13
  287. package/dist/directory-DiQ_DkaF.d.mts.map +0 -1
  288. package/dist/faker-B14IEMIN.cjs +0 -304
  289. package/dist/faker-B14IEMIN.cjs.map +0 -1
  290. package/dist/faker-BGKYFoCT.mjs +0 -262
  291. package/dist/faker-BGKYFoCT.mjs.map +0 -1
  292. package/dist/faker-Cg76aFNO.d.cts +0 -166
  293. package/dist/faker-Cg76aFNO.d.cts.map +0 -1
  294. package/dist/faker-D9gz7KjY.d.mts +0 -166
  295. package/dist/faker-D9gz7KjY.d.mts.map +0 -1
@@ -1,5 +1,188 @@
1
- const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-DyiX-QtK.cjs');
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _geekmidas_envkit = require("@geekmidas/envkit");
3
+ let _geekmidas_services = require("@geekmidas/services");
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) {
12
+ /**
13
+ * Lowest isolation level. Allows dirty reads.
14
+ * Not recommended for testing.
15
+ */
16
+ IsolationLevel["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
17
+ /**
18
+ * Default PostgreSQL isolation level.
19
+ * Prevents dirty reads but allows non-repeatable reads.
20
+ */
21
+ IsolationLevel["READ_COMMITTED"] = "READ COMMITTED";
22
+ /**
23
+ * Prevents dirty reads and non-repeatable reads.
24
+ * Recommended for most test scenarios.
25
+ */
26
+ IsolationLevel["REPEATABLE_READ"] = "REPEATABLE READ";
27
+ /**
28
+ * Highest isolation level. Prevents all phenomena.
29
+ * May cause performance overhead in tests.
30
+ */
31
+ IsolationLevel["SERIALIZABLE"] = "SERIALIZABLE";
32
+ return IsolationLevel;
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
+ api;
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 = "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
+ await use(await creator(trx));
109
+ };
110
+ return this.api.extend({
111
+ trx: async ({}, use) => {
112
+ class TestRollback extends Error {
113
+ constructor() {
114
+ super("Test rollback");
115
+ this.name = "TestRollback";
116
+ }
117
+ }
118
+ let testError;
119
+ const conn = await resolveConnection();
120
+ try {
121
+ await this.transact(conn, isolationLevel, async (transaction) => {
122
+ try {
123
+ await setup?.(transaction);
124
+ await use(transaction);
125
+ } catch (error) {
126
+ testError = error;
127
+ }
128
+ throw new TestRollback();
129
+ });
130
+ } catch (error) {
131
+ if (!(error instanceof TestRollback)) throw error;
132
+ if (testError) throw testError;
133
+ }
134
+ },
135
+ ...additionalFixtures
136
+ });
137
+ }
138
+ };
139
+ /** A construct is distinguished from a factory by owning a service. */
140
+ function isConstruct(connection) {
141
+ return typeof connection !== "function";
142
+ }
143
+ /**
144
+ * Extends a wrapped test API with additional fixtures that depend on the transaction.
145
+ * This allows composing test context with factories, repositories, or other helpers.
146
+ *
147
+ * @template Transaction - The transaction type
148
+ * @template Extended - The type of additional context to provide
149
+ * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction
150
+ * @param fixtures - Object mapping fixture names to creator functions
151
+ * @returns An extended test API with both trx and the additional fixtures
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
156
+ *
157
+ * // Create base wrapped test
158
+ * const baseTest = wrapVitestKyselyTransaction(test, {
159
+ * connection: db,
160
+ * setup: createTestTables,
161
+ * });
162
+ *
163
+ * // Extend with fixtures
164
+ * const it = extendWithFixtures(baseTest, {
165
+ * factory: (trx) => new KyselyFactory(builders, seeds, trx),
166
+ * userRepo: (trx) => new UserRepository(trx),
167
+ * });
168
+ *
169
+ * // Use in tests - trx and all fixtures are available
170
+ * it('should create user with factory', async ({ trx, factory, userRepo }) => {
171
+ * const user = await factory.insert('user', { name: 'Test' });
172
+ * expect(user).toBeDefined();
173
+ * });
174
+ * ```
175
+ */
176
+ function extendWithFixtures(wrappedTest, fixtures) {
177
+ const fixtureDefinitions = {};
178
+ for (const [key, creator] of Object.entries(fixtures)) fixtureDefinitions[key] = async ({ trx }, use) => {
179
+ await use(await creator(trx));
180
+ };
181
+ return wrappedTest.extend(fixtureDefinitions);
182
+ }
183
+ //#endregion
184
+ exports.IsolationLevel = IsolationLevel;
185
+ exports.VitestPostgresTransactionIsolator = VitestPostgresTransactionIsolator;
186
+ exports.extendWithFixtures = extendWithFixtures;
2
187
 
3
- exports.IsolationLevel = require_VitestTransactionIsolator.IsolationLevel;
4
- exports.VitestPostgresTransactionIsolator = require_VitestTransactionIsolator.VitestPostgresTransactionIsolator;
5
- exports.extendWithFixtures = require_VitestTransactionIsolator.extendWithFixtures;
188
+ //# sourceMappingURL=VitestTransactionIsolator.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VitestTransactionIsolator.cjs","names":["EnvironmentParser","serviceContext"],"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,iBAAL,yBAAA,gBAAA;;;;;CAKN,eAAA,sBAAA;;;;;CAKA,eAAA,oBAAA;;;;;CAKA,eAAA,qBAAA;;;;;CAKA,eAAA,kBAAA;;AACD,EAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,IAAsB,oCAAtB,MAA4E;CAsB9C;;;;;;CAA7B,YAAY,KAA+B;EAAd,KAAA,MAAA;CAAe;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B5C,0BACC,SACC;EACD,MAAM,EACL,YACA,OACA,iBAAA,mBACA,aACG;EAKJ,IAAI;EACJ,MAAM,oBAAoB,YAA4B;GACrD,IAAI,aAAa,KAAA,GAChB,WAAW,OAAO,YAAY,UAAU,IACrC,WAAW,QAAQ,SAAS;IAE5B,WAAW,IAAIA,kBAAAA,kBAAkB,EAAE,GAAG,QAAQ,IAAI,CAAC;IACnD,SAASC,oBAAAA;GACV,CAAC,IACA,WAAW;GAEf,OAAO;EACR;EAGA,MAAM,qBAA8C,CAAC;EACrD,IAAI,UACH,KAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,QAAQ,GACnD,mBAAmB,OAAO,OACzB,EAAE,OACF,QACI;GAEJ,MAAM,IAAI,MADW,QAA0C,GAAG,CACnD;EAChB;EAaF,OAJY,KAAK,IAIN,OAAyB;GAGnC,KAAK,OAAO,IAAQ,QAA+C;IAElE,MAAM,qBAAqB,MAAM;KAChC,cAAc;MACb,MAAM,eAAe;MACrB,KAAK,OAAO;KACb;IACD;IAEA,IAAI;IACJ,MAAM,OAAO,MAAM,kBAAkB;IACrC,IAAI;KACH,MAAM,KAAK,SAAS,MAAM,gBAAgB,OAAO,gBAAgB;MAChE,IAAI;OAEH,MAAM,QAAQ,WAAW;OACzB,MAAM,IAAI,WAAW;MACtB,SAAS,OAAO;OAEf,YAAY;MACb;MAGA,MAAM,IAAI,aAAa;KACxB,CAAC;IACF,SAAS,OAAO;KAEf,IAAI,EAAE,iBAAiB,eACtB,MAAM;KAIP,IAAI,WACH,MAAM;IAER;GACD;GACA,GAAG;EACJ,CAAC;CACF;AACD;;AAiBA,SAAS,YACR,YAC2C;CAC3C,OAAO,OAAO,eAAe;AAC9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwGA,SAAgB,mBAMf,aACA,UACqD;CAErD,MAAM,qBAA0C,CAAC;CAEjD,KAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,QAAQ,GACnD,mBAAmB,OAAO,OACzB,EAAE,OACF,QACI;EAEJ,MAAM,IAAI,MADW,QAA0C,GAAG,CACnD;CAChB;CAGD,OAAQ,YAAoB,OAAO,kBAAkB;AACtD"}
@@ -1,2 +1,219 @@
1
- import { ConnectableConstruct, DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-glcxyS_R.cjs";
2
- export { ConnectableConstruct, DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures };
1
+ import { Service } from "@geekmidas/services";
2
+ import { TestAPI } from "vitest";
3
+ //#region src/VitestTransactionIsolator.d.ts
4
+ /**
5
+ * Type definition for test fixtures that provide transaction access.
6
+ * Used with Vitest's test.extend() API to inject transactions into tests.
7
+ *
8
+ * @template Transaction - The transaction type specific to the database driver
9
+ * @template Extended - Additional context properties provided by the extend function
10
+ */
11
+ export interface DatabaseFixtures<Transaction, _Extended = object> {
12
+ /**
13
+ * The database transaction available to the test.
14
+ * All database operations should use this transaction to ensure proper rollback.
15
+ */
16
+ trx: Transaction;
17
+ }
18
+ /**
19
+ * Combined fixtures type that merges the base transaction fixture with extended context.
20
+ */
21
+ export type ExtendedDatabaseFixtures<Transaction, Extended = object> = DatabaseFixtures<Transaction> & Extended;
22
+ /**
23
+ * Function type for extending test context with additional properties.
24
+ * Receives the transaction and returns additional context to be merged with { trx }.
25
+ *
26
+ * @template Transaction - The transaction type
27
+ * @template Extended - The type of additional context to provide
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const extendContext: ExtendContextFn<Transaction<DB>, { factory: KyselyFactory }> =
32
+ * (trx) => ({ factory: new KyselyFactory(builders, seeds, trx) });
33
+ * ```
34
+ */
35
+ export type ExtendContextFn<Transaction, Extended> = (trx: Transaction) => Extended | Promise<Extended>;
36
+ /**
37
+ * PostgreSQL transaction isolation levels.
38
+ * Controls the visibility of concurrent transactions.
39
+ *
40
+ * @see https://www.postgresql.org/docs/current/transaction-iso.html
41
+ */
42
+ export declare enum IsolationLevel {
43
+ /**
44
+ * Lowest isolation level. Allows dirty reads.
45
+ * Not recommended for testing.
46
+ */
47
+ READ_UNCOMMITTED = "READ UNCOMMITTED",
48
+ /**
49
+ * Default PostgreSQL isolation level.
50
+ * Prevents dirty reads but allows non-repeatable reads.
51
+ */
52
+ READ_COMMITTED = "READ COMMITTED",
53
+ /**
54
+ * Prevents dirty reads and non-repeatable reads.
55
+ * Recommended for most test scenarios.
56
+ */
57
+ REPEATABLE_READ = "REPEATABLE READ",
58
+ /**
59
+ * Highest isolation level. Prevents all phenomena.
60
+ * May cause performance overhead in tests.
61
+ */
62
+ SERIALIZABLE = "SERIALIZABLE"
63
+ }
64
+ /**
65
+ * Abstract base class for implementing database transaction isolation in Vitest tests.
66
+ * Provides automatic transaction rollback after each test to maintain test isolation.
67
+ * Subclasses must implement the transact() method for their specific database driver.
68
+ *
69
+ * @template TConn - The database connection type
70
+ * @template Transaction - The transaction type
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * // Implement for your database driver
75
+ * class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
76
+ * async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
77
+ * await conn.transaction(level, fn);
78
+ * }
79
+ * }
80
+ *
81
+ * // Use in tests
82
+ * const isolator = new MyDatabaseIsolator(test);
83
+ * const isolatedTest = isolator.wrapVitestWithTransaction(db);
84
+ *
85
+ * isolatedTest('should create user', async ({ trx }) => {
86
+ * await trx.insert('users', { name: 'Test' });
87
+ * // Data is automatically rolled back after test
88
+ * });
89
+ * ```
90
+ */
91
+ export declare abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
92
+ private readonly api;
93
+ /**
94
+ * Abstract method to create a transaction with the specified isolation level.
95
+ * Must be implemented by subclasses for specific database drivers.
96
+ *
97
+ * @param conn - The database connection
98
+ * @param isolationLevel - The transaction isolation level
99
+ * @param fn - The function to execute within the transaction
100
+ * @returns Promise that resolves when the transaction completes
101
+ */
102
+ abstract transact(conn: TConn, isolationLevel: IsolationLevel, fn: (trx: Transaction) => Promise<void>): Promise<void>;
103
+ abstract destroy(conn: TConn): Promise<void>;
104
+ /**
105
+ * Creates a new VitestPostgresTransactionIsolator instance.
106
+ *
107
+ * @param api - The Vitest test API (usually the `test` export from vitest)
108
+ */
109
+ constructor(api: TestAPI);
110
+ /**
111
+ * Creates a wrapped version of Vitest's test API that provides transaction isolation.
112
+ * Each test will run within a database transaction that is automatically rolled back.
113
+ *
114
+ * @param options - Configuration options for transaction wrapping
115
+ * @returns A wrapped test API with transaction support
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * const isolatedTest = isolator.wrapVitestWithTransaction({
120
+ * connection: db,
121
+ * setup: async (trx) => {
122
+ * await trx.insert('settings', { key: 'test', value: 'true' });
123
+ * },
124
+ * fixtures: {
125
+ * factory: (trx) => new Factory(trx),
126
+ * },
127
+ * });
128
+ *
129
+ * isolatedTest('test with transaction', async ({ trx, factory }) => {
130
+ * const user = await factory.insert('user', { name: 'Test' });
131
+ * expect(user).toBeDefined();
132
+ * });
133
+ * ```
134
+ */
135
+ wrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(options: TransactionWrapperOptions<TConn, Transaction, Extended>): TestAPI<DatabaseFixtures<Transaction, object> & Extended>;
136
+ }
137
+ export type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;
138
+ /**
139
+ * Where a connection comes from: a construct, or a function returning one.
140
+ *
141
+ * A construct is preferred, because it builds the client through the same
142
+ * `connect()` production uses. A hand-rolled client missing `CamelCasePlugin`
143
+ * writes `createdAt` where the app writes `created_at`, and the test passes
144
+ * while production does not. The URL is read from `process.env`.
145
+ */
146
+ export type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn> | ConnectableConstruct<Conn>;
147
+ /**
148
+ * Options for wrapping Vitest tests with database transaction isolation.
149
+ */
150
+ /**
151
+ * Anything that owns a service handing back a connection — a database
152
+ * construct, structurally, so testkit depends on no construct package.
153
+ */
154
+ export interface ConnectableConstruct<TConn> {
155
+ readonly service: Service<string, TConn>;
156
+ }
157
+ export interface TransactionWrapperOptions<TConn, Transaction, Extended extends Record<string, unknown> = {}> {
158
+ /** Where the connection comes from — a construct, or a function returning one */
159
+ connection: DatabaseConnection<TConn>;
160
+ /** Optional setup function to run within the transaction before each test */
161
+ setup?: (trx: Transaction) => Promise<void>;
162
+ /** Transaction isolation level (defaults to REPEATABLE_READ) */
163
+ isolationLevel?: IsolationLevel;
164
+ /** Additional fixtures that depend on the transaction */
165
+ fixtures?: FixtureCreators<Transaction, Extended>;
166
+ }
167
+ /**
168
+ * Type for fixture creator functions that depend on the transaction.
169
+ * Each function receives the transaction and returns the fixture value.
170
+ */
171
+ export type FixtureCreators<Transaction, Extended extends Record<string, unknown>> = { [K in keyof Extended]: (trx: Transaction) => Extended[K] | Promise<Extended[K]>; };
172
+ /**
173
+ * The test API returned by extendWithFixtures.
174
+ * Provides access to both the transaction (trx) and all extended fixtures.
175
+ *
176
+ * @template Transaction - The transaction type
177
+ * @template Extended - The type of additional fixtures provided
178
+ * @template BaseTest - The base wrapped test type
179
+ */
180
+ export type TestWithExtendedFixtures<Transaction, Extended extends Record<string, unknown>, BaseTest extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>> = BaseTest & {
181
+ <C extends object>(name: string, fn: (context: DatabaseFixtures<Transaction> & Extended & C) => Promise<void>): void;
182
+ <C extends object>(name: string, options: object, fn: (context: DatabaseFixtures<Transaction> & Extended & C) => Promise<void>): void;
183
+ };
184
+ /**
185
+ * Extends a wrapped test API with additional fixtures that depend on the transaction.
186
+ * This allows composing test context with factories, repositories, or other helpers.
187
+ *
188
+ * @template Transaction - The transaction type
189
+ * @template Extended - The type of additional context to provide
190
+ * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction
191
+ * @param fixtures - Object mapping fixture names to creator functions
192
+ * @returns An extended test API with both trx and the additional fixtures
193
+ *
194
+ * @example
195
+ * ```typescript
196
+ * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
197
+ *
198
+ * // Create base wrapped test
199
+ * const baseTest = wrapVitestKyselyTransaction(test, {
200
+ * connection: db,
201
+ * setup: createTestTables,
202
+ * });
203
+ *
204
+ * // Extend with fixtures
205
+ * const it = extendWithFixtures(baseTest, {
206
+ * factory: (trx) => new KyselyFactory(builders, seeds, trx),
207
+ * userRepo: (trx) => new UserRepository(trx),
208
+ * });
209
+ *
210
+ * // Use in tests - trx and all fixtures are available
211
+ * it('should create user with factory', async ({ trx, factory, userRepo }) => {
212
+ * const user = await factory.insert('user', { name: 'Test' });
213
+ * expect(user).toBeDefined();
214
+ * });
215
+ * ```
216
+ */
217
+ export 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>;
218
+ //#endregion
219
+ //# sourceMappingURL=VitestTransactionIsolator.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VitestTransactionIsolator.d.cts","names":[],"sources":["../src/VitestTransactionIsolator.ts"],"mappings":";;;;;;;;;;iBAWiB,iBAAiB,aAAa;;;;;EAK9C,KAAK;;;;;YAMM,yBACX,aACA,qBACG,iBAAiB,eAAe;;;;;;;;;;;;;;YAexB,gBAAgB,aAAa,aACxC,KAAK,gBACD,WAAW,QAAQ;;;;;;;oBAQZ;;;;;EAKX;;;;;EAKA;;;;;EAKA;;;;;EAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8BA8BqB,kCAAkC,OAAO;mBAsBjC;;;;;;;;;;WAZpB,SACR,MAAM,OACN,gBAAgB,gBAChB,KAAK,KAAK,gBAAgB,gBACxB;WAEM,QAAQ,MAAM,QAAQ;;;;;;EAM/B,YAA6B,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;EA2BlC,0BAA0B,iBAAiB,8BAC1C,SAAS,0BAA0B,OAAO,aAAa,YAAS,QAAA,iBAAA,uBAAA;;YA8FtD,qBAAqB,cAAc,OAAO,QAAQ;;;;;;;;;YAUlD,mBAAmB,QAC5B,qBAAqB,QACrB,qBAAqB;;;;;;;;iBAgBP,qBAAqB;WAC5B,SAAS,gBAAgB;;iBAGlB,0BAChB,OACA,aACA,iBAAiB;;EAGjB,YAAY,mBAAmB;;EAE/B,SAAS,KAAK,gBAAgB;;EAE9B,iBAAiB;;EAEjB,WAAW,gBAAgB,aAAa;;;;;;YAO7B,gBACX,aACA,iBAAiB,8BAEhB,WAAW,YACX,KAAK,gBACD,SAAS,KAAK,QAAQ,SAAS;;;;;;;;;YAWzB,yBACX,aACA,iBAAiB,yBACjB,iBAAiB,WAAW,qBAAqB,WAChD,sBAEE;GACF,kBACA,cACA,KACC,SAAS,iBAAiB,eAAe,WAAW,MAChD;GAEL,kBACA,cACA,iBACA,KACC,SAAS,iBAAiB,eAAe,WAAW,MAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAqCS,mBACf,aACA,iBAAiB,yBAEjB,UAAU,WAAW,0BAErB,aAAa,GACb,UAAU,gBAAgB,aAAa,YACrC,yBAAyB,aAAa,UAAU"}
@@ -1,2 +1,219 @@
1
- import { ConnectableConstruct, DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures } from "./VitestTransactionIsolator-BLaw80cx.mjs";
2
- export { ConnectableConstruct, DatabaseConnection, DatabaseConnectionFn, DatabaseFixtures, ExtendContextFn, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions, VitestPostgresTransactionIsolator, extendWithFixtures };
1
+ import { Service } from "@geekmidas/services";
2
+ import { TestAPI } from "vitest";
3
+ //#region src/VitestTransactionIsolator.d.ts
4
+ /**
5
+ * Type definition for test fixtures that provide transaction access.
6
+ * Used with Vitest's test.extend() API to inject transactions into tests.
7
+ *
8
+ * @template Transaction - The transaction type specific to the database driver
9
+ * @template Extended - Additional context properties provided by the extend function
10
+ */
11
+ export interface DatabaseFixtures<Transaction, _Extended = object> {
12
+ /**
13
+ * The database transaction available to the test.
14
+ * All database operations should use this transaction to ensure proper rollback.
15
+ */
16
+ trx: Transaction;
17
+ }
18
+ /**
19
+ * Combined fixtures type that merges the base transaction fixture with extended context.
20
+ */
21
+ export type ExtendedDatabaseFixtures<Transaction, Extended = object> = DatabaseFixtures<Transaction> & Extended;
22
+ /**
23
+ * Function type for extending test context with additional properties.
24
+ * Receives the transaction and returns additional context to be merged with { trx }.
25
+ *
26
+ * @template Transaction - The transaction type
27
+ * @template Extended - The type of additional context to provide
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const extendContext: ExtendContextFn<Transaction<DB>, { factory: KyselyFactory }> =
32
+ * (trx) => ({ factory: new KyselyFactory(builders, seeds, trx) });
33
+ * ```
34
+ */
35
+ export type ExtendContextFn<Transaction, Extended> = (trx: Transaction) => Extended | Promise<Extended>;
36
+ /**
37
+ * PostgreSQL transaction isolation levels.
38
+ * Controls the visibility of concurrent transactions.
39
+ *
40
+ * @see https://www.postgresql.org/docs/current/transaction-iso.html
41
+ */
42
+ export declare enum IsolationLevel {
43
+ /**
44
+ * Lowest isolation level. Allows dirty reads.
45
+ * Not recommended for testing.
46
+ */
47
+ READ_UNCOMMITTED = "READ UNCOMMITTED",
48
+ /**
49
+ * Default PostgreSQL isolation level.
50
+ * Prevents dirty reads but allows non-repeatable reads.
51
+ */
52
+ READ_COMMITTED = "READ COMMITTED",
53
+ /**
54
+ * Prevents dirty reads and non-repeatable reads.
55
+ * Recommended for most test scenarios.
56
+ */
57
+ REPEATABLE_READ = "REPEATABLE READ",
58
+ /**
59
+ * Highest isolation level. Prevents all phenomena.
60
+ * May cause performance overhead in tests.
61
+ */
62
+ SERIALIZABLE = "SERIALIZABLE"
63
+ }
64
+ /**
65
+ * Abstract base class for implementing database transaction isolation in Vitest tests.
66
+ * Provides automatic transaction rollback after each test to maintain test isolation.
67
+ * Subclasses must implement the transact() method for their specific database driver.
68
+ *
69
+ * @template TConn - The database connection type
70
+ * @template Transaction - The transaction type
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * // Implement for your database driver
75
+ * class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
76
+ * async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
77
+ * await conn.transaction(level, fn);
78
+ * }
79
+ * }
80
+ *
81
+ * // Use in tests
82
+ * const isolator = new MyDatabaseIsolator(test);
83
+ * const isolatedTest = isolator.wrapVitestWithTransaction(db);
84
+ *
85
+ * isolatedTest('should create user', async ({ trx }) => {
86
+ * await trx.insert('users', { name: 'Test' });
87
+ * // Data is automatically rolled back after test
88
+ * });
89
+ * ```
90
+ */
91
+ export declare abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
92
+ private readonly api;
93
+ /**
94
+ * Abstract method to create a transaction with the specified isolation level.
95
+ * Must be implemented by subclasses for specific database drivers.
96
+ *
97
+ * @param conn - The database connection
98
+ * @param isolationLevel - The transaction isolation level
99
+ * @param fn - The function to execute within the transaction
100
+ * @returns Promise that resolves when the transaction completes
101
+ */
102
+ abstract transact(conn: TConn, isolationLevel: IsolationLevel, fn: (trx: Transaction) => Promise<void>): Promise<void>;
103
+ abstract destroy(conn: TConn): Promise<void>;
104
+ /**
105
+ * Creates a new VitestPostgresTransactionIsolator instance.
106
+ *
107
+ * @param api - The Vitest test API (usually the `test` export from vitest)
108
+ */
109
+ constructor(api: TestAPI);
110
+ /**
111
+ * Creates a wrapped version of Vitest's test API that provides transaction isolation.
112
+ * Each test will run within a database transaction that is automatically rolled back.
113
+ *
114
+ * @param options - Configuration options for transaction wrapping
115
+ * @returns A wrapped test API with transaction support
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * const isolatedTest = isolator.wrapVitestWithTransaction({
120
+ * connection: db,
121
+ * setup: async (trx) => {
122
+ * await trx.insert('settings', { key: 'test', value: 'true' });
123
+ * },
124
+ * fixtures: {
125
+ * factory: (trx) => new Factory(trx),
126
+ * },
127
+ * });
128
+ *
129
+ * isolatedTest('test with transaction', async ({ trx, factory }) => {
130
+ * const user = await factory.insert('user', { name: 'Test' });
131
+ * expect(user).toBeDefined();
132
+ * });
133
+ * ```
134
+ */
135
+ wrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(options: TransactionWrapperOptions<TConn, Transaction, Extended>): TestAPI<DatabaseFixtures<Transaction, object> & Extended>;
136
+ }
137
+ export type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;
138
+ /**
139
+ * Where a connection comes from: a construct, or a function returning one.
140
+ *
141
+ * A construct is preferred, because it builds the client through the same
142
+ * `connect()` production uses. A hand-rolled client missing `CamelCasePlugin`
143
+ * writes `createdAt` where the app writes `created_at`, and the test passes
144
+ * while production does not. The URL is read from `process.env`.
145
+ */
146
+ export type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn> | ConnectableConstruct<Conn>;
147
+ /**
148
+ * Options for wrapping Vitest tests with database transaction isolation.
149
+ */
150
+ /**
151
+ * Anything that owns a service handing back a connection — a database
152
+ * construct, structurally, so testkit depends on no construct package.
153
+ */
154
+ export interface ConnectableConstruct<TConn> {
155
+ readonly service: Service<string, TConn>;
156
+ }
157
+ export interface TransactionWrapperOptions<TConn, Transaction, Extended extends Record<string, unknown> = {}> {
158
+ /** Where the connection comes from — a construct, or a function returning one */
159
+ connection: DatabaseConnection<TConn>;
160
+ /** Optional setup function to run within the transaction before each test */
161
+ setup?: (trx: Transaction) => Promise<void>;
162
+ /** Transaction isolation level (defaults to REPEATABLE_READ) */
163
+ isolationLevel?: IsolationLevel;
164
+ /** Additional fixtures that depend on the transaction */
165
+ fixtures?: FixtureCreators<Transaction, Extended>;
166
+ }
167
+ /**
168
+ * Type for fixture creator functions that depend on the transaction.
169
+ * Each function receives the transaction and returns the fixture value.
170
+ */
171
+ export type FixtureCreators<Transaction, Extended extends Record<string, unknown>> = { [K in keyof Extended]: (trx: Transaction) => Extended[K] | Promise<Extended[K]>; };
172
+ /**
173
+ * The test API returned by extendWithFixtures.
174
+ * Provides access to both the transaction (trx) and all extended fixtures.
175
+ *
176
+ * @template Transaction - The transaction type
177
+ * @template Extended - The type of additional fixtures provided
178
+ * @template BaseTest - The base wrapped test type
179
+ */
180
+ export type TestWithExtendedFixtures<Transaction, Extended extends Record<string, unknown>, BaseTest extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>> = BaseTest & {
181
+ <C extends object>(name: string, fn: (context: DatabaseFixtures<Transaction> & Extended & C) => Promise<void>): void;
182
+ <C extends object>(name: string, options: object, fn: (context: DatabaseFixtures<Transaction> & Extended & C) => Promise<void>): void;
183
+ };
184
+ /**
185
+ * Extends a wrapped test API with additional fixtures that depend on the transaction.
186
+ * This allows composing test context with factories, repositories, or other helpers.
187
+ *
188
+ * @template Transaction - The transaction type
189
+ * @template Extended - The type of additional context to provide
190
+ * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction
191
+ * @param fixtures - Object mapping fixture names to creator functions
192
+ * @returns An extended test API with both trx and the additional fixtures
193
+ *
194
+ * @example
195
+ * ```typescript
196
+ * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
197
+ *
198
+ * // Create base wrapped test
199
+ * const baseTest = wrapVitestKyselyTransaction(test, {
200
+ * connection: db,
201
+ * setup: createTestTables,
202
+ * });
203
+ *
204
+ * // Extend with fixtures
205
+ * const it = extendWithFixtures(baseTest, {
206
+ * factory: (trx) => new KyselyFactory(builders, seeds, trx),
207
+ * userRepo: (trx) => new UserRepository(trx),
208
+ * });
209
+ *
210
+ * // Use in tests - trx and all fixtures are available
211
+ * it('should create user with factory', async ({ trx, factory, userRepo }) => {
212
+ * const user = await factory.insert('user', { name: 'Test' });
213
+ * expect(user).toBeDefined();
214
+ * });
215
+ * ```
216
+ */
217
+ export 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>;
218
+ //#endregion
219
+ //# sourceMappingURL=VitestTransactionIsolator.d.mts.map