@geekmidas/testkit 0.0.17 → 0.1.1
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 +445 -193
- package/dist/{Factory-BZ8uMoXl.d.cts → Factory-DE3hE0WO.d.mts} +2 -2
- package/dist/{Factory-CRquB4vz.d.mts → Factory-pNV7ZQ7-.d.cts} +2 -2
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-BDS_QqRT.d.mts → KyselyFactory-CPZTUuMB.d.cts} +23 -10
- 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-DRQ83r0o.d.cts → KyselyFactory-RAyvZ8Cj.d.mts} +23 -10
- package/dist/KyselyFactory.cjs +1 -1
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/KyselyFactory.mjs +1 -1
- package/dist/{ObjectionFactory-C3tHvX1d.d.mts → ObjectionFactory-BlBicEia.d.mts} +26 -13
- package/dist/{ObjectionFactory-C4X78k0B.d.cts → ObjectionFactory-BwpN4gMX.d.cts} +26 -13
- 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 +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/ObjectionFactory.mjs +1 -1
- package/dist/{VitestKyselyTransactionIsolator-COCVfvfr.d.mts → VitestKyselyTransactionIsolator-BqrZDeaT.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-DiskaURs.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-CD2ucJpH.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-CJ4ds5Qv.d.cts → VitestObjectionTransactionIsolator-DhQ8XGva.d.mts} +2 -2
- 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.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-DLdQlfZ5.d.mts} +70 -3
- package/dist/{VitestTransactionIsolator-DdLNODZg.d.cts → VitestTransactionIsolator-DfA80g2M.d.cts} +70 -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 +8 -11
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.mts +2 -2
- package/dist/better-auth.mjs +8 -11
- package/dist/better-auth.mjs.map +1 -1
- package/dist/{directory-B4oYx02C.d.mts → directory-Mi7tdOuD.d.cts} +3 -3
- package/dist/{directory-BUcnztHI.d.cts → directory-Q178x53k.d.mts} +3 -3
- package/dist/{faker-Cg76aFNO.d.cts → faker-BSH1EMtg.d.cts} +3 -3
- package/dist/{faker-Br8MzXil.d.mts → faker-D9gz7KjY.d.mts} +3 -3
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/kysely.cjs +58 -4
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +56 -6
- package/dist/kysely.d.mts +56 -6
- 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 +52 -6
- package/dist/objection.d.mts +52 -6
- 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 +7 -3
- package/src/KyselyFactory.ts +29 -16
- package/src/ObjectionFactory.ts +34 -19
- package/src/VitestTransactionIsolator.ts +126 -2
- package/src/__tests__/KyselyFactory.spec.ts +10 -10
- package/src/__tests__/ObjectionFactory.spec.ts +9 -12
- package/src/__tests__/integration.spec.ts +171 -14
- package/src/better-auth.ts +13 -15
- package/src/kysely.ts +71 -0
- package/src/objection.ts +66 -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
|
@@ -24,7 +24,7 @@ describe('KyselyFactory', () => {
|
|
|
24
24
|
async ({ trx }) => {
|
|
25
25
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
26
26
|
'users',
|
|
27
|
-
async (attrs) => ({
|
|
27
|
+
async ({ attrs }) => ({
|
|
28
28
|
name: 'John Doe',
|
|
29
29
|
email: `user${Date.now()}@example.com`,
|
|
30
30
|
createdAt: new Date(),
|
|
@@ -57,7 +57,7 @@ describe('KyselyFactory', () => {
|
|
|
57
57
|
async ({ trx }) => {
|
|
58
58
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
59
59
|
'users',
|
|
60
|
-
async (attrs) => ({
|
|
60
|
+
async ({ attrs }) => ({
|
|
61
61
|
name: 'John Doe',
|
|
62
62
|
email: `user${Date.now()}@example.com`,
|
|
63
63
|
createdAt: new Date(),
|
|
@@ -98,7 +98,7 @@ describe('KyselyFactory', () => {
|
|
|
98
98
|
|
|
99
99
|
const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
|
|
100
100
|
'posts',
|
|
101
|
-
async (attrs, factory) => {
|
|
101
|
+
async ({ attrs, factory }) => {
|
|
102
102
|
// Create a user if userId not provided
|
|
103
103
|
if (!attrs.userId) {
|
|
104
104
|
const user = await factory.insert('user');
|
|
@@ -156,7 +156,7 @@ describe('KyselyFactory', () => {
|
|
|
156
156
|
async ({ trx }) => {
|
|
157
157
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
158
158
|
'users',
|
|
159
|
-
async (
|
|
159
|
+
async () => ({
|
|
160
160
|
name: 'John Doe',
|
|
161
161
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
162
162
|
createdAt: new Date(),
|
|
@@ -189,7 +189,7 @@ describe('KyselyFactory', () => {
|
|
|
189
189
|
async ({ trx }) => {
|
|
190
190
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
191
191
|
'users',
|
|
192
|
-
async (
|
|
192
|
+
async () => ({
|
|
193
193
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
194
194
|
createdAt: new Date(),
|
|
195
195
|
}),
|
|
@@ -233,7 +233,7 @@ describe('KyselyFactory', () => {
|
|
|
233
233
|
let counter = 0;
|
|
234
234
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
235
235
|
'users',
|
|
236
|
-
async (
|
|
236
|
+
async () => {
|
|
237
237
|
// Simulate async operation
|
|
238
238
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
239
239
|
counter++;
|
|
@@ -307,7 +307,7 @@ describe('KyselyFactory', () => {
|
|
|
307
307
|
async ({ trx }) => {
|
|
308
308
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
309
309
|
'users',
|
|
310
|
-
async (attrs) => ({
|
|
310
|
+
async ({ attrs }) => ({
|
|
311
311
|
name: 'John Doe',
|
|
312
312
|
email: `user${Date.now()}@example.com`,
|
|
313
313
|
createdAt: new Date(),
|
|
@@ -364,7 +364,7 @@ describe('KyselyFactory', () => {
|
|
|
364
364
|
async ({ trx }) => {
|
|
365
365
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
366
366
|
'users',
|
|
367
|
-
async (attrs) => ({
|
|
367
|
+
async ({ attrs }) => ({
|
|
368
368
|
name: 'John Doe',
|
|
369
369
|
email: `user${Date.now()}@example.com`,
|
|
370
370
|
createdAt: new Date(),
|
|
@@ -400,7 +400,7 @@ describe('KyselyFactory', () => {
|
|
|
400
400
|
async ({ trx }) => {
|
|
401
401
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
402
402
|
'users',
|
|
403
|
-
async (attrs) => ({
|
|
403
|
+
async ({ attrs }) => ({
|
|
404
404
|
name: 'John Doe',
|
|
405
405
|
email: `user${Date.now()}@example.com`,
|
|
406
406
|
createdAt: new Date(),
|
|
@@ -409,7 +409,7 @@ describe('KyselyFactory', () => {
|
|
|
409
409
|
|
|
410
410
|
const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
|
|
411
411
|
'posts',
|
|
412
|
-
async (
|
|
412
|
+
async ({ factory }) => {
|
|
413
413
|
const user = await factory.insert('user');
|
|
414
414
|
return {
|
|
415
415
|
title: 'Default Post',
|
|
@@ -323,7 +323,7 @@ describe('ObjectionFactory', () => {
|
|
|
323
323
|
it('should create a builder function with auto-insert', async ({ trx }) => {
|
|
324
324
|
const userBuilder = ObjectionFactory.createBuilder(
|
|
325
325
|
User,
|
|
326
|
-
(attrs,
|
|
326
|
+
({ attrs, faker }) => ({
|
|
327
327
|
name: faker.person.fullName(),
|
|
328
328
|
...attrs,
|
|
329
329
|
}),
|
|
@@ -344,7 +344,7 @@ describe('ObjectionFactory', () => {
|
|
|
344
344
|
}) => {
|
|
345
345
|
const userBuilder = ObjectionFactory.createBuilder(
|
|
346
346
|
User,
|
|
347
|
-
(attrs) => ({
|
|
347
|
+
({ attrs }) => ({
|
|
348
348
|
name: 'No Insert User',
|
|
349
349
|
...attrs,
|
|
350
350
|
}),
|
|
@@ -369,8 +369,8 @@ describe('ObjectionFactory', () => {
|
|
|
369
369
|
|
|
370
370
|
const userBuilder = ObjectionFactory.createBuilder(
|
|
371
371
|
User,
|
|
372
|
-
(attrs, factory, db, fakerInstance) => {
|
|
373
|
-
capturedFactory =
|
|
372
|
+
({ attrs, factory: passedFactory, db, faker: fakerInstance }) => {
|
|
373
|
+
capturedFactory = passedFactory;
|
|
374
374
|
capturedDb = db;
|
|
375
375
|
capturedFaker = fakerInstance;
|
|
376
376
|
|
|
@@ -394,7 +394,7 @@ describe('ObjectionFactory', () => {
|
|
|
394
394
|
it('should handle async item functions', async ({ trx }) => {
|
|
395
395
|
const userBuilder = ObjectionFactory.createBuilder(
|
|
396
396
|
User,
|
|
397
|
-
async (attrs
|
|
397
|
+
async ({ attrs }) => {
|
|
398
398
|
// Simulate async operation
|
|
399
399
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
400
400
|
|
|
@@ -433,13 +433,10 @@ describe('ObjectionFactory', () => {
|
|
|
433
433
|
});
|
|
434
434
|
|
|
435
435
|
it('should allow overriding default values', async ({ trx }) => {
|
|
436
|
-
const userBuilder = ObjectionFactory.createBuilder(
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
...attrs,
|
|
441
|
-
}),
|
|
442
|
-
);
|
|
436
|
+
const userBuilder = ObjectionFactory.createBuilder(User, ({ attrs }) => ({
|
|
437
|
+
name: 'Default Name',
|
|
438
|
+
...attrs,
|
|
439
|
+
}));
|
|
443
440
|
|
|
444
441
|
const builders = { user: userBuilder };
|
|
445
442
|
const factory = new ObjectionFactory(builders, {}, trx);
|
|
@@ -3,7 +3,7 @@ import { TEST_DATABASE_CONFIG } from '../../test/globalSetup';
|
|
|
3
3
|
import { type TestDatabase, createTestTables } from '../../test/helpers';
|
|
4
4
|
import { KyselyFactory } from '../KyselyFactory';
|
|
5
5
|
import { createKyselyDb } from '../helpers';
|
|
6
|
-
import { wrapVitestKyselyTransaction } from '../kysely';
|
|
6
|
+
import { extendWithFixtures, wrapVitestKyselyTransaction } from '../kysely';
|
|
7
7
|
|
|
8
8
|
const db = () => createKyselyDb<TestDatabase>(TEST_DATABASE_CONFIG);
|
|
9
9
|
const it = wrapVitestKyselyTransaction<TestDatabase>(
|
|
@@ -18,7 +18,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
18
18
|
// Create builders for all entities
|
|
19
19
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
20
20
|
'users',
|
|
21
|
-
async (
|
|
21
|
+
async () => ({
|
|
22
22
|
name: 'John Doe',
|
|
23
23
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
24
24
|
role: 'user' as const,
|
|
@@ -29,7 +29,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
29
29
|
|
|
30
30
|
const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
|
|
31
31
|
'posts',
|
|
32
|
-
async (attrs, factory) => {
|
|
32
|
+
async ({ attrs, factory }) => {
|
|
33
33
|
// Create a user if no userId provided
|
|
34
34
|
if (!attrs.userId) {
|
|
35
35
|
const user = await factory.insert('user');
|
|
@@ -55,7 +55,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
55
55
|
const commentBuilder = KyselyFactory.createBuilder<
|
|
56
56
|
TestDatabase,
|
|
57
57
|
'comments'
|
|
58
|
-
>('comments', async (attrs, factory) => {
|
|
58
|
+
>('comments', async ({ attrs, factory }) => {
|
|
59
59
|
let postId = attrs.postId;
|
|
60
60
|
let userId = attrs.userId;
|
|
61
61
|
|
|
@@ -152,7 +152,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
152
152
|
|
|
153
153
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
154
154
|
'users',
|
|
155
|
-
async (
|
|
155
|
+
async () => ({
|
|
156
156
|
name: 'Default User',
|
|
157
157
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
158
158
|
role: 'user' as const,
|
|
@@ -163,7 +163,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
163
163
|
|
|
164
164
|
const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
|
|
165
165
|
'posts',
|
|
166
|
-
async (attrs, factory) => {
|
|
166
|
+
async ({ attrs, factory }) => {
|
|
167
167
|
if (!attrs.userId) {
|
|
168
168
|
const user = await factory.insert('user');
|
|
169
169
|
return {
|
|
@@ -193,7 +193,10 @@ describe('Testkit Integration Tests', () => {
|
|
|
193
193
|
// Create complex seeds
|
|
194
194
|
const seeds = {
|
|
195
195
|
blogWithAdminAndPosts: KyselyFactory.createSeed(
|
|
196
|
-
async (
|
|
196
|
+
async (
|
|
197
|
+
attrs: { postCount?: number },
|
|
198
|
+
factory: KyselyFactory<TestDatabase, typeof builders, {}>,
|
|
199
|
+
) => {
|
|
197
200
|
// Create admin user
|
|
198
201
|
const admin = await factory.insert('user', {
|
|
199
202
|
name: 'Blog Admin',
|
|
@@ -229,13 +232,15 @@ describe('Testkit Integration Tests', () => {
|
|
|
229
232
|
usersWithPosts: KyselyFactory.createSeed(
|
|
230
233
|
async (
|
|
231
234
|
attrs: { userCount?: number; postsPerUser?: number },
|
|
232
|
-
factory:
|
|
233
|
-
db: any,
|
|
235
|
+
factory: KyselyFactory<TestDatabase, typeof builders, {}>,
|
|
234
236
|
) => {
|
|
235
237
|
const userCount = attrs.userCount || 2;
|
|
236
238
|
const postsPerUser = attrs.postsPerUser || 2;
|
|
237
239
|
|
|
238
|
-
const results: Array<{
|
|
240
|
+
const results: Array<{
|
|
241
|
+
user: Awaited<ReturnType<typeof builders.user>>;
|
|
242
|
+
posts: Awaited<ReturnType<typeof builders.post>>[];
|
|
243
|
+
}> = [];
|
|
239
244
|
|
|
240
245
|
for (let i = 0; i < userCount; i++) {
|
|
241
246
|
const user = await factory.insert('user', {
|
|
@@ -306,7 +311,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
306
311
|
it('should handle transaction isolation properly', async ({ trx }) => {
|
|
307
312
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
308
313
|
'users',
|
|
309
|
-
async (
|
|
314
|
+
async ({ faker }) => ({
|
|
310
315
|
name: 'Test User',
|
|
311
316
|
email: faker.internet.email(),
|
|
312
317
|
role: 'user' as const,
|
|
@@ -345,7 +350,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
345
350
|
it('should handle creating many records efficiently', async ({ trx }) => {
|
|
346
351
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
347
352
|
'users',
|
|
348
|
-
async (
|
|
353
|
+
async ({ faker }) => ({
|
|
349
354
|
name: `User ${Math.random()}`,
|
|
350
355
|
email: faker.internet.email().toLowerCase(),
|
|
351
356
|
role: 'user' as const,
|
|
@@ -381,7 +386,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
381
386
|
it('should handle complex attribute generation', async ({ trx }) => {
|
|
382
387
|
const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
383
388
|
'users',
|
|
384
|
-
async (attrs,
|
|
389
|
+
async ({ attrs, faker }) => {
|
|
385
390
|
return {
|
|
386
391
|
name: `Generated User ${attrs.id}`,
|
|
387
392
|
email: faker.internet.email().toLowerCase(),
|
|
@@ -392,7 +397,7 @@ describe('Testkit Integration Tests', () => {
|
|
|
392
397
|
|
|
393
398
|
const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
|
|
394
399
|
'posts',
|
|
395
|
-
async (attrs, factory) => {
|
|
400
|
+
async ({ attrs, factory }) => {
|
|
396
401
|
let userId = attrs.userId;
|
|
397
402
|
if (!userId) {
|
|
398
403
|
const user = await factory.insert('user');
|
|
@@ -445,3 +450,155 @@ describe('Testkit Integration Tests', () => {
|
|
|
445
450
|
});
|
|
446
451
|
});
|
|
447
452
|
});
|
|
453
|
+
|
|
454
|
+
describe('extendWithFixtures', () => {
|
|
455
|
+
// Create builders for use in extended fixtures
|
|
456
|
+
const builders = {
|
|
457
|
+
user: KyselyFactory.createBuilder<TestDatabase, 'users'>(
|
|
458
|
+
'users',
|
|
459
|
+
({ faker }) => ({
|
|
460
|
+
name: faker.person.fullName(),
|
|
461
|
+
email: faker.internet.email().toLowerCase(),
|
|
462
|
+
role: 'user' as const,
|
|
463
|
+
createdAt: new Date(),
|
|
464
|
+
updatedAt: new Date(),
|
|
465
|
+
}),
|
|
466
|
+
),
|
|
467
|
+
post: KyselyFactory.createBuilder<TestDatabase, 'posts'>(
|
|
468
|
+
'posts',
|
|
469
|
+
async ({ attrs, factory, faker }) => {
|
|
470
|
+
const userId = attrs.userId ?? (await factory.insert('user')).id;
|
|
471
|
+
return {
|
|
472
|
+
title: faker.lorem.sentence(),
|
|
473
|
+
content: faker.lorem.paragraphs(),
|
|
474
|
+
userId,
|
|
475
|
+
published: false,
|
|
476
|
+
createdAt: new Date(),
|
|
477
|
+
updatedAt: new Date(),
|
|
478
|
+
};
|
|
479
|
+
},
|
|
480
|
+
),
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// Create base test with transaction
|
|
484
|
+
const baseTest = wrapVitestKyselyTransaction<TestDatabase>(
|
|
485
|
+
base,
|
|
486
|
+
db,
|
|
487
|
+
createTestTables,
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
// Extend with factory fixture
|
|
491
|
+
const itWithFactory = extendWithFixtures<
|
|
492
|
+
TestDatabase,
|
|
493
|
+
{ factory: KyselyFactory<TestDatabase, typeof builders, {}> }
|
|
494
|
+
>(baseTest, {
|
|
495
|
+
factory: (trx) => new KyselyFactory(builders, {}, trx),
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
itWithFactory(
|
|
499
|
+
'should provide factory fixture alongside trx',
|
|
500
|
+
async ({ trx, factory }) => {
|
|
501
|
+
// Both trx and factory should be available
|
|
502
|
+
expect(trx).toBeDefined();
|
|
503
|
+
expect(factory).toBeDefined();
|
|
504
|
+
expect(factory).toBeInstanceOf(KyselyFactory);
|
|
505
|
+
|
|
506
|
+
// Factory should work with the transaction
|
|
507
|
+
const user = await factory.insert('user', { name: 'Test User' });
|
|
508
|
+
expect(user.id).toBeDefined();
|
|
509
|
+
expect(user.name).toBe('Test User');
|
|
510
|
+
|
|
511
|
+
// Verify user exists in transaction
|
|
512
|
+
const found = await trx
|
|
513
|
+
.selectFrom('users')
|
|
514
|
+
.where('id', '=', user.id)
|
|
515
|
+
.selectAll()
|
|
516
|
+
.executeTakeFirst();
|
|
517
|
+
|
|
518
|
+
expect(found).toBeDefined();
|
|
519
|
+
expect(found?.name).toBe('Test User');
|
|
520
|
+
},
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
itWithFactory(
|
|
524
|
+
'should allow factory to create related records',
|
|
525
|
+
async ({ factory }) => {
|
|
526
|
+
// Create user first
|
|
527
|
+
const user = await factory.insert('user', {
|
|
528
|
+
name: 'Author',
|
|
529
|
+
email: 'author@example.com',
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
// Create posts for the user
|
|
533
|
+
const posts = await factory.insertMany(3, 'post', (idx: number) => ({
|
|
534
|
+
title: `Post ${idx + 1}`,
|
|
535
|
+
userId: user.id,
|
|
536
|
+
published: idx === 0,
|
|
537
|
+
}));
|
|
538
|
+
|
|
539
|
+
expect(posts).toHaveLength(3);
|
|
540
|
+
expect(posts[0].userId).toBe(user.id);
|
|
541
|
+
expect(posts[0].published).toBe(true);
|
|
542
|
+
expect(posts[1].published).toBe(false);
|
|
543
|
+
},
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
// Test with multiple fixtures
|
|
547
|
+
const itWithMultipleFixtures = extendWithFixtures<
|
|
548
|
+
TestDatabase,
|
|
549
|
+
{
|
|
550
|
+
factory: KyselyFactory<TestDatabase, typeof builders, {}>;
|
|
551
|
+
userCount: number;
|
|
552
|
+
}
|
|
553
|
+
>(baseTest, {
|
|
554
|
+
factory: (trx) => new KyselyFactory(builders, {}, trx),
|
|
555
|
+
userCount: () => 42, // Simple fixture that doesn't use trx
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
itWithMultipleFixtures(
|
|
559
|
+
'should support multiple fixtures',
|
|
560
|
+
async ({ trx, factory, userCount }) => {
|
|
561
|
+
expect(trx).toBeDefined();
|
|
562
|
+
expect(factory).toBeInstanceOf(KyselyFactory);
|
|
563
|
+
expect(userCount).toBe(42);
|
|
564
|
+
|
|
565
|
+
// Use the factory
|
|
566
|
+
const user = await factory.insert('user');
|
|
567
|
+
expect(user.id).toBeDefined();
|
|
568
|
+
},
|
|
569
|
+
);
|
|
570
|
+
|
|
571
|
+
// Test async fixture creators
|
|
572
|
+
const itWithAsyncFixture = extendWithFixtures<
|
|
573
|
+
TestDatabase,
|
|
574
|
+
{ initialUser: Awaited<ReturnType<typeof builders.user>> }
|
|
575
|
+
>(baseTest, {
|
|
576
|
+
initialUser: async (trx) => {
|
|
577
|
+
// Create a user directly in the fixture
|
|
578
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
579
|
+
return factory.insert('user', {
|
|
580
|
+
name: 'Initial User',
|
|
581
|
+
email: 'initial@example.com',
|
|
582
|
+
});
|
|
583
|
+
},
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
itWithAsyncFixture(
|
|
587
|
+
'should support async fixture creators',
|
|
588
|
+
async ({ trx, initialUser }) => {
|
|
589
|
+
expect(initialUser).toBeDefined();
|
|
590
|
+
expect(initialUser.name).toBe('Initial User');
|
|
591
|
+
expect(initialUser.email).toBe('initial@example.com');
|
|
592
|
+
|
|
593
|
+
// Verify user exists in database
|
|
594
|
+
const found = await trx
|
|
595
|
+
.selectFrom('users')
|
|
596
|
+
.where('id', '=', initialUser.id)
|
|
597
|
+
.selectAll()
|
|
598
|
+
.executeTakeFirst();
|
|
599
|
+
|
|
600
|
+
expect(found).toBeDefined();
|
|
601
|
+
expect(found?.id).toBe(initialUser.id);
|
|
602
|
+
},
|
|
603
|
+
);
|
|
604
|
+
});
|
package/src/better-auth.ts
CHANGED
|
@@ -65,22 +65,24 @@ function matchesWhere(record: any, where?: Where[]): boolean {
|
|
|
65
65
|
matches = recordValue !== value;
|
|
66
66
|
break;
|
|
67
67
|
case 'lt':
|
|
68
|
-
matches = recordValue < value;
|
|
68
|
+
matches = value != null && recordValue < value;
|
|
69
69
|
break;
|
|
70
70
|
case 'lte':
|
|
71
|
-
matches = recordValue <= value;
|
|
71
|
+
matches = value != null && recordValue <= value;
|
|
72
72
|
break;
|
|
73
73
|
case 'gt':
|
|
74
|
-
matches = recordValue > value;
|
|
74
|
+
matches = value != null && recordValue > value;
|
|
75
75
|
break;
|
|
76
76
|
case 'gte':
|
|
77
|
-
matches = recordValue >= value;
|
|
77
|
+
matches = value != null && recordValue >= value;
|
|
78
78
|
break;
|
|
79
79
|
case 'in':
|
|
80
|
-
matches =
|
|
80
|
+
matches =
|
|
81
|
+
Array.isArray(value) && (value as unknown[]).includes(recordValue);
|
|
81
82
|
break;
|
|
82
83
|
case 'not_in':
|
|
83
|
-
matches =
|
|
84
|
+
matches =
|
|
85
|
+
Array.isArray(value) && !(value as unknown[]).includes(recordValue);
|
|
84
86
|
break;
|
|
85
87
|
case 'contains':
|
|
86
88
|
matches =
|
|
@@ -207,7 +209,7 @@ export const memoryAdapter = (
|
|
|
207
209
|
return null;
|
|
208
210
|
},
|
|
209
211
|
|
|
210
|
-
findMany: async ({ where, model, limit, offset, sortBy
|
|
212
|
+
findMany: async ({ where, model, limit, offset, sortBy }) => {
|
|
211
213
|
debugLog('FIND_MANY', { model, where });
|
|
212
214
|
|
|
213
215
|
const modelName = getModelName(model);
|
|
@@ -230,15 +232,11 @@ export const memoryAdapter = (
|
|
|
230
232
|
}
|
|
231
233
|
|
|
232
234
|
return Promise.all(
|
|
233
|
-
results.map(
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return transformed;
|
|
237
|
-
}),
|
|
238
|
-
);
|
|
235
|
+
results.map((record) => transformOutput(record, model)),
|
|
236
|
+
) as any;
|
|
239
237
|
},
|
|
240
238
|
|
|
241
|
-
update: async ({ where, update, model
|
|
239
|
+
update: async ({ where, update, model }) => {
|
|
242
240
|
debugLog('UPDATE', { model, where });
|
|
243
241
|
|
|
244
242
|
const modelName = getModelName(model);
|
|
@@ -254,7 +252,7 @@ export const memoryAdapter = (
|
|
|
254
252
|
);
|
|
255
253
|
const updated = { ...record, ...transformedData };
|
|
256
254
|
modelData.set(id, updated);
|
|
257
|
-
return transformOutput(updated, model
|
|
255
|
+
return transformOutput(updated, model) as any;
|
|
258
256
|
}
|
|
259
257
|
}
|
|
260
258
|
return null;
|
package/src/kysely.ts
CHANGED
|
@@ -3,7 +3,9 @@ import type { TestAPI } from 'vitest';
|
|
|
3
3
|
import { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';
|
|
4
4
|
import {
|
|
5
5
|
type DatabaseConnection,
|
|
6
|
+
type FixtureCreators,
|
|
6
7
|
IsolationLevel,
|
|
8
|
+
extendWithFixtures as baseExtendWithFixtures,
|
|
7
9
|
} from './VitestTransactionIsolator';
|
|
8
10
|
|
|
9
11
|
/**
|
|
@@ -15,6 +17,15 @@ export { KyselyFactory } from './KyselyFactory';
|
|
|
15
17
|
export { PostgresKyselyMigrator } from './PostgresKyselyMigrator';
|
|
16
18
|
export { VitestKyselyTransactionIsolator } from './VitestKyselyTransactionIsolator';
|
|
17
19
|
export { IsolationLevel } from './VitestTransactionIsolator';
|
|
20
|
+
export type {
|
|
21
|
+
DatabaseFixtures,
|
|
22
|
+
ExtendedDatabaseFixtures,
|
|
23
|
+
FixtureCreators,
|
|
24
|
+
TestWithExtendedFixtures,
|
|
25
|
+
} from './VitestTransactionIsolator';
|
|
26
|
+
|
|
27
|
+
// Re-export faker and FakerFactory for type portability in declaration files
|
|
28
|
+
export { faker, type FakerFactory } from './faker';
|
|
18
29
|
|
|
19
30
|
/**
|
|
20
31
|
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
@@ -82,3 +93,63 @@ export function wrapVitestKyselyTransaction<Database>(
|
|
|
82
93
|
|
|
83
94
|
return wrapper.wrapVitestWithTransaction(connection, setup, level);
|
|
84
95
|
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Extends a Kysely transaction-wrapped test with additional fixtures.
|
|
99
|
+
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
100
|
+
*
|
|
101
|
+
* @template Database - The database schema type
|
|
102
|
+
* @template Extended - The type of additional fixtures to provide
|
|
103
|
+
* @param wrappedTest - The base wrapped test from wrapVitestKyselyTransaction
|
|
104
|
+
* @param fixtures - Object mapping fixture names to creator functions
|
|
105
|
+
* @returns An extended test API with both trx and the additional fixtures
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* import { test } from 'vitest';
|
|
110
|
+
* import { wrapVitestKyselyTransaction, extendWithFixtures, KyselyFactory } from '@geekmidas/testkit/kysely';
|
|
111
|
+
*
|
|
112
|
+
* // Define your builders
|
|
113
|
+
* const builders = {
|
|
114
|
+
* user: KyselyFactory.createBuilder<DB, 'users'>('users', ({ faker }) => ({
|
|
115
|
+
* name: faker.person.fullName(),
|
|
116
|
+
* email: faker.internet.email(),
|
|
117
|
+
* })),
|
|
118
|
+
* };
|
|
119
|
+
*
|
|
120
|
+
* // Create base wrapped test
|
|
121
|
+
* const baseTest = wrapVitestKyselyTransaction<DB>(test, db, createTestTables);
|
|
122
|
+
*
|
|
123
|
+
* // Extend with fixtures - each fixture receives the transaction
|
|
124
|
+
* const it = extendWithFixtures<DB, { factory: KyselyFactory<DB, typeof builders, {}> }>(
|
|
125
|
+
* baseTest,
|
|
126
|
+
* {
|
|
127
|
+
* factory: (trx) => new KyselyFactory(builders, {}, trx),
|
|
128
|
+
* }
|
|
129
|
+
* );
|
|
130
|
+
*
|
|
131
|
+
* // Use in tests - both trx and factory are available
|
|
132
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
133
|
+
* const user = await factory.insert('user', { name: 'Test User' });
|
|
134
|
+
* expect(user.id).toBeDefined();
|
|
135
|
+
*
|
|
136
|
+
* // Verify in database
|
|
137
|
+
* const found = await trx
|
|
138
|
+
* .selectFrom('users')
|
|
139
|
+
* .where('id', '=', user.id)
|
|
140
|
+
* .selectAll()
|
|
141
|
+
* .executeTakeFirst();
|
|
142
|
+
* expect(found?.name).toBe('Test User');
|
|
143
|
+
* });
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function extendWithFixtures<
|
|
147
|
+
Database,
|
|
148
|
+
Extended extends Record<string, unknown>,
|
|
149
|
+
T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>,
|
|
150
|
+
>(wrappedTest: T, fixtures: FixtureCreators<Transaction<Database>, Extended>) {
|
|
151
|
+
return baseExtendWithFixtures<Transaction<Database>, Extended, T>(
|
|
152
|
+
wrappedTest,
|
|
153
|
+
fixtures,
|
|
154
|
+
);
|
|
155
|
+
}
|
package/src/objection.ts
CHANGED
|
@@ -3,7 +3,9 @@ import type { TestAPI } from 'vitest';
|
|
|
3
3
|
import { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';
|
|
4
4
|
import {
|
|
5
5
|
type DatabaseConnection,
|
|
6
|
+
type FixtureCreators,
|
|
6
7
|
IsolationLevel,
|
|
8
|
+
extendWithFixtures as baseExtendWithFixtures,
|
|
7
9
|
} from './VitestTransactionIsolator';
|
|
8
10
|
|
|
9
11
|
/**
|
|
@@ -16,6 +18,15 @@ export { ObjectionFactory } from './ObjectionFactory';
|
|
|
16
18
|
export { VitestObjectionTransactionIsolator } from './VitestObjectionTransactionIsolator';
|
|
17
19
|
export { IsolationLevel } from './VitestTransactionIsolator';
|
|
18
20
|
export { PostgresObjectionMigrator } from './PostgresObjectionMigrator';
|
|
21
|
+
export type {
|
|
22
|
+
DatabaseFixtures,
|
|
23
|
+
ExtendedDatabaseFixtures,
|
|
24
|
+
FixtureCreators,
|
|
25
|
+
TestWithExtendedFixtures,
|
|
26
|
+
} from './VitestTransactionIsolator';
|
|
27
|
+
|
|
28
|
+
// Re-export faker and FakerFactory for type portability in declaration files
|
|
29
|
+
export { faker, type FakerFactory } from './faker';
|
|
19
30
|
|
|
20
31
|
/**
|
|
21
32
|
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
@@ -98,3 +109,58 @@ export function wrapVitestObjectionTransaction(
|
|
|
98
109
|
|
|
99
110
|
return wrapper.wrapVitestWithTransaction(conn, setup, level);
|
|
100
111
|
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
115
|
+
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
116
|
+
*
|
|
117
|
+
* @template Extended - The type of additional fixtures to provide
|
|
118
|
+
* @param wrappedTest - The base wrapped test from wrapVitestObjectionTransaction
|
|
119
|
+
* @param fixtures - Object mapping fixture names to creator functions
|
|
120
|
+
* @returns An extended test API with both trx and the additional fixtures
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* import { test } from 'vitest';
|
|
125
|
+
* import { wrapVitestObjectionTransaction, extendWithFixtures, ObjectionFactory } from '@geekmidas/testkit/objection';
|
|
126
|
+
* import { User } from './models';
|
|
127
|
+
*
|
|
128
|
+
* // Define your builders
|
|
129
|
+
* const builders = {
|
|
130
|
+
* user: ObjectionFactory.createBuilder(User, ({ faker }) => ({
|
|
131
|
+
* name: faker.person.fullName(),
|
|
132
|
+
* email: faker.internet.email(),
|
|
133
|
+
* })),
|
|
134
|
+
* };
|
|
135
|
+
*
|
|
136
|
+
* // Create base wrapped test
|
|
137
|
+
* const baseTest = wrapVitestObjectionTransaction(test, knex, createTestTables);
|
|
138
|
+
*
|
|
139
|
+
* // Extend with fixtures - each fixture receives the transaction
|
|
140
|
+
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
141
|
+
* baseTest,
|
|
142
|
+
* {
|
|
143
|
+
* factory: (trx) => new ObjectionFactory(builders, {}, trx),
|
|
144
|
+
* }
|
|
145
|
+
* );
|
|
146
|
+
*
|
|
147
|
+
* // Use in tests - both trx and factory are available
|
|
148
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
149
|
+
* const user = await factory.insert('user', { name: 'Test User' });
|
|
150
|
+
* expect(user.id).toBeDefined();
|
|
151
|
+
*
|
|
152
|
+
* // Verify in database
|
|
153
|
+
* const found = await User.query(trx).findById(user.id);
|
|
154
|
+
* expect(found?.name).toBe('Test User');
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function extendWithFixtures<
|
|
159
|
+
Extended extends Record<string, unknown>,
|
|
160
|
+
T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>,
|
|
161
|
+
>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>) {
|
|
162
|
+
return baseExtendWithFixtures<Knex.Transaction, Extended, T>(
|
|
163
|
+
wrappedTest,
|
|
164
|
+
fixtures,
|
|
165
|
+
);
|
|
166
|
+
}
|