@geekmidas/testkit 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +162 -13
- package/dist/{Factory-BZ8uMoXl.d.cts → Factory-D7oHtI2D.d.cts} +2 -2
- package/dist/Factory.d.cts +2 -2
- package/dist/{KyselyFactory-BFygzOlO.d.cts → KyselyFactory-dYdviox2.d.cts} +3 -3
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/{ObjectionFactory-CeSIN3kZ.d.cts → ObjectionFactory-0gUdx8bB.d.cts} +3 -3
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs → VitestKyselyTransactionIsolator-CNURW8y6.mjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map → VitestKyselyTransactionIsolator-CNURW8y6.mjs.map} +1 -1
- package/dist/{VitestKyselyTransactionIsolator-DnyZMaA-.d.mts → VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-4HOeLQ0d.d.cts → VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs → VitestKyselyTransactionIsolator-EvDLk5zg.cjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map → VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map} +1 -1
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/{VitestObjectionTransactionIsolator-COVDlpEo.d.cts → VitestObjectionTransactionIsolator-1TpsPqfG.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs → VitestObjectionTransactionIsolator-CM5KTAFA.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map → VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-lZUSz1w0.d.mts → VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs → VitestObjectionTransactionIsolator-jQFaCz0u.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map → VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map} +1 -1
- package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
- package/dist/{VitestTransactionIsolator-DWDbnITQ.d.mts → VitestTransactionIsolator-BvR19bYn.d.mts} +44 -18
- package/dist/{VitestTransactionIsolator-BKIrj3Uy.cjs → VitestTransactionIsolator-CMfJXZP8.cjs} +52 -36
- package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CyG_i_Nj.d.cts → VitestTransactionIsolator-CwQaxZLP.d.cts} +44 -18
- package/dist/{VitestTransactionIsolator-BIaMs4c2.mjs → VitestTransactionIsolator-DQ7tLqgV.mjs} +52 -36
- package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/{directory-DlkPEzL4.d.cts → directory-BUcnztHI.d.cts} +3 -3
- package/dist/{faker-Cg76aFNO.d.cts → faker-Dg3trU4a.d.cts} +3 -3
- package/dist/faker.d.cts +1 -1
- package/dist/kysely.cjs +23 -29
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +40 -38
- package/dist/kysely.d.mts +37 -35
- package/dist/kysely.mjs +23 -29
- package/dist/kysely.mjs.map +1 -1
- package/dist/objection.cjs +23 -47
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +40 -56
- package/dist/objection.d.mts +37 -53
- package/dist/objection.mjs +23 -47
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/package.json +1 -1
- package/src/VitestTransactionIsolator.ts +98 -31
- package/src/__tests__/KyselyFactory.spec.ts +4 -5
- package/src/__tests__/ObjectionFactory.spec.ts +4 -5
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +6 -3
- package/src/__tests__/integration.spec.ts +8 -10
- package/src/kysely.ts +49 -34
- package/src/objection.ts +47 -52
- package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +0 -1
package/dist/objection.d.cts
CHANGED
|
@@ -1,24 +1,36 @@
|
|
|
1
|
-
import { FakerFactory, faker } from "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
1
|
+
import { FakerFactory, faker } from "./faker-Dg3trU4a.cjs";
|
|
2
|
+
import "./Factory-D7oHtI2D.cjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-0gUdx8bB.cjs";
|
|
4
4
|
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-CZHHcCOv.cjs";
|
|
6
|
-
import { DatabaseConnection, DatabaseFixtures, FixtureCreators, IsolationLevel } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-
|
|
6
|
+
import { DatabaseConnection, DatabaseFixtures, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions } from "./VitestTransactionIsolator-CwQaxZLP.cjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-1TpsPqfG.cjs";
|
|
8
8
|
import { Knex } from "knex";
|
|
9
9
|
import { TestAPI } from "vitest";
|
|
10
10
|
|
|
11
11
|
//#region src/objection.d.ts
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Objection.js-specific options for transaction wrapping.
|
|
15
|
+
*/
|
|
16
|
+
interface ObjectionTransactionOptions<Extended extends Record<string, unknown> = {}> {
|
|
17
|
+
/** Function that creates or returns a Knex database connection */
|
|
18
|
+
connection: DatabaseConnection<Knex>;
|
|
19
|
+
/** Optional setup function to run within the transaction before each test */
|
|
20
|
+
setup?: (trx: Knex.Transaction) => Promise<void>;
|
|
21
|
+
/** Transaction isolation level (defaults to REPEATABLE_READ) */
|
|
22
|
+
isolationLevel?: IsolationLevel;
|
|
23
|
+
/** Additional fixtures that depend on the transaction */
|
|
24
|
+
fixtures?: FixtureCreators<Knex.Transaction, Extended>;
|
|
25
|
+
}
|
|
13
26
|
/**
|
|
14
27
|
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
15
28
|
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
29
|
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
30
|
*
|
|
31
|
+
* @template Extended - Additional fixtures to provide
|
|
18
32
|
* @param api - The Vitest test API (usually `test` from vitest)
|
|
19
|
-
* @param
|
|
20
|
-
* @param setup - Optional setup function to run before each test in the transaction
|
|
21
|
-
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
33
|
+
* @param options - Configuration options for transaction wrapping
|
|
22
34
|
* @returns A wrapped test API that provides transaction isolation
|
|
23
35
|
*
|
|
24
36
|
* @example
|
|
@@ -26,10 +38,12 @@ import { TestAPI } from "vitest";
|
|
|
26
38
|
* import { test } from 'vitest';
|
|
27
39
|
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
28
40
|
* import { knex } from './database';
|
|
29
|
-
* import { User
|
|
41
|
+
* import { User } from './models';
|
|
30
42
|
*
|
|
31
43
|
* // Create isolated test with automatic rollback
|
|
32
|
-
* const isolatedTest = wrapVitestObjectionTransaction(test,
|
|
44
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, {
|
|
45
|
+
* connection: knex,
|
|
46
|
+
* });
|
|
33
47
|
*
|
|
34
48
|
* // Use in tests - each test gets its own transaction
|
|
35
49
|
* isolatedTest('should create user', async ({ trx }) => {
|
|
@@ -37,53 +51,23 @@ import { TestAPI } from "vitest";
|
|
|
37
51
|
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
38
52
|
*
|
|
39
53
|
* expect(user).toBeDefined();
|
|
40
|
-
* // User is automatically rolled back after test
|
|
41
54
|
* });
|
|
42
55
|
*
|
|
43
|
-
* // With
|
|
44
|
-
* const
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* await knex('settings')
|
|
50
|
-
* .transacting(trx)
|
|
51
|
-
* .insert({ key: 'test_mode', value: 'true' });
|
|
52
|
-
* }
|
|
53
|
-
* );
|
|
54
|
-
*
|
|
55
|
-
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
56
|
-
* const setting = await knex('settings')
|
|
57
|
-
* .transacting(trx)
|
|
58
|
-
* .where('key', 'test_mode')
|
|
59
|
-
* .first();
|
|
60
|
-
*
|
|
61
|
-
* expect(setting?.value).toBe('true');
|
|
56
|
+
* // With fixtures for factories
|
|
57
|
+
* const it = wrapVitestObjectionTransaction<{ factory: Factory }>(test, {
|
|
58
|
+
* connection: knex,
|
|
59
|
+
* fixtures: {
|
|
60
|
+
* factory: (trx) => new Factory(trx),
|
|
61
|
+
* },
|
|
62
62
|
* });
|
|
63
63
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* isolatedTest('creates related data', async ({ trx }) => {
|
|
69
|
-
* // Factory can use the transaction
|
|
70
|
-
* const user = await User.query(trx).insert({ name: 'Author' });
|
|
71
|
-
* const posts = await Post.query(trx).insert([
|
|
72
|
-
* { title: 'Post 1', userId: user.id },
|
|
73
|
-
* { title: 'Post 2', userId: user.id }
|
|
74
|
-
* ]);
|
|
75
|
-
*
|
|
76
|
-
* const userWithPosts = await User.query(trx)
|
|
77
|
-
* .findById(user.id)
|
|
78
|
-
* .withGraphFetched('posts');
|
|
79
|
-
*
|
|
80
|
-
* expect(userWithPosts.posts).toHaveLength(2);
|
|
64
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
65
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
66
|
+
* expect(user.id).toBeDefined();
|
|
81
67
|
* });
|
|
82
68
|
* ```
|
|
83
69
|
*/
|
|
84
|
-
declare function wrapVitestObjectionTransaction(api: TestAPI,
|
|
85
|
-
trx: Knex.Transaction<any, any[]>;
|
|
86
|
-
}>;
|
|
70
|
+
declare function wrapVitestObjectionTransaction<Extended extends Record<string, unknown> = {}>(api: TestAPI, options: ObjectionTransactionOptions<Extended>): TestAPI<DatabaseFixtures<Knex.Transaction<any, any[]>, object> & Extended>;
|
|
87
71
|
/**
|
|
88
72
|
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
89
73
|
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
@@ -108,7 +92,10 @@ declare function wrapVitestObjectionTransaction(api: TestAPI, conn: DatabaseConn
|
|
|
108
92
|
* };
|
|
109
93
|
*
|
|
110
94
|
* // Create base wrapped test
|
|
111
|
-
* const baseTest = wrapVitestObjectionTransaction(test,
|
|
95
|
+
* const baseTest = wrapVitestObjectionTransaction(test, {
|
|
96
|
+
* connection: knex,
|
|
97
|
+
* setup: createTestTables,
|
|
98
|
+
* });
|
|
112
99
|
*
|
|
113
100
|
* // Extend with fixtures - each fixture receives the transaction
|
|
114
101
|
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
@@ -129,10 +116,7 @@ declare function wrapVitestObjectionTransaction(api: TestAPI, conn: DatabaseConn
|
|
|
129
116
|
* });
|
|
130
117
|
* ```
|
|
131
118
|
*/
|
|
132
|
-
declare function extendWithFixtures<Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>):
|
|
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
|
-
};
|
|
119
|
+
declare function extendWithFixtures<Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>): TestWithExtendedFixtures<Knex.Transaction<any, any[]>, Extended, T>;
|
|
136
120
|
//#endregion
|
|
137
|
-
export { FakerFactory, FixtureCreators, IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
121
|
+
export { DatabaseFixtures, ExtendedDatabaseFixtures, FakerFactory, FixtureCreators, IsolationLevel, ObjectionFactory, ObjectionTransactionOptions, PostgresObjectionMigrator, TestWithExtendedFixtures, TransactionWrapperOptions, VitestObjectionTransactionIsolator, extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
138
122
|
//# sourceMappingURL=objection.d.cts.map
|
package/dist/objection.d.mts
CHANGED
|
@@ -3,22 +3,34 @@ import "./Factory-Cmr3s3-s.mjs";
|
|
|
3
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, DatabaseFixtures, FixtureCreators, IsolationLevel } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-
|
|
6
|
+
import { DatabaseConnection, DatabaseFixtures, ExtendedDatabaseFixtures, FixtureCreators, IsolationLevel, TestWithExtendedFixtures, TransactionWrapperOptions } from "./VitestTransactionIsolator-BvR19bYn.mjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-i9jIgU8Q.mjs";
|
|
8
8
|
import { TestAPI } from "vitest";
|
|
9
9
|
import { Knex } from "knex";
|
|
10
10
|
|
|
11
11
|
//#region src/objection.d.ts
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Objection.js-specific options for transaction wrapping.
|
|
15
|
+
*/
|
|
16
|
+
interface ObjectionTransactionOptions<Extended extends Record<string, unknown> = {}> {
|
|
17
|
+
/** Function that creates or returns a Knex database connection */
|
|
18
|
+
connection: DatabaseConnection<Knex>;
|
|
19
|
+
/** Optional setup function to run within the transaction before each test */
|
|
20
|
+
setup?: (trx: Knex.Transaction) => Promise<void>;
|
|
21
|
+
/** Transaction isolation level (defaults to REPEATABLE_READ) */
|
|
22
|
+
isolationLevel?: IsolationLevel;
|
|
23
|
+
/** Additional fixtures that depend on the transaction */
|
|
24
|
+
fixtures?: FixtureCreators<Knex.Transaction, Extended>;
|
|
25
|
+
}
|
|
13
26
|
/**
|
|
14
27
|
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
15
28
|
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
29
|
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
30
|
*
|
|
31
|
+
* @template Extended - Additional fixtures to provide
|
|
18
32
|
* @param api - The Vitest test API (usually `test` from vitest)
|
|
19
|
-
* @param
|
|
20
|
-
* @param setup - Optional setup function to run before each test in the transaction
|
|
21
|
-
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
33
|
+
* @param options - Configuration options for transaction wrapping
|
|
22
34
|
* @returns A wrapped test API that provides transaction isolation
|
|
23
35
|
*
|
|
24
36
|
* @example
|
|
@@ -26,10 +38,12 @@ import { Knex } from "knex";
|
|
|
26
38
|
* import { test } from 'vitest';
|
|
27
39
|
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
28
40
|
* import { knex } from './database';
|
|
29
|
-
* import { User
|
|
41
|
+
* import { User } from './models';
|
|
30
42
|
*
|
|
31
43
|
* // Create isolated test with automatic rollback
|
|
32
|
-
* const isolatedTest = wrapVitestObjectionTransaction(test,
|
|
44
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, {
|
|
45
|
+
* connection: knex,
|
|
46
|
+
* });
|
|
33
47
|
*
|
|
34
48
|
* // Use in tests - each test gets its own transaction
|
|
35
49
|
* isolatedTest('should create user', async ({ trx }) => {
|
|
@@ -37,53 +51,23 @@ import { Knex } from "knex";
|
|
|
37
51
|
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
38
52
|
*
|
|
39
53
|
* expect(user).toBeDefined();
|
|
40
|
-
* // User is automatically rolled back after test
|
|
41
54
|
* });
|
|
42
55
|
*
|
|
43
|
-
* // With
|
|
44
|
-
* const
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* await knex('settings')
|
|
50
|
-
* .transacting(trx)
|
|
51
|
-
* .insert({ key: 'test_mode', value: 'true' });
|
|
52
|
-
* }
|
|
53
|
-
* );
|
|
54
|
-
*
|
|
55
|
-
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
56
|
-
* const setting = await knex('settings')
|
|
57
|
-
* .transacting(trx)
|
|
58
|
-
* .where('key', 'test_mode')
|
|
59
|
-
* .first();
|
|
60
|
-
*
|
|
61
|
-
* expect(setting?.value).toBe('true');
|
|
56
|
+
* // With fixtures for factories
|
|
57
|
+
* const it = wrapVitestObjectionTransaction<{ factory: Factory }>(test, {
|
|
58
|
+
* connection: knex,
|
|
59
|
+
* fixtures: {
|
|
60
|
+
* factory: (trx) => new Factory(trx),
|
|
61
|
+
* },
|
|
62
62
|
* });
|
|
63
63
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* isolatedTest('creates related data', async ({ trx }) => {
|
|
69
|
-
* // Factory can use the transaction
|
|
70
|
-
* const user = await User.query(trx).insert({ name: 'Author' });
|
|
71
|
-
* const posts = await Post.query(trx).insert([
|
|
72
|
-
* { title: 'Post 1', userId: user.id },
|
|
73
|
-
* { title: 'Post 2', userId: user.id }
|
|
74
|
-
* ]);
|
|
75
|
-
*
|
|
76
|
-
* const userWithPosts = await User.query(trx)
|
|
77
|
-
* .findById(user.id)
|
|
78
|
-
* .withGraphFetched('posts');
|
|
79
|
-
*
|
|
80
|
-
* expect(userWithPosts.posts).toHaveLength(2);
|
|
64
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
65
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
66
|
+
* expect(user.id).toBeDefined();
|
|
81
67
|
* });
|
|
82
68
|
* ```
|
|
83
69
|
*/
|
|
84
|
-
declare function wrapVitestObjectionTransaction(api: TestAPI,
|
|
85
|
-
trx: Knex.Transaction<any, any[]>;
|
|
86
|
-
}>;
|
|
70
|
+
declare function wrapVitestObjectionTransaction<Extended extends Record<string, unknown> = {}>(api: TestAPI, options: ObjectionTransactionOptions<Extended>): TestAPI<DatabaseFixtures<Knex.Transaction<any, any[]>, object> & Extended>;
|
|
87
71
|
/**
|
|
88
72
|
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
89
73
|
* Each fixture receives the transaction and can create dependencies like factories or repositories.
|
|
@@ -108,7 +92,10 @@ declare function wrapVitestObjectionTransaction(api: TestAPI, conn: DatabaseConn
|
|
|
108
92
|
* };
|
|
109
93
|
*
|
|
110
94
|
* // Create base wrapped test
|
|
111
|
-
* const baseTest = wrapVitestObjectionTransaction(test,
|
|
95
|
+
* const baseTest = wrapVitestObjectionTransaction(test, {
|
|
96
|
+
* connection: knex,
|
|
97
|
+
* setup: createTestTables,
|
|
98
|
+
* });
|
|
112
99
|
*
|
|
113
100
|
* // Extend with fixtures - each fixture receives the transaction
|
|
114
101
|
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
|
@@ -129,10 +116,7 @@ declare function wrapVitestObjectionTransaction(api: TestAPI, conn: DatabaseConn
|
|
|
129
116
|
* });
|
|
130
117
|
* ```
|
|
131
118
|
*/
|
|
132
|
-
declare function extendWithFixtures<Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>):
|
|
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
|
-
};
|
|
119
|
+
declare function extendWithFixtures<Extended extends Record<string, unknown>, T extends ReturnType<TestAPI['extend']> = ReturnType<TestAPI['extend']>>(wrappedTest: T, fixtures: FixtureCreators<Knex.Transaction, Extended>): TestWithExtendedFixtures<Knex.Transaction<any, any[]>, Extended, T>;
|
|
136
120
|
//#endregion
|
|
137
|
-
export { FakerFactory, FixtureCreators, IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
121
|
+
export { DatabaseFixtures, ExtendedDatabaseFixtures, FakerFactory, FixtureCreators, IsolationLevel, ObjectionFactory, ObjectionTransactionOptions, PostgresObjectionMigrator, TestWithExtendedFixtures, TransactionWrapperOptions, VitestObjectionTransactionIsolator, extendWithFixtures, faker, wrapVitestObjectionTransaction };
|
|
138
122
|
//# sourceMappingURL=objection.d.mts.map
|
package/dist/objection.mjs
CHANGED
|
@@ -3,8 +3,8 @@ import { faker } from "./faker-BGKYFoCT.mjs";
|
|
|
3
3
|
import { ObjectionFactory } from "./ObjectionFactory-zf2fLKrL.mjs";
|
|
4
4
|
import "./PostgresMigrator-DbuJGAVy.mjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-DPj2pOpX.mjs";
|
|
6
|
-
import { IsolationLevel, extendWithFixtures } from "./VitestTransactionIsolator-
|
|
7
|
-
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-
|
|
6
|
+
import { IsolationLevel, extendWithFixtures } from "./VitestTransactionIsolator-DQ7tLqgV.mjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-jQFaCz0u.mjs";
|
|
8
8
|
|
|
9
9
|
//#region src/objection.ts
|
|
10
10
|
/**
|
|
@@ -12,10 +12,9 @@ import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransaction
|
|
|
12
12
|
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
13
13
|
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
14
14
|
*
|
|
15
|
+
* @template Extended - Additional fixtures to provide
|
|
15
16
|
* @param api - The Vitest test API (usually `test` from vitest)
|
|
16
|
-
* @param
|
|
17
|
-
* @param setup - Optional setup function to run before each test in the transaction
|
|
18
|
-
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
17
|
+
* @param options - Configuration options for transaction wrapping
|
|
19
18
|
* @returns A wrapped test API that provides transaction isolation
|
|
20
19
|
*
|
|
21
20
|
* @example
|
|
@@ -23,10 +22,12 @@ import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransaction
|
|
|
23
22
|
* import { test } from 'vitest';
|
|
24
23
|
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
25
24
|
* import { knex } from './database';
|
|
26
|
-
* import { User
|
|
25
|
+
* import { User } from './models';
|
|
27
26
|
*
|
|
28
27
|
* // Create isolated test with automatic rollback
|
|
29
|
-
* const isolatedTest = wrapVitestObjectionTransaction(test,
|
|
28
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, {
|
|
29
|
+
* connection: knex,
|
|
30
|
+
* });
|
|
30
31
|
*
|
|
31
32
|
* // Use in tests - each test gets its own transaction
|
|
32
33
|
* isolatedTest('should create user', async ({ trx }) => {
|
|
@@ -34,53 +35,25 @@ import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransaction
|
|
|
34
35
|
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
35
36
|
*
|
|
36
37
|
* expect(user).toBeDefined();
|
|
37
|
-
* // User is automatically rolled back after test
|
|
38
38
|
* });
|
|
39
39
|
*
|
|
40
|
-
* // With
|
|
41
|
-
* const
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* await knex('settings')
|
|
47
|
-
* .transacting(trx)
|
|
48
|
-
* .insert({ key: 'test_mode', value: 'true' });
|
|
49
|
-
* }
|
|
50
|
-
* );
|
|
51
|
-
*
|
|
52
|
-
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
53
|
-
* const setting = await knex('settings')
|
|
54
|
-
* .transacting(trx)
|
|
55
|
-
* .where('key', 'test_mode')
|
|
56
|
-
* .first();
|
|
57
|
-
*
|
|
58
|
-
* expect(setting?.value).toBe('true');
|
|
40
|
+
* // With fixtures for factories
|
|
41
|
+
* const it = wrapVitestObjectionTransaction<{ factory: Factory }>(test, {
|
|
42
|
+
* connection: knex,
|
|
43
|
+
* fixtures: {
|
|
44
|
+
* factory: (trx) => new Factory(trx),
|
|
45
|
+
* },
|
|
59
46
|
* });
|
|
60
47
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* isolatedTest('creates related data', async ({ trx }) => {
|
|
66
|
-
* // Factory can use the transaction
|
|
67
|
-
* const user = await User.query(trx).insert({ name: 'Author' });
|
|
68
|
-
* const posts = await Post.query(trx).insert([
|
|
69
|
-
* { title: 'Post 1', userId: user.id },
|
|
70
|
-
* { title: 'Post 2', userId: user.id }
|
|
71
|
-
* ]);
|
|
72
|
-
*
|
|
73
|
-
* const userWithPosts = await User.query(trx)
|
|
74
|
-
* .findById(user.id)
|
|
75
|
-
* .withGraphFetched('posts');
|
|
76
|
-
*
|
|
77
|
-
* expect(userWithPosts.posts).toHaveLength(2);
|
|
48
|
+
* it('should create user with factory', async ({ trx, factory }) => {
|
|
49
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
50
|
+
* expect(user.id).toBeDefined();
|
|
78
51
|
* });
|
|
79
52
|
* ```
|
|
80
53
|
*/
|
|
81
|
-
function wrapVitestObjectionTransaction(api,
|
|
54
|
+
function wrapVitestObjectionTransaction(api, options) {
|
|
82
55
|
const wrapper = new VitestObjectionTransactionIsolator(api);
|
|
83
|
-
return wrapper.wrapVitestWithTransaction(
|
|
56
|
+
return wrapper.wrapVitestWithTransaction(options);
|
|
84
57
|
}
|
|
85
58
|
/**
|
|
86
59
|
* Extends an Objection.js transaction-wrapped test with additional fixtures.
|
|
@@ -106,7 +79,10 @@ function wrapVitestObjectionTransaction(api, conn, setup, level = IsolationLevel
|
|
|
106
79
|
* };
|
|
107
80
|
*
|
|
108
81
|
* // Create base wrapped test
|
|
109
|
-
* const baseTest = wrapVitestObjectionTransaction(test,
|
|
82
|
+
* const baseTest = wrapVitestObjectionTransaction(test, {
|
|
83
|
+
* connection: knex,
|
|
84
|
+
* setup: createTestTables,
|
|
85
|
+
* });
|
|
110
86
|
*
|
|
111
87
|
* // Extend with fixtures - each fixture receives the transaction
|
|
112
88
|
* const it = extendWithFixtures<{ factory: ObjectionFactory<typeof builders, {}> }>(
|
package/dist/objection.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objection.mjs","names":["api: TestAPI","
|
|
1
|
+
{"version":3,"file":"objection.mjs","names":["api: TestAPI","options: ObjectionTransactionOptions<Extended>","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 type 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 {\n DatabaseFixtures,\n ExtendedDatabaseFixtures,\n FixtureCreators,\n TestWithExtendedFixtures,\n TransactionWrapperOptions,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific options for transaction wrapping.\n */\nexport interface ObjectionTransactionOptions<\n Extended extends Record<string, unknown> = {},\n> {\n /** Function that creates or returns a Knex database connection */\n connection: DatabaseConnection<Knex>;\n /** Optional setup function to run within the transaction before each test */\n setup?: (trx: Knex.Transaction) => Promise<void>;\n /** Transaction isolation level (defaults to REPEATABLE_READ) */\n isolationLevel?: IsolationLevel;\n /** Additional fixtures that depend on the transaction */\n fixtures?: FixtureCreators<Knex.Transaction, Extended>;\n}\n\n// Re-export faker and FakerFactory for type portability in declaration files\nexport { faker, type FakerFactory } from './faker';\n\n/**\n * Creates a wrapped Vitest test API with automatic transaction rollback for 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 * @template Extended - Additional fixtures to provide\n * @param api - The Vitest test API (usually `test` from vitest)\n * @param options - Configuration options for transaction wrapping\n * @returns A wrapped test API that provides transaction isolation\n *\n * @example\n * ```typescript\n * import { test } from 'vitest';\n * import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';\n * import { knex } from './database';\n * import { User } from './models';\n *\n * // Create isolated test with automatic rollback\n * const isolatedTest = wrapVitestObjectionTransaction(test, {\n * connection: knex,\n * });\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 * });\n *\n * // With fixtures for factories\n * const it = wrapVitestObjectionTransaction<{ factory: Factory }>(test, {\n * connection: knex,\n * fixtures: {\n * factory: (trx) => new Factory(trx),\n * },\n * });\n *\n * it('should create user with factory', async ({ trx, factory }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user.id).toBeDefined();\n * });\n * ```\n */\nexport function wrapVitestObjectionTransaction<\n Extended extends Record<string, unknown> = {},\n>(api: TestAPI, options: ObjectionTransactionOptions<Extended>) {\n const wrapper = new VitestObjectionTransactionIsolator(api);\n\n return wrapper.wrapVitestWithTransaction(options);\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, {\n * connection: knex,\n * setup: createTestTables,\n * });\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,SAAgB,+BAEdA,KAAcC,SAAgD;CAC9D,MAAM,UAAU,IAAI,mCAAmC;AAEvD,QAAO,QAAQ,0BAA0B,QAAQ;AAClD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,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-BUcnztHI.cjs";
|
|
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-BUcnztHI.cjs";
|
|
2
2
|
export { itWithDir };
|
package/package.json
CHANGED
|
@@ -124,30 +124,59 @@ export abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
|
|
|
124
124
|
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
125
125
|
* Each test will run within a database transaction that is automatically rolled back.
|
|
126
126
|
*
|
|
127
|
-
* @param
|
|
128
|
-
* @param setup - Optional setup function to run within the transaction before each test
|
|
129
|
-
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
127
|
+
* @param options - Configuration options for transaction wrapping
|
|
130
128
|
* @returns A wrapped test API with transaction support
|
|
131
129
|
*
|
|
132
130
|
* @example
|
|
133
131
|
* ```typescript
|
|
134
|
-
* const isolatedTest = isolator.wrapVitestWithTransaction(
|
|
135
|
-
*
|
|
136
|
-
*
|
|
132
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction({
|
|
133
|
+
* connection: db,
|
|
134
|
+
* setup: async (trx) => {
|
|
135
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
136
|
+
* },
|
|
137
|
+
* fixtures: {
|
|
138
|
+
* factory: (trx) => new Factory(trx),
|
|
139
|
+
* },
|
|
137
140
|
* });
|
|
138
141
|
*
|
|
139
|
-
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
140
|
-
* const user = await
|
|
142
|
+
* isolatedTest('test with transaction', async ({ trx, factory }) => {
|
|
143
|
+
* const user = await factory.insert('user', { name: 'Test' });
|
|
141
144
|
* expect(user).toBeDefined();
|
|
142
145
|
* });
|
|
143
146
|
* ```
|
|
144
147
|
*/
|
|
145
|
-
wrapVitestWithTransaction(
|
|
146
|
-
|
|
147
|
-
setup?: (trx: Transaction) => Promise<void>,
|
|
148
|
-
level: IsolationLevel = IsolationLevel.REPEATABLE_READ,
|
|
148
|
+
wrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(
|
|
149
|
+
options: TransactionWrapperOptions<TConn, Transaction, Extended>,
|
|
149
150
|
) {
|
|
150
|
-
|
|
151
|
+
const {
|
|
152
|
+
connection,
|
|
153
|
+
setup,
|
|
154
|
+
isolationLevel = IsolationLevel.REPEATABLE_READ,
|
|
155
|
+
fixtures,
|
|
156
|
+
} = options;
|
|
157
|
+
|
|
158
|
+
// Build fixture definitions for additional fixtures that depend on trx
|
|
159
|
+
const additionalFixtures: Record<string, unknown> = {};
|
|
160
|
+
if (fixtures) {
|
|
161
|
+
for (const [key, creator] of Object.entries(fixtures)) {
|
|
162
|
+
additionalFixtures[key] = async (
|
|
163
|
+
{ trx }: { trx: Transaction },
|
|
164
|
+
use: (value: unknown) => Promise<void>,
|
|
165
|
+
) => {
|
|
166
|
+
const value = await (creator as (trx: Transaction) => unknown)(trx);
|
|
167
|
+
await use(value);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
type CombinedFixtures = DatabaseFixtures<Transaction> & Extended;
|
|
173
|
+
|
|
174
|
+
// Cast to bypass Vitest's strict fixture typing which can't infer
|
|
175
|
+
// dynamically built fixture objects
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
|
+
const extendFn = this.api.extend as <T>(fixtures: any) => TestAPI<T>;
|
|
178
|
+
|
|
179
|
+
return extendFn<CombinedFixtures>({
|
|
151
180
|
// This fixture automatically provides a transaction to each test
|
|
152
181
|
trx: async ({}, use: (value: Transaction) => Promise<void>) => {
|
|
153
182
|
// Create a custom error class for rollback
|
|
@@ -159,9 +188,9 @@ export abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
|
|
|
159
188
|
}
|
|
160
189
|
|
|
161
190
|
let testError: Error | undefined;
|
|
162
|
-
const conn = await
|
|
191
|
+
const conn = await connection();
|
|
163
192
|
try {
|
|
164
|
-
await this.transact(conn,
|
|
193
|
+
await this.transact(conn, isolationLevel, async (transaction) => {
|
|
165
194
|
try {
|
|
166
195
|
// Provide the transaction to the test
|
|
167
196
|
await setup?.(transaction);
|
|
@@ -188,6 +217,7 @@ export abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
|
|
|
188
217
|
await this.destroy(conn);
|
|
189
218
|
}
|
|
190
219
|
},
|
|
220
|
+
...additionalFixtures,
|
|
191
221
|
});
|
|
192
222
|
}
|
|
193
223
|
}
|
|
@@ -195,6 +225,24 @@ export abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {
|
|
|
195
225
|
export type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;
|
|
196
226
|
export type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;
|
|
197
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Options for wrapping Vitest tests with database transaction isolation.
|
|
230
|
+
*/
|
|
231
|
+
export interface TransactionWrapperOptions<
|
|
232
|
+
TConn,
|
|
233
|
+
Transaction,
|
|
234
|
+
Extended extends Record<string, unknown> = {},
|
|
235
|
+
> {
|
|
236
|
+
/** Function that creates or returns a database connection */
|
|
237
|
+
connection: DatabaseConnection<TConn>;
|
|
238
|
+
/** Optional setup function to run within the transaction before each test */
|
|
239
|
+
setup?: (trx: Transaction) => Promise<void>;
|
|
240
|
+
/** Transaction isolation level (defaults to REPEATABLE_READ) */
|
|
241
|
+
isolationLevel?: IsolationLevel;
|
|
242
|
+
/** Additional fixtures that depend on the transaction */
|
|
243
|
+
fixtures?: FixtureCreators<Transaction, Extended>;
|
|
244
|
+
}
|
|
245
|
+
|
|
198
246
|
/**
|
|
199
247
|
* Type for fixture creator functions that depend on the transaction.
|
|
200
248
|
* Each function receives the transaction and returns the fixture value.
|
|
@@ -208,6 +256,36 @@ export type FixtureCreators<
|
|
|
208
256
|
) => Extended[K] | Promise<Extended[K]>;
|
|
209
257
|
};
|
|
210
258
|
|
|
259
|
+
/**
|
|
260
|
+
* The test API returned by extendWithFixtures.
|
|
261
|
+
* Provides access to both the transaction (trx) and all extended fixtures.
|
|
262
|
+
*
|
|
263
|
+
* @template Transaction - The transaction type
|
|
264
|
+
* @template Extended - The type of additional fixtures provided
|
|
265
|
+
* @template BaseTest - The base wrapped test type
|
|
266
|
+
*/
|
|
267
|
+
export type TestWithExtendedFixtures<
|
|
268
|
+
Transaction,
|
|
269
|
+
Extended extends Record<string, unknown>,
|
|
270
|
+
BaseTest extends ReturnType<TestAPI['extend']> = ReturnType<
|
|
271
|
+
TestAPI['extend']
|
|
272
|
+
>,
|
|
273
|
+
> = BaseTest & {
|
|
274
|
+
<C extends object>(
|
|
275
|
+
name: string,
|
|
276
|
+
fn: (
|
|
277
|
+
context: DatabaseFixtures<Transaction> & Extended & C,
|
|
278
|
+
) => Promise<void>,
|
|
279
|
+
): void;
|
|
280
|
+
<C extends object>(
|
|
281
|
+
name: string,
|
|
282
|
+
options: object,
|
|
283
|
+
fn: (
|
|
284
|
+
context: DatabaseFixtures<Transaction> & Extended & C,
|
|
285
|
+
) => Promise<void>,
|
|
286
|
+
): void;
|
|
287
|
+
};
|
|
288
|
+
|
|
211
289
|
/**
|
|
212
290
|
* Extends a wrapped test API with additional fixtures that depend on the transaction.
|
|
213
291
|
* This allows composing test context with factories, repositories, or other helpers.
|
|
@@ -223,7 +301,10 @@ export type FixtureCreators<
|
|
|
223
301
|
* import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';
|
|
224
302
|
*
|
|
225
303
|
* // Create base wrapped test
|
|
226
|
-
* const baseTest = wrapVitestKyselyTransaction(test,
|
|
304
|
+
* const baseTest = wrapVitestKyselyTransaction(test, {
|
|
305
|
+
* connection: db,
|
|
306
|
+
* setup: createTestTables,
|
|
307
|
+
* });
|
|
227
308
|
*
|
|
228
309
|
* // Extend with fixtures
|
|
229
310
|
* const it = extendWithFixtures(baseTest, {
|
|
@@ -246,21 +327,7 @@ export function extendWithFixtures<
|
|
|
246
327
|
>(
|
|
247
328
|
wrappedTest: T,
|
|
248
329
|
fixtures: FixtureCreators<Transaction, Extended>,
|
|
249
|
-
): T
|
|
250
|
-
<C extends object>(
|
|
251
|
-
name: string,
|
|
252
|
-
fn: (
|
|
253
|
-
context: DatabaseFixtures<Transaction> & Extended & C,
|
|
254
|
-
) => Promise<void>,
|
|
255
|
-
): void;
|
|
256
|
-
<C extends object>(
|
|
257
|
-
name: string,
|
|
258
|
-
options: object,
|
|
259
|
-
fn: (
|
|
260
|
-
context: DatabaseFixtures<Transaction> & Extended & C,
|
|
261
|
-
) => Promise<void>,
|
|
262
|
-
): void;
|
|
263
|
-
} {
|
|
330
|
+
): TestWithExtendedFixtures<Transaction, Extended, T> {
|
|
264
331
|
// Build fixture definitions for Vitest's extend API
|
|
265
332
|
const fixtureDefinitions: Record<string, any> = {};
|
|
266
333
|
|