@geekmidas/testkit 0.0.16 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +302 -199
- package/dist/{KyselyFactory-DRQ83r0o.d.cts → KyselyFactory-BFygzOlO.d.cts} +21 -8
- package/dist/{KyselyFactory-Cx3sezwH.d.mts → KyselyFactory-BTdygZ-i.d.mts} +21 -8
- package/dist/{KyselyFactory-BcYkC0t2.mjs → KyselyFactory-CXY5gJk2.mjs} +25 -12
- package/dist/KyselyFactory-CXY5gJk2.mjs.map +1 -0
- package/dist/{KyselyFactory-Cf0o2YxO.cjs → KyselyFactory-DaaCykWP.cjs} +25 -12
- package/dist/KyselyFactory-DaaCykWP.cjs.map +1 -0
- package/dist/KyselyFactory.cjs +1 -1
- package/dist/KyselyFactory.d.cts +1 -1
- package/dist/KyselyFactory.d.mts +1 -1
- package/dist/KyselyFactory.mjs +1 -1
- package/dist/{ObjectionFactory-C-59Hjwj.d.mts → ObjectionFactory-BagGjikT.d.mts} +24 -11
- package/dist/{ObjectionFactory-C4X78k0B.d.cts → ObjectionFactory-CeSIN3kZ.d.cts} +24 -11
- package/dist/{ObjectionFactory-CDriunkS.cjs → ObjectionFactory-Eb04AOnv.cjs} +28 -15
- package/dist/ObjectionFactory-Eb04AOnv.cjs.map +1 -0
- package/dist/{ObjectionFactory-8hebmnai.mjs → ObjectionFactory-zf2fLKrL.mjs} +28 -15
- package/dist/ObjectionFactory-zf2fLKrL.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.d.cts +1 -1
- package/dist/ObjectionFactory.d.mts +1 -1
- package/dist/ObjectionFactory.mjs +1 -1
- package/dist/{VitestKyselyTransactionIsolator-COCVfvfr.d.mts → VitestKyselyTransactionIsolator-4HOeLQ0d.d.cts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-Cst3vFjb.cjs → VitestKyselyTransactionIsolator-DX_VPKS-.cjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-Cst3vFjb.cjs.map → VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map} +1 -1
- package/dist/{VitestKyselyTransactionIsolator-DYUYVEh9.d.cts → VitestKyselyTransactionIsolator-DnyZMaA-.d.mts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-BxjlD1YM.mjs → VitestKyselyTransactionIsolator-XDL3ngs_.mjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-BxjlD1YM.mjs.map → VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map} +1 -1
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/{VitestObjectionTransactionIsolator-b973r9O1.d.mts → VitestObjectionTransactionIsolator-COVDlpEo.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-DzeF4UAq.cjs → VitestObjectionTransactionIsolator-D_tlOtq8.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-DzeF4UAq.cjs.map → VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-BU-jXEhz.mjs → VitestObjectionTransactionIsolator-_EhJKu_O.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-BU-jXEhz.mjs.map → VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-CJ4ds5Qv.d.cts → VitestObjectionTransactionIsolator-lZUSz1w0.d.mts} +2 -2
- package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
- package/dist/{VitestTransactionIsolator-CskiiJbW.mjs → VitestTransactionIsolator-BIaMs4c2.mjs} +40 -2
- package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +1 -0
- package/dist/{VitestTransactionIsolator-BQ5FpLtC.cjs → VitestTransactionIsolator-BKIrj3Uy.cjs} +45 -1
- package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CsfJBxcb.d.mts → VitestTransactionIsolator-CyG_i_Nj.d.cts} +61 -3
- package/dist/{VitestTransactionIsolator-DdLNODZg.d.cts → VitestTransactionIsolator-DWDbnITQ.d.mts} +61 -3
- package/dist/VitestTransactionIsolator.cjs +3 -2
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +2 -2
- package/dist/better-auth.cjs +242 -0
- package/dist/better-auth.cjs.map +1 -0
- package/dist/better-auth.d.cts +17 -0
- package/dist/better-auth.d.mts +17 -0
- package/dist/better-auth.mjs +241 -0
- package/dist/better-auth.mjs.map +1 -0
- package/dist/{directory-B4oYx02C.d.mts → directory-BXavAeJZ.d.mts} +3 -3
- package/dist/{directory-BUcnztHI.d.cts → directory-DlkPEzL4.d.cts} +3 -3
- package/dist/kysely.cjs +58 -4
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +58 -5
- package/dist/kysely.d.mts +58 -5
- package/dist/kysely.mjs +57 -5
- package/dist/kysely.mjs.map +1 -1
- package/dist/objection.cjs +54 -4
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +54 -5
- package/dist/objection.d.mts +54 -5
- package/dist/objection.mjs +53 -5
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/directory.d.mts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/package.json +13 -3
- package/src/KyselyFactory.ts +29 -16
- package/src/ObjectionFactory.ts +34 -19
- package/src/VitestTransactionIsolator.ts +110 -2
- package/src/__tests__/KyselyFactory.spec.ts +10 -10
- package/src/__tests__/ObjectionFactory.spec.ts +9 -12
- package/src/__tests__/better-auth.spec.ts +21 -0
- package/src/__tests__/integration.spec.ts +171 -14
- package/src/better-auth.ts +325 -0
- package/src/kysely.ts +66 -0
- package/src/objection.ts +61 -0
- package/dist/KyselyFactory-BcYkC0t2.mjs.map +0 -1
- package/dist/KyselyFactory-Cf0o2YxO.cjs.map +0 -1
- package/dist/ObjectionFactory-8hebmnai.mjs.map +0 -1
- package/dist/ObjectionFactory-CDriunkS.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BQ5FpLtC.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-CskiiJbW.mjs.map +0 -1
package/dist/kysely.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kysely.mjs","names":["api: TestAPI","connection: DatabaseConnection<Kysely<Database>>","setup?: (trx: Transaction<Database>) => Promise<void>","level: IsolationLevel"],"sources":["../src/kysely.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport type { TestAPI } from 'vitest';\nimport { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';\nimport {\n type DatabaseConnection,\n IsolationLevel,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific exports for test utilities.\n * Provides factories, migrators, and transaction isolators for Kysely ORM.\n */\n\nexport { KyselyFactory } from './KyselyFactory';\nexport { PostgresKyselyMigrator } from './PostgresKyselyMigrator';\nexport { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @template Database - The database schema type\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param db - The Kysely database instance\n * @param setup - Optional setup function to run before each test in the transaction\n * @param level - Transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';\n * import { db } from './database';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestKyselyTransaction(test, db);\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await trx\n * .insertInto('users')\n * .values({ name: 'Test User', email: 'test@example.com' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * // User is automatically rolled back after test\n * });\n *\n * // With setup function for common test data\n * const testWithSetup = wrapVitestKyselyTransaction(\n * test,\n * db,\n * async (trx) => {\n * // Create common test data\n * await trx.insertInto('settings')\n * .values({ key: 'test_mode', value: 'true' })\n * .execute();\n * }\n * );\n *\n * testWithSetup('should have test settings', async ({ trx }) => {\n * const setting = await trx\n * .selectFrom('settings')\n * .where('key', '=', 'test_mode')\n * .selectAll()\n * .executeTakeFirst();\n *\n * expect(setting?.value).toBe('true');\n * });\n * ```\n */\nexport function wrapVitestKyselyTransaction<Database>(\n api: TestAPI,\n connection: DatabaseConnection<Kysely<Database>>,\n setup?: (trx: Transaction<Database>) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n) {\n const wrapper = new VitestKyselyTransactionIsolator<Database>(api);\n\n return wrapper.wrapVitestWithTransaction(connection, setup, level);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"kysely.mjs","names":["api: TestAPI","connection: DatabaseConnection<Kysely<Database>>","setup?: (trx: Transaction<Database>) => Promise<void>","level: IsolationLevel","extendWithFixtures","wrappedTest: T","fixtures: FixtureCreators<Transaction<Database>, Extended>"],"sources":["../src/kysely.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport type { TestAPI } from 'vitest';\nimport { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';\nimport {\n type DatabaseConnection,\n type FixtureCreators,\n IsolationLevel,\n extendWithFixtures as baseExtendWithFixtures,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific exports for test utilities.\n * Provides factories, migrators, and transaction isolators for Kysely ORM.\n */\n\nexport { KyselyFactory } from './KyselyFactory';\nexport { PostgresKyselyMigrator } from './PostgresKyselyMigrator';\nexport { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\nexport type { FixtureCreators } from './VitestTransactionIsolator';\n\n// Re-export faker and FakerFactory for type portability in declaration files\nexport { faker, type FakerFactory } from './faker';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @template Database - The database schema type\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param db - The Kysely database instance\n * @param setup - Optional setup function to run before each test in the transaction\n * @param level - Transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';\n * import { db } from './database';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestKyselyTransaction(test, db);\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await trx\n * .insertInto('users')\n * .values({ name: 'Test User', email: 'test@example.com' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * // User is automatically rolled back after test\n * });\n *\n * // With setup function for common test data\n * const testWithSetup = wrapVitestKyselyTransaction(\n * test,\n * db,\n * async (trx) => {\n * // Create common test data\n * await trx.insertInto('settings')\n * .values({ key: 'test_mode', value: 'true' })\n * .execute();\n * }\n * );\n *\n * testWithSetup('should have test settings', async ({ trx }) => {\n * const setting = await trx\n * .selectFrom('settings')\n * .where('key', '=', 'test_mode')\n * .selectAll()\n * .executeTakeFirst();\n *\n * expect(setting?.value).toBe('true');\n * });\n * ```\n */\nexport function wrapVitestKyselyTransaction<Database>(\n api: TestAPI,\n connection: DatabaseConnection<Kysely<Database>>,\n setup?: (trx: Transaction<Database>) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n) {\n const wrapper = new VitestKyselyTransactionIsolator<Database>(api);\n\n return wrapper.wrapVitestWithTransaction(connection, setup, level);\n}\n\n/**\n * Extends a Kysely transaction-wrapped test with additional fixtures.\n * Each fixture receives the transaction and can create dependencies like factories or repositories.\n *\n * @template Database - The database schema type\n * @template Extended - The type of additional fixtures to provide\n * @param wrappedTest - The base wrapped test from wrapVitestKyselyTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestKyselyTransaction, extendWithFixtures, KyselyFactory } from '@geekmidas/testkit/kysely';\n *\n * // Define your builders\n * const builders = {\n * user: KyselyFactory.createBuilder<DB, 'users'>('users', ({ faker }) => ({\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * })),\n * };\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestKyselyTransaction<DB>(test, db, createTestTables);\n *\n * // Extend with fixtures - each fixture receives the transaction\n * const it = extendWithFixtures<DB, { factory: KyselyFactory<DB, typeof builders, {}> }>(\n * baseTest,\n * {\n * factory: (trx) => new KyselyFactory(builders, {}, trx),\n * }\n * );\n *\n * // Use in tests - both trx and factory are available\n * it('should create user with factory', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test User' });\n * expect(user.id).toBeDefined();\n *\n * // Verify in database\n * const found = await trx\n * .selectFrom('users')\n * .where('id', '=', user.id)\n * .selectAll()\n * .executeTakeFirst();\n * expect(found?.name).toBe('Test User');\n * });\n * ```\n */\nexport function extendWithFixtures<\n Database,\n Extended extends Record<string, unknown>,\n T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>,\n>(wrappedTest: T, fixtures: FixtureCreators<Transaction<Database>, Extended>) {\n return baseExtendWithFixtures<Transaction<Database>, Extended, T>(\n wrappedTest,\n fixtures,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgFA,SAAgB,4BACdA,KACAC,YACAC,OACAC,QAAwB,eAAe,iBACvC;CACA,MAAM,UAAU,IAAI,gCAA0C;AAE9D,QAAO,QAAQ,0BAA0B,YAAY,OAAO,MAAM;AACnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDD,SAAgBC,qBAIdC,aAAgBC,UAA4D;AAC5E,QAAO,mBACL,aACA,SACD;AACF"}
|
package/dist/objection.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
|
-
require('./faker-B14IEMIN.cjs');
|
|
3
|
-
const require_ObjectionFactory = require('./ObjectionFactory-
|
|
2
|
+
const require_faker = require('./faker-B14IEMIN.cjs');
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-Eb04AOnv.cjs');
|
|
4
4
|
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
5
5
|
const require_PostgresObjectionMigrator = require('./PostgresObjectionMigrator-BG6ymgnt.cjs');
|
|
6
|
-
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-
|
|
7
|
-
const require_VitestObjectionTransactionIsolator = require('./VitestObjectionTransactionIsolator-
|
|
6
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-BKIrj3Uy.cjs');
|
|
7
|
+
const require_VitestObjectionTransactionIsolator = require('./VitestObjectionTransactionIsolator-D_tlOtq8.cjs');
|
|
8
8
|
|
|
9
9
|
//#region src/objection.ts
|
|
10
10
|
/**
|
|
@@ -82,11 +82,61 @@ function wrapVitestObjectionTransaction(api, conn, setup, level = require_Vitest
|
|
|
82
82
|
const wrapper = new require_VitestObjectionTransactionIsolator.VitestObjectionTransactionIsolator(api);
|
|
83
83
|
return wrapper.wrapVitestWithTransaction(conn, setup, level);
|
|
84
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
87
|
+
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
88
|
+
*
|
|
89
|
+
* @template Extended - The type of additional fixtures to provide
|
|
90
|
+
* @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction
|
|
91
|
+
* @param fixtures - Object mapping fixture names to creator functions
|
|
92
|
+
* @returns An extended test API with both trx and the additional fixtures
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import { test } from 'vitest';
|
|
97
|
+
* import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';
|
|
98
|
+
* import { User } from './models';
|
|
99
|
+
*
|
|
100
|
+
* // Define your builders
|
|
101
|
+
* const builders = {
|
|
102
|
+
* user: ObjectionFactory.createBuilder(User, ({ faker }) => ({
|
|
103
|
+
* name: faker.person.fullName(),
|
|
104
|
+
* email: faker.internet.email(),
|
|
105
|
+
* })),
|
|
106
|
+
* };
|
|
107
|
+
*
|
|
108
|
+
* // Create base wrapped test
|
|
109
|
+
* const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
|
|
110
|
+
*
|
|
111
|
+
* // Extend with fixtures - each fixture receives the transaction
|
|
112
|
+
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
113
|
+
* baseTest,
|
|
114
|
+
* {
|
|
115
|
+
* factory: (trx) => new ObjectionFactory(builders, {}, trx),
|
|
116
|
+
* }
|
|
117
|
+
* );
|
|
118
|
+
*
|
|
119
|
+
* // Use in tests - both trx and factory are available
|
|
120
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
121
|
+
* const user = await factory.insert('user', { name: 'Test User' });
|
|
122
|
+
* expect(user.id).toBeDefined();
|
|
123
|
+
*
|
|
124
|
+
* // Verify in database
|
|
125
|
+
* const found = await User.query(trx).findById(user.id);
|
|
126
|
+
* expect(found?.name).toBe('Test User');
|
|
127
|
+
* });
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
function extendWithFixtures$1(wrappedTest, fixtures) {
|
|
131
|
+
return require_VitestTransactionIsolator.extendWithFixtures(wrappedTest, fixtures);
|
|
132
|
+
}
|
|
85
133
|
|
|
86
134
|
//#endregion
|
|
87
135
|
exports.IsolationLevel = require_VitestTransactionIsolator.IsolationLevel;
|
|
88
136
|
exports.ObjectionFactory = require_ObjectionFactory.ObjectionFactory;
|
|
89
137
|
exports.PostgresObjectionMigrator = require_PostgresObjectionMigrator.PostgresObjectionMigrator;
|
|
90
138
|
exports.VitestObjectionTransactionIsolator = require_VitestObjectionTransactionIsolator.VitestObjectionTransactionIsolator;
|
|
139
|
+
exports.extendWithFixtures = extendWithFixtures$1;
|
|
140
|
+
exports.faker = require_faker.faker;
|
|
91
141
|
exports.wrapVitestObjectionTransaction = wrapVitestObjectionTransaction;
|
|
92
142
|
//# sourceMappingURL=objection.cjs.map
|
package/dist/objection.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objection.cjs","names":["api: TestAPI","conn: DatabaseConnection<Knex>","setup?: (trx: Knex.Transaction) => Promise<void>","level: IsolationLevel","IsolationLevel","VitestObjectionTransactionIsolator"],"sources":["../src/objection.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { TestAPI } from 'vitest';\nimport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nimport {\n type DatabaseConnection,\n IsolationLevel,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific exports for test utilities.\n * Provides factory implementation for creating test data with Objection.js ORM\n * and transaction isolation for test suites.\n */\n\nexport { ObjectionFactory } from './ObjectionFactory';\nexport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\nexport { PostgresObjectionMigrator } from './PostgresObjectionMigrator';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param conn - The Knex database connection instance\n * @param setup - Optional setup function to run before each test in the transaction\n * @param level - Transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';\n * import { knex } from './database';\n * import { User, Post } from './models';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await User.query(trx)\n * .insert({ name: 'Test User', email: 'test@example.com' });\n *\n * expect(user).toBeDefined();\n * // User is automatically rolled back after test\n * });\n *\n * // With setup function for common test data\n * const testWithSetup = wrapVitestObjectionTransaction(\n * test,\n * knex,\n * async (trx) => {\n * // Create common test data\n * await knex('settings')\n * .transacting(trx)\n * .insert({ key: 'test_mode', value: 'true' });\n * }\n * );\n *\n * testWithSetup('should have test settings', async ({ trx }) => {\n * const setting = await knex('settings')\n * .transacting(trx)\n * .where('key', 'test_mode')\n * .first();\n *\n * expect(setting?.value).toBe('true');\n * });\n *\n * // Example with factory and transaction\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n * const factory = new ObjectionFactory(builders, seeds, knex);\n *\n * isolatedTest('creates related data', async ({ trx }) => {\n * // Factory can use the transaction\n * const user = await User.query(trx).insert({ name: 'Author' });\n * const posts = await Post.query(trx).insert([\n * { title: 'Post 1', userId: user.id },\n * { title: 'Post 2', userId: user.id }\n * ]);\n *\n * const userWithPosts = await User.query(trx)\n * .findById(user.id)\n * .withGraphFetched('posts');\n *\n * expect(userWithPosts.posts).toHaveLength(2);\n * });\n * ```\n */\nexport function wrapVitestObjectionTransaction(\n api: TestAPI,\n conn: DatabaseConnection<Knex>,\n setup?: (trx: Knex.Transaction) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n) {\n const wrapper = new VitestObjectionTransactionIsolator(api);\n\n return wrapper.wrapVitestWithTransaction(conn, setup, level);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"objection.cjs","names":["api: TestAPI","conn: DatabaseConnection<Knex>","setup?: (trx: Knex.Transaction) => Promise<void>","level: IsolationLevel","IsolationLevel","VitestObjectionTransactionIsolator","extendWithFixtures","wrappedTest: T","fixtures: FixtureCreators<Knex.Transaction, Extended>"],"sources":["../src/objection.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { TestAPI } from 'vitest';\nimport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nimport {\n type DatabaseConnection,\n type FixtureCreators,\n IsolationLevel,\n extendWithFixtures as baseExtendWithFixtures,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific exports for test utilities.\n * Provides factory implementation for creating test data with Objection.js ORM\n * and transaction isolation for test suites.\n */\n\nexport { ObjectionFactory } from './ObjectionFactory';\nexport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\nexport { PostgresObjectionMigrator } from './PostgresObjectionMigrator';\nexport type { FixtureCreators } from './VitestTransactionIsolator';\n\n// Re-export faker and FakerFactory for type portability in declaration files\nexport { faker, type FakerFactory } from './faker';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param conn - The Knex database connection instance\n * @param setup - Optional setup function to run before each test in the transaction\n * @param level - Transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';\n * import { knex } from './database';\n * import { User, Post } from './models';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await User.query(trx)\n * .insert({ name: 'Test User', email: 'test@example.com' });\n *\n * expect(user).toBeDefined();\n * // User is automatically rolled back after test\n * });\n *\n * // With setup function for common test data\n * const testWithSetup = wrapVitestObjectionTransaction(\n * test,\n * knex,\n * async (trx) => {\n * // Create common test data\n * await knex('settings')\n * .transacting(trx)\n * .insert({ key: 'test_mode', value: 'true' });\n * }\n * );\n *\n * testWithSetup('should have test settings', async ({ trx }) => {\n * const setting = await knex('settings')\n * .transacting(trx)\n * .where('key', 'test_mode')\n * .first();\n *\n * expect(setting?.value).toBe('true');\n * });\n *\n * // Example with factory and transaction\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n * const factory = new ObjectionFactory(builders, seeds, knex);\n *\n * isolatedTest('creates related data', async ({ trx }) => {\n * // Factory can use the transaction\n * const user = await User.query(trx).insert({ name: 'Author' });\n * const posts = await Post.query(trx).insert([\n * { title: 'Post 1', userId: user.id },\n * { title: 'Post 2', userId: user.id }\n * ]);\n *\n * const userWithPosts = await User.query(trx)\n * .findById(user.id)\n * .withGraphFetched('posts');\n *\n * expect(userWithPosts.posts).toHaveLength(2);\n * });\n * ```\n */\nexport function wrapVitestObjectionTransaction(\n api: TestAPI,\n conn: DatabaseConnection<Knex>,\n setup?: (trx: Knex.Transaction) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n) {\n const wrapper = new VitestObjectionTransactionIsolator(api);\n\n return wrapper.wrapVitestWithTransaction(conn, setup, level);\n}\n\n/**\n * Extends an Objection.js transaction-wrapped test with additional fixtures.\n * Each fixture receives the transaction and can create dependencies like factories or repositories.\n *\n * @template Extended - The type of additional fixtures to provide\n * @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';\n * import { User } from './models';\n *\n * // Define your builders\n * const builders = {\n * user: ObjectionFactory.createBuilder(User, ({ faker }) => ({\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * })),\n * };\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);\n *\n * // Extend with fixtures - each fixture receives the transaction\n * const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(\n * baseTest,\n * {\n * factory: (trx) => new ObjectionFactory(builders, {}, trx),\n * }\n * );\n *\n * // Use in tests - both trx and factory are available\n * it('should create user with factory', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test User' });\n * expect(user.id).toBeDefined();\n *\n * // Verify in database\n * const found = await User.query(trx).findById(user.id);\n * expect(found?.name).toBe('Test User');\n * });\n * ```\n */\nexport function extendWithFixtures<\n Extended extends Record<string, unknown>,\n T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>,\n>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>) {\n return baseExtendWithFixtures<Knex.Transaction, Extended, T>(\n wrappedTest,\n fixtures,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgGA,SAAgB,+BACdA,KACAC,MACAC,OACAC,QAAwBC,iDAAe,iBACvC;CACA,MAAM,UAAU,IAAIC,8EAAmC;AAEvD,QAAO,QAAQ,0BAA0B,MAAM,OAAO,MAAM;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CD,SAAgBC,qBAGdC,aAAgBC,UAAuD;AACvE,QAAO,qDACL,aACA,SACD;AACF"}
|
package/dist/objection.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import "./faker-Cg76aFNO.cjs";
|
|
1
|
+
import { FakerFactory, faker } from "./faker-Cg76aFNO.cjs";
|
|
2
2
|
import "./Factory-BZ8uMoXl.cjs";
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-CeSIN3kZ.cjs";
|
|
4
4
|
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-CZHHcCOv.cjs";
|
|
6
|
-
import { DatabaseConnection, IsolationLevel } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-
|
|
6
|
+
import { DatabaseConnection, DatabaseFixtures, FixtureCreators, IsolationLevel } from "./VitestTransactionIsolator-CyG_i_Nj.cjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-COVDlpEo.cjs";
|
|
8
8
|
import { Knex } from "knex";
|
|
9
9
|
import { TestAPI } from "vitest";
|
|
10
10
|
|
|
@@ -84,6 +84,55 @@ import { TestAPI } from "vitest";
|
|
|
84
84
|
declare function wrapVitestObjectionTransaction(api: TestAPI, conn: DatabaseConnection<Knex>, setup?: (trx: Knex.Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
85
85
|
trx: Knex.Transaction<any, any[]>;
|
|
86
86
|
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
89
|
+
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
90
|
+
*
|
|
91
|
+
* @template Extended - The type of additional fixtures to provide
|
|
92
|
+
* @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction
|
|
93
|
+
* @param fixtures - Object mapping fixture names to creator functions
|
|
94
|
+
* @returns An extended test API with both trx and the additional fixtures
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { test } from 'vitest';
|
|
99
|
+
* import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';
|
|
100
|
+
* import { User } from './models';
|
|
101
|
+
*
|
|
102
|
+
* // Define your builders
|
|
103
|
+
* const builders = {
|
|
104
|
+
* user: ObjectionFactory.createBuilder(User, ({ faker }) => ({
|
|
105
|
+
* name: faker.person.fullName(),
|
|
106
|
+
* email: faker.internet.email(),
|
|
107
|
+
* })),
|
|
108
|
+
* };
|
|
109
|
+
*
|
|
110
|
+
* // Create base wrapped test
|
|
111
|
+
* const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
|
|
112
|
+
*
|
|
113
|
+
* // Extend with fixtures - each fixture receives the transaction
|
|
114
|
+
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
115
|
+
* baseTest,
|
|
116
|
+
* {
|
|
117
|
+
* factory: (trx) => new ObjectionFactory(builders, {}, trx),
|
|
118
|
+
* }
|
|
119
|
+
* );
|
|
120
|
+
*
|
|
121
|
+
* // Use in tests - both trx and factory are available
|
|
122
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
123
|
+
* const user = await factory.insert('user', { name: 'Test User' });
|
|
124
|
+
* expect(user.id).toBeDefined();
|
|
125
|
+
*
|
|
126
|
+
* // Verify in database
|
|
127
|
+
* const found = await User.query(trx).findById(user.id);
|
|
128
|
+
* expect(found?.name).toBe('Test User');
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function extendWithFixtures<Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>): T & {
|
|
133
|
+
<C extends object>(name: string, fn: (context: DatabaseFixtures<Knex.Transaction<any, any[]>, object> & Extended & C) => Promise<void>): void;
|
|
134
|
+
<C extends object>(name: string, options: object, fn: (context: DatabaseFixtures<Knex.Transaction<any, any[]>, object> & Extended & C) => Promise<void>): void;
|
|
135
|
+
};
|
|
87
136
|
//#endregion
|
|
88
|
-
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
|
137
|
+
export { FakerFactory, FixtureCreators, IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
89
138
|
//# sourceMappingURL=objection.d.cts.map
|
package/dist/objection.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import "./faker-DHh7xs4u.mjs";
|
|
1
|
+
import { FakerFactory, faker } from "./faker-DHh7xs4u.mjs";
|
|
2
2
|
import "./Factory-Cmr3s3-s.mjs";
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-BagGjikT.mjs";
|
|
4
4
|
import "./PostgresMigrator-DQaRxoaY.mjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-D_hCcrQu.mjs";
|
|
6
|
-
import { DatabaseConnection, IsolationLevel } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-
|
|
6
|
+
import { DatabaseConnection, DatabaseFixtures, FixtureCreators, IsolationLevel } from "./VitestTransactionIsolator-DWDbnITQ.mjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-lZUSz1w0.mjs";
|
|
8
8
|
import { TestAPI } from "vitest";
|
|
9
9
|
import { Knex } from "knex";
|
|
10
10
|
|
|
@@ -84,6 +84,55 @@ import { Knex } from "knex";
|
|
|
84
84
|
declare function wrapVitestObjectionTransaction(api: TestAPI, conn: DatabaseConnection<Knex>, setup?: (trx: Knex.Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
85
85
|
trx: Knex.Transaction<any, any[]>;
|
|
86
86
|
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
89
|
+
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
90
|
+
*
|
|
91
|
+
* @template Extended - The type of additional fixtures to provide
|
|
92
|
+
* @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction
|
|
93
|
+
* @param fixtures - Object mapping fixture names to creator functions
|
|
94
|
+
* @returns An extended test API with both trx and the additional fixtures
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { test } from 'vitest';
|
|
99
|
+
* import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';
|
|
100
|
+
* import { User } from './models';
|
|
101
|
+
*
|
|
102
|
+
* // Define your builders
|
|
103
|
+
* const builders = {
|
|
104
|
+
* user: ObjectionFactory.createBuilder(User, ({ faker }) => ({
|
|
105
|
+
* name: faker.person.fullName(),
|
|
106
|
+
* email: faker.internet.email(),
|
|
107
|
+
* })),
|
|
108
|
+
* };
|
|
109
|
+
*
|
|
110
|
+
* // Create base wrapped test
|
|
111
|
+
* const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
|
|
112
|
+
*
|
|
113
|
+
* // Extend with fixtures - each fixture receives the transaction
|
|
114
|
+
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
115
|
+
* baseTest,
|
|
116
|
+
* {
|
|
117
|
+
* factory: (trx) => new ObjectionFactory(builders, {}, trx),
|
|
118
|
+
* }
|
|
119
|
+
* );
|
|
120
|
+
*
|
|
121
|
+
* // Use in tests - both trx and factory are available
|
|
122
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
123
|
+
* const user = await factory.insert('user', { name: 'Test User' });
|
|
124
|
+
* expect(user.id).toBeDefined();
|
|
125
|
+
*
|
|
126
|
+
* // Verify in database
|
|
127
|
+
* const found = await User.query(trx).findById(user.id);
|
|
128
|
+
* expect(found?.name).toBe('Test User');
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function extendWithFixtures<Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>): T & {
|
|
133
|
+
<C extends object>(name: string, fn: (context: DatabaseFixtures<Knex.Transaction<any, any[]>, object> & Extended & C) => Promise<void>): void;
|
|
134
|
+
<C extends object>(name: string, options: object, fn: (context: DatabaseFixtures<Knex.Transaction<any, any[]>, object> & Extended & C) => Promise<void>): void;
|
|
135
|
+
};
|
|
87
136
|
//#endregion
|
|
88
|
-
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
|
137
|
+
export { FakerFactory, FixtureCreators, IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
89
138
|
//# sourceMappingURL=objection.d.mts.map
|
package/dist/objection.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import "./Factory-z2m01hMj.mjs";
|
|
2
|
-
import "./faker-BGKYFoCT.mjs";
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
2
|
+
import { faker } from "./faker-BGKYFoCT.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-zf2fLKrL.mjs";
|
|
4
4
|
import "./PostgresMigrator-DbuJGAVy.mjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-DPj2pOpX.mjs";
|
|
6
|
-
import { IsolationLevel } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-
|
|
6
|
+
import { IsolationLevel, extendWithFixtures } from "./VitestTransactionIsolator-BIaMs4c2.mjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-_EhJKu_O.mjs";
|
|
8
8
|
|
|
9
9
|
//#region src/objection.ts
|
|
10
10
|
/**
|
|
@@ -82,7 +82,55 @@ function wrapVitestObjectionTransaction(api, conn, setup, level = IsolationLevel
|
|
|
82
82
|
const wrapper = new VitestObjectionTransactionIsolator(api);
|
|
83
83
|
return wrapper.wrapVitestWithTransaction(conn, setup, level);
|
|
84
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
87
|
+
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
88
|
+
*
|
|
89
|
+
* @template Extended - The type of additional fixtures to provide
|
|
90
|
+
* @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction
|
|
91
|
+
* @param fixtures - Object mapping fixture names to creator functions
|
|
92
|
+
* @returns An extended test API with both trx and the additional fixtures
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import { test } from 'vitest';
|
|
97
|
+
* import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';
|
|
98
|
+
* import { User } from './models';
|
|
99
|
+
*
|
|
100
|
+
* // Define your builders
|
|
101
|
+
* const builders = {
|
|
102
|
+
* user: ObjectionFactory.createBuilder(User, ({ faker }) => ({
|
|
103
|
+
* name: faker.person.fullName(),
|
|
104
|
+
* email: faker.internet.email(),
|
|
105
|
+
* })),
|
|
106
|
+
* };
|
|
107
|
+
*
|
|
108
|
+
* // Create base wrapped test
|
|
109
|
+
* const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
|
|
110
|
+
*
|
|
111
|
+
* // Extend with fixtures - each fixture receives the transaction
|
|
112
|
+
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
113
|
+
* baseTest,
|
|
114
|
+
* {
|
|
115
|
+
* factory: (trx) => new ObjectionFactory(builders, {}, trx),
|
|
116
|
+
* }
|
|
117
|
+
* );
|
|
118
|
+
*
|
|
119
|
+
* // Use in tests - both trx and factory are available
|
|
120
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
121
|
+
* const user = await factory.insert('user', { name: 'Test User' });
|
|
122
|
+
* expect(user.id).toBeDefined();
|
|
123
|
+
*
|
|
124
|
+
* // Verify in database
|
|
125
|
+
* const found = await User.query(trx).findById(user.id);
|
|
126
|
+
* expect(found?.name).toBe('Test User');
|
|
127
|
+
* });
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
function extendWithFixtures$1(wrappedTest, fixtures) {
|
|
131
|
+
return extendWithFixtures(wrappedTest, fixtures);
|
|
132
|
+
}
|
|
85
133
|
|
|
86
134
|
//#endregion
|
|
87
|
-
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
|
135
|
+
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, extendWithFixtures$1 as extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
88
136
|
//# sourceMappingURL=objection.mjs.map
|
package/dist/objection.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objection.mjs","names":["api: TestAPI","conn: DatabaseConnection<Knex>","setup?: (trx: Knex.Transaction) => Promise<void>","level: IsolationLevel"],"sources":["../src/objection.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { TestAPI } from 'vitest';\nimport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nimport {\n type DatabaseConnection,\n IsolationLevel,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific exports for test utilities.\n * Provides factory implementation for creating test data with Objection.js ORM\n * and transaction isolation for test suites.\n */\n\nexport { ObjectionFactory } from './ObjectionFactory';\nexport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\nexport { PostgresObjectionMigrator } from './PostgresObjectionMigrator';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param conn - The Knex database connection instance\n * @param setup - Optional setup function to run before each test in the transaction\n * @param level - Transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';\n * import { knex } from './database';\n * import { User, Post } from './models';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await User.query(trx)\n * .insert({ name: 'Test User', email: 'test@example.com' });\n *\n * expect(user).toBeDefined();\n * // User is automatically rolled back after test\n * });\n *\n * // With setup function for common test data\n * const testWithSetup = wrapVitestObjectionTransaction(\n * test,\n * knex,\n * async (trx) => {\n * // Create common test data\n * await knex('settings')\n * .transacting(trx)\n * .insert({ key: 'test_mode', value: 'true' });\n * }\n * );\n *\n * testWithSetup('should have test settings', async ({ trx }) => {\n * const setting = await knex('settings')\n * .transacting(trx)\n * .where('key', 'test_mode')\n * .first();\n *\n * expect(setting?.value).toBe('true');\n * });\n *\n * // Example with factory and transaction\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n * const factory = new ObjectionFactory(builders, seeds, knex);\n *\n * isolatedTest('creates related data', async ({ trx }) => {\n * // Factory can use the transaction\n * const user = await User.query(trx).insert({ name: 'Author' });\n * const posts = await Post.query(trx).insert([\n * { title: 'Post 1', userId: user.id },\n * { title: 'Post 2', userId: user.id }\n * ]);\n *\n * const userWithPosts = await User.query(trx)\n * .findById(user.id)\n * .withGraphFetched('posts');\n *\n * expect(userWithPosts.posts).toHaveLength(2);\n * });\n * ```\n */\nexport function wrapVitestObjectionTransaction(\n api: TestAPI,\n conn: DatabaseConnection<Knex>,\n setup?: (trx: Knex.Transaction) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n) {\n const wrapper = new VitestObjectionTransactionIsolator(api);\n\n return wrapper.wrapVitestWithTransaction(conn, setup, level);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"objection.mjs","names":["api: TestAPI","conn: DatabaseConnection<Knex>","setup?: (trx: Knex.Transaction) => Promise<void>","level: IsolationLevel","extendWithFixtures","wrappedTest: T","fixtures: FixtureCreators<Knex.Transaction, Extended>"],"sources":["../src/objection.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { TestAPI } from 'vitest';\nimport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nimport {\n type DatabaseConnection,\n type FixtureCreators,\n IsolationLevel,\n extendWithFixtures as baseExtendWithFixtures,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific exports for test utilities.\n * Provides factory implementation for creating test data with Objection.js ORM\n * and transaction isolation for test suites.\n */\n\nexport { ObjectionFactory } from './ObjectionFactory';\nexport { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';\nexport { IsolationLevel } from './VitestTransactionIsolator';\nexport { PostgresObjectionMigrator } from './PostgresObjectionMigrator';\nexport type { FixtureCreators } from './VitestTransactionIsolator';\n\n// Re-export faker and FakerFactory for type portability in declaration files\nexport { faker, type FakerFactory } from './faker';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.\n * Each test runs in an isolated database transaction that is rolled back after completion.\n * This ensures tests don't affect each other's data and run faster than truncating tables.\n *\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param conn - The Knex database connection instance\n * @param setup - Optional setup function to run before each test in the transaction\n * @param level - Transaction isolation level (defaults to REPEATABLE_READ)\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';\n * import { knex } from './database';\n * import { User, Post } from './models';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n *\n * // Use in tests - each test gets its own transaction\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await User.query(trx)\n * .insert({ name: 'Test User', email: 'test@example.com' });\n *\n * expect(user).toBeDefined();\n * // User is automatically rolled back after test\n * });\n *\n * // With setup function for common test data\n * const testWithSetup = wrapVitestObjectionTransaction(\n * test,\n * knex,\n * async (trx) => {\n * // Create common test data\n * await knex('settings')\n * .transacting(trx)\n * .insert({ key: 'test_mode', value: 'true' });\n * }\n * );\n *\n * testWithSetup('should have test settings', async ({ trx }) => {\n * const setting = await knex('settings')\n * .transacting(trx)\n * .where('key', 'test_mode')\n * .first();\n *\n * expect(setting?.value).toBe('true');\n * });\n *\n * // Example with factory and transaction\n * const isolatedTest = wrapVitestObjectionTransaction(test, knex);\n * const factory = new ObjectionFactory(builders, seeds, knex);\n *\n * isolatedTest('creates related data', async ({ trx }) => {\n * // Factory can use the transaction\n * const user = await User.query(trx).insert({ name: 'Author' });\n * const posts = await Post.query(trx).insert([\n * { title: 'Post 1', userId: user.id },\n * { title: 'Post 2', userId: user.id }\n * ]);\n *\n * const userWithPosts = await User.query(trx)\n * .findById(user.id)\n * .withGraphFetched('posts');\n *\n * expect(userWithPosts.posts).toHaveLength(2);\n * });\n * ```\n */\nexport function wrapVitestObjectionTransaction(\n api: TestAPI,\n conn: DatabaseConnection<Knex>,\n setup?: (trx: Knex.Transaction) => Promise<void>,\n level: IsolationLevel = IsolationLevel.REPEATABLE_READ,\n) {\n const wrapper = new VitestObjectionTransactionIsolator(api);\n\n return wrapper.wrapVitestWithTransaction(conn, setup, level);\n}\n\n/**\n * Extends an Objection.js transaction-wrapped test with additional fixtures.\n * Each fixture receives the transaction and can create dependencies like factories or repositories.\n *\n * @template Extended - The type of additional fixtures to provide\n * @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';\n * import { User } from './models';\n *\n * // Define your builders\n * const builders = {\n * user: ObjectionFactory.createBuilder(User, ({ faker }) => ({\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * })),\n * };\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);\n *\n * // Extend with fixtures - each fixture receives the transaction\n * const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(\n * baseTest,\n * {\n * factory: (trx) => new ObjectionFactory(builders, {}, trx),\n * }\n * );\n *\n * // Use in tests - both trx and factory are available\n * it('should create user with factory', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test User' });\n * expect(user.id).toBeDefined();\n *\n * // Verify in database\n * const found = await User.query(trx).findById(user.id);\n * expect(found?.name).toBe('Test User');\n * });\n * ```\n */\nexport function extendWithFixtures<\n Extended extends Record<string, unknown>,\n T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>,\n>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>) {\n return baseExtendWithFixtures<Knex.Transaction, Extended, T>(\n wrappedTest,\n fixtures,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgGA,SAAgB,+BACdA,KACAC,MACAC,OACAC,QAAwB,eAAe,iBACvC;CACA,MAAM,UAAU,IAAI,mCAAmC;AAEvD,QAAO,QAAQ,0BAA0B,MAAM,OAAO,MAAM;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CD,SAAgBC,qBAGdC,aAAgBC,UAAuD;AACvE,QAAO,mBACL,aACA,SACD;AACF"}
|
package/dist/os/directory.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DirectoryFixtures, itWithDir } from "../directory-
|
|
1
|
+
import { DirectoryFixtures, itWithDir } from "../directory-DlkPEzL4.cjs";
|
|
2
2
|
export { DirectoryFixtures, itWithDir };
|
package/dist/os/directory.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DirectoryFixtures, itWithDir } from "../directory-
|
|
1
|
+
import { DirectoryFixtures, itWithDir } from "../directory-BXavAeJZ.mjs";
|
|
2
2
|
export { DirectoryFixtures, itWithDir };
|
package/dist/os/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { itWithDir } from "../directory-
|
|
1
|
+
import { itWithDir } from "../directory-DlkPEzL4.cjs";
|
|
2
2
|
export { itWithDir };
|
package/dist/os/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { itWithDir } from "../directory-
|
|
1
|
+
import { itWithDir } from "../directory-BXavAeJZ.mjs";
|
|
2
2
|
export { itWithDir };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -38,15 +38,24 @@
|
|
|
38
38
|
"types": "./dist/logger.d.ts",
|
|
39
39
|
"import": "./dist/logger.mjs",
|
|
40
40
|
"require": "./dist/logger.cjs"
|
|
41
|
+
},
|
|
42
|
+
"./better-auth": {
|
|
43
|
+
"types": "./dist/better-auth.d.ts",
|
|
44
|
+
"import": "./dist/better-auth.mjs",
|
|
45
|
+
"require": "./dist/better-auth.cjs"
|
|
41
46
|
}
|
|
42
47
|
},
|
|
43
48
|
"dependencies": {
|
|
44
49
|
"@faker-js/faker": "~9.9.0",
|
|
45
|
-
"@geekmidas/logger": "0.0
|
|
50
|
+
"@geekmidas/logger": "0.1.0"
|
|
46
51
|
},
|
|
47
52
|
"devDependencies": {
|
|
48
53
|
"@types/pg": "~8.15.4"
|
|
49
54
|
},
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "https://github.com/geekmidas/toolbox"
|
|
58
|
+
},
|
|
50
59
|
"publishConfig": {
|
|
51
60
|
"registry": "https://registry.npmjs.org/",
|
|
52
61
|
"access": "public"
|
|
@@ -59,6 +68,7 @@
|
|
|
59
68
|
"db-errors": "~0.2.3",
|
|
60
69
|
"vitest": "~3.2.4",
|
|
61
70
|
"@types/aws-lambda": ">=8.10.92",
|
|
62
|
-
"
|
|
71
|
+
"better-auth": ">=1.3.34",
|
|
72
|
+
"@geekmidas/envkit": "0.1.0"
|
|
63
73
|
}
|
|
64
74
|
}
|
package/src/KyselyFactory.ts
CHANGED
|
@@ -25,13 +25,13 @@ import { type FakerFactory, faker } from './faker.ts';
|
|
|
25
25
|
*
|
|
26
26
|
* // Create builders
|
|
27
27
|
* const builders = {
|
|
28
|
-
* user: KyselyFactory.createBuilder<Database, 'users'>('users', (attrs,
|
|
28
|
+
* user: KyselyFactory.createBuilder<Database, 'users'>('users', ({ attrs, faker }) => ({
|
|
29
29
|
* id: faker.string.uuid(),
|
|
30
30
|
* name: faker.person.fullName(),
|
|
31
31
|
* email: faker.internet.email(),
|
|
32
32
|
* ...attrs
|
|
33
33
|
* })),
|
|
34
|
-
* post: KyselyFactory.createBuilder<Database, 'posts'>('posts', (attrs) => ({
|
|
34
|
+
* post: KyselyFactory.createBuilder<Database, 'posts'>('posts', ({ attrs }) => ({
|
|
35
35
|
* title: 'Test Post',
|
|
36
36
|
* content: 'Test content',
|
|
37
37
|
* ...attrs
|
|
@@ -88,15 +88,15 @@ export class KyselyFactory<
|
|
|
88
88
|
* @template Result - The result type (defaults to Selectable of the table)
|
|
89
89
|
*
|
|
90
90
|
* @param table - The name of the database table
|
|
91
|
-
* @param
|
|
91
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
92
92
|
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
93
93
|
* @returns A builder function that creates and optionally inserts records
|
|
94
94
|
*
|
|
95
95
|
* @example
|
|
96
96
|
* ```typescript
|
|
97
|
-
* // Create a simple builder with defaults
|
|
97
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
98
98
|
* const userBuilder = KyselyFactory.createBuilder<DB, 'users'>('users',
|
|
99
|
-
* (attrs,
|
|
99
|
+
* ({ attrs, faker }) => ({
|
|
100
100
|
* id: faker.string.uuid(),
|
|
101
101
|
* name: faker.person.fullName(),
|
|
102
102
|
* email: faker.internet.email(),
|
|
@@ -105,9 +105,17 @@ export class KyselyFactory<
|
|
|
105
105
|
* })
|
|
106
106
|
* );
|
|
107
107
|
*
|
|
108
|
+
* // Only need faker? Just destructure that
|
|
109
|
+
* const leaveTypeBuilder = KyselyFactory.createBuilder<DB, 'leaveTypes'>('leaveTypes',
|
|
110
|
+
* ({ faker }) => ({
|
|
111
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
112
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
113
|
+
* })
|
|
114
|
+
* );
|
|
115
|
+
*
|
|
108
116
|
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
109
117
|
* const addressBuilder = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',
|
|
110
|
-
* (attrs) => ({
|
|
118
|
+
* ({ attrs }) => ({
|
|
111
119
|
* street: '123 Main St',
|
|
112
120
|
* city: 'Anytown',
|
|
113
121
|
* ...attrs
|
|
@@ -126,12 +134,12 @@ export class KyselyFactory<
|
|
|
126
134
|
Result = Selectable<DB[TableName]>,
|
|
127
135
|
>(
|
|
128
136
|
table: TableName,
|
|
129
|
-
|
|
130
|
-
attrs: Attrs
|
|
131
|
-
factory: Factory
|
|
132
|
-
db: Kysely<DB
|
|
133
|
-
faker: FakerFactory
|
|
134
|
-
) =>
|
|
137
|
+
defaults?: (context: {
|
|
138
|
+
attrs: Attrs;
|
|
139
|
+
factory: Factory;
|
|
140
|
+
db: Kysely<DB>;
|
|
141
|
+
faker: FakerFactory;
|
|
142
|
+
}) =>
|
|
135
143
|
| Partial<Insertable<DB[TableName]>>
|
|
136
144
|
| Promise<Partial<Insertable<DB[TableName]>>>,
|
|
137
145
|
autoInsert?: boolean,
|
|
@@ -145,15 +153,20 @@ export class KyselyFactory<
|
|
|
145
153
|
attrs: Attrs,
|
|
146
154
|
factory: Factory,
|
|
147
155
|
db: Kysely<DB>,
|
|
148
|
-
|
|
156
|
+
fakerInstance: FakerFactory,
|
|
149
157
|
) => {
|
|
150
158
|
// Start with attributes
|
|
151
159
|
let data: Partial<Insertable<DB[TableName]>> = { ...attrs };
|
|
152
160
|
|
|
153
161
|
// Apply defaults
|
|
154
|
-
if (
|
|
155
|
-
const
|
|
156
|
-
|
|
162
|
+
if (defaults) {
|
|
163
|
+
const defaultValues = await defaults({
|
|
164
|
+
attrs,
|
|
165
|
+
factory,
|
|
166
|
+
db,
|
|
167
|
+
faker: fakerInstance,
|
|
168
|
+
});
|
|
169
|
+
data = { ...defaultValues, ...data };
|
|
157
170
|
}
|
|
158
171
|
|
|
159
172
|
// Handle insertion based on autoInsert flag
|