@geekmidas/testkit 0.0.9 → 0.0.11
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/dist/Factory-B9LP1L18.d.cts +131 -0
- package/dist/Factory-DiZSNxC0.d.mts +131 -0
- package/dist/Factory.d.cts +3 -0
- package/dist/Factory.d.mts +3 -0
- package/dist/KyselyFactory-B-zlLbov.d.mts +200 -0
- package/dist/KyselyFactory-CrLWxJW4.d.cts +200 -0
- package/dist/KyselyFactory.d.cts +4 -0
- package/dist/KyselyFactory.d.mts +4 -0
- package/dist/ObjectionFactory-CJCpvwts.d.mts +213 -0
- package/dist/{ObjectionFactory-C47B03Ot.cjs → ObjectionFactory-Wq80ypMM.cjs} +82 -40
- package/dist/{ObjectionFactory-89p-FFEw.mjs → ObjectionFactory-aqM0dDW7.mjs} +82 -40
- package/dist/ObjectionFactory-vME-wNlq.d.cts +213 -0
- package/dist/ObjectionFactory.cjs +2 -1
- package/dist/ObjectionFactory.d.cts +4 -0
- package/dist/ObjectionFactory.d.mts +4 -0
- package/dist/ObjectionFactory.mjs +2 -1
- package/dist/{PostgresKyselyMigrator-ChIpZFYB.mjs → PostgresKyselyMigrator-Bdhl251C.mjs} +1 -1
- package/dist/PostgresKyselyMigrator-CQ3aUoy_.d.cts +67 -0
- package/dist/{PostgresKyselyMigrator-Bs31emFd.cjs → PostgresKyselyMigrator-CfytARcA.cjs} +1 -1
- package/dist/PostgresKyselyMigrator-_6yHZigp.d.mts +67 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.d.cts +3 -0
- package/dist/PostgresKyselyMigrator.d.mts +3 -0
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-BlvuQl7d.d.mts +84 -0
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +84 -0
- package/dist/{PostgresMigrator-BtAWdLss.cjs → PostgresMigrator-DFcNdCvD.cjs} +2 -1
- package/dist/{PostgresMigrator-BzqksJcW.mjs → PostgresMigrator-DxPC_gGu.mjs} +3 -2
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.d.cts +2 -0
- package/dist/PostgresMigrator.d.mts +2 -0
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/{PostgresObjectionMigrator-B88aTT0m.cjs → PostgresObjectionMigrator-BG6ymgnt.cjs} +1 -1
- package/dist/PostgresObjectionMigrator-C69n7vzr.d.mts +77 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +77 -0
- package/dist/{PostgresObjectionMigrator-DydSgYFv.mjs → PostgresObjectionMigrator-G4h5FLvU.mjs} +1 -1
- package/dist/PostgresObjectionMigrator.cjs +2 -2
- package/dist/PostgresObjectionMigrator.d.cts +3 -0
- package/dist/PostgresObjectionMigrator.d.mts +3 -0
- package/dist/PostgresObjectionMigrator.mjs +2 -2
- package/dist/VitestKyselyTransactionIsolator-ClCazkBO.d.mts +56 -0
- package/dist/VitestKyselyTransactionIsolator-UE1J-UoP.d.cts +56 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +3 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +3 -0
- package/dist/VitestObjectionTransactionIsolator-CO2nTi9r.d.cts +57 -0
- package/dist/VitestObjectionTransactionIsolator-D264iuPy.d.mts +57 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +3 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +3 -0
- package/dist/VitestTransactionIsolator-DHf2MxmC.d.cts +118 -0
- package/dist/VitestTransactionIsolator-Xqyjlmw6.d.mts +118 -0
- package/dist/VitestTransactionIsolator.d.cts +2 -0
- package/dist/VitestTransactionIsolator.d.mts +2 -0
- package/dist/__tests__/Factory.spec.d.cts +1 -0
- package/dist/__tests__/Factory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +2 -2
- package/dist/__tests__/KyselyFactory.spec.d.cts +1 -0
- package/dist/__tests__/KyselyFactory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.mjs +2 -2
- package/dist/__tests__/ObjectionFactory.spec.cjs +117 -1
- package/dist/__tests__/ObjectionFactory.spec.d.cts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.d.mts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +117 -1
- package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +2 -2
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +2 -2
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +4 -3
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +4 -3
- package/dist/__tests__/faker.spec.d.cts +1 -0
- package/dist/__tests__/faker.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.cjs +2 -2
- package/dist/__tests__/integration.spec.d.cts +1 -0
- package/dist/__tests__/integration.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.mjs +2 -2
- package/dist/example.d.cts +26 -0
- package/dist/example.d.mts +26 -0
- package/dist/faker-DAiFK3T3.d.cts +155 -0
- package/dist/faker-nN9Ki6fn.d.mts +155 -0
- package/dist/faker.d.cts +2 -0
- package/dist/faker.d.mts +2 -0
- package/dist/helpers.d.cts +41 -0
- package/dist/helpers.d.mts +41 -0
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +73 -0
- package/dist/kysely.d.mts +73 -0
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +4 -3
- package/dist/objection.d.cts +88 -0
- package/dist/objection.d.mts +88 -0
- package/dist/objection.mjs +4 -3
- package/package.json +5 -5
- package/src/ObjectionFactory.ts +156 -13
- package/src/PostgresMigrator.ts +3 -1
- package/src/__tests__/ObjectionFactory.spec.ts +162 -0
- package/PostgresKyselyMigrator.spec +0 -471
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import * as _faker_js_faker4 from "@faker-js/faker";
|
|
2
|
+
|
|
3
|
+
//#region src/faker.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates random timestamp fields for database records.
|
|
7
|
+
* Creates a createdAt date in the past and an updatedAt date between creation and now.
|
|
8
|
+
* Milliseconds are set to 0 for cleaner database storage.
|
|
9
|
+
*
|
|
10
|
+
* @returns Object with createdAt and updatedAt Date fields
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const { createdAt, updatedAt } = timestamps();
|
|
15
|
+
* console.log(createdAt); // 2023-05-15T10:30:00.000Z
|
|
16
|
+
* console.log(updatedAt); // 2023-11-20T14:45:00.000Z
|
|
17
|
+
*
|
|
18
|
+
* // Use in factory
|
|
19
|
+
* const user = {
|
|
20
|
+
* name: 'John Doe',
|
|
21
|
+
* ...timestamps()
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function timestamps(): Timestamps;
|
|
26
|
+
/**
|
|
27
|
+
* Generates a reverse domain name identifier.
|
|
28
|
+
* Useful for creating unique identifiers that follow domain naming conventions.
|
|
29
|
+
*
|
|
30
|
+
* @param suffix - Optional suffix to append to the identifier
|
|
31
|
+
* @returns A reverse domain name string (e.g., "com.example.feature123")
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* console.log(identifier()); // "com.example.widget1"
|
|
36
|
+
* console.log(identifier('user')); // "org.acme.user"
|
|
37
|
+
* console.log(identifier('api')); // "net.demo.api"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function identifier(suffix?: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Generates sequential numbers for a named sequence.
|
|
43
|
+
* Useful for creating unique IDs or numbered test data.
|
|
44
|
+
* Each named sequence maintains its own counter.
|
|
45
|
+
*
|
|
46
|
+
* @param name - The sequence name (default: 'default')
|
|
47
|
+
* @returns The next number in the sequence
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* console.log(sequence()); // 1
|
|
52
|
+
* console.log(sequence()); // 2
|
|
53
|
+
* console.log(sequence('user')); // 1
|
|
54
|
+
* console.log(sequence('user')); // 2
|
|
55
|
+
* console.log(sequence()); // 3
|
|
56
|
+
*
|
|
57
|
+
* // Use in factories
|
|
58
|
+
* const email = `user${sequence('email')}@example.com`;
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function sequence(name?: string): number;
|
|
62
|
+
/**
|
|
63
|
+
* Resets a named sequence counter to a specific value.
|
|
64
|
+
* Useful for resetting sequences between test suites.
|
|
65
|
+
*
|
|
66
|
+
* @param name - The sequence name to reset (default: 'default')
|
|
67
|
+
* @param value - The new starting value (default: 0)
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* sequence('user'); // 1
|
|
72
|
+
* sequence('user'); // 2
|
|
73
|
+
* resetSequence('user');
|
|
74
|
+
* sequence('user'); // 1
|
|
75
|
+
*
|
|
76
|
+
* resetSequence('order', 1000);
|
|
77
|
+
* sequence('order'); // 1001
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
declare function resetSequence(name?: string, value?: number): void;
|
|
81
|
+
/**
|
|
82
|
+
* Resets all sequence counters.
|
|
83
|
+
* Useful for cleaning up between test suites to ensure predictable sequences.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // In test setup
|
|
88
|
+
* beforeEach(() => {
|
|
89
|
+
* resetAllSequences();
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* it('starts sequences from 1', () => {
|
|
93
|
+
* expect(sequence()).toBe(1);
|
|
94
|
+
* expect(sequence('user')).toBe(1);
|
|
95
|
+
* });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function resetAllSequences(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Generates a random price as a number.
|
|
101
|
+
* Converts faker's string price to a numeric value.
|
|
102
|
+
*
|
|
103
|
+
* @returns A random price number
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const productPrice = price(); // 29.99
|
|
108
|
+
* const total = price() * quantity; // Numeric calculation
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function price(): number;
|
|
112
|
+
/**
|
|
113
|
+
* Enhanced faker instance with additional utility methods for testing.
|
|
114
|
+
* Extends @faker-js/faker with custom methods for common test data generation patterns.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { faker } from '@geekmidas/testkit';
|
|
119
|
+
*
|
|
120
|
+
* // Use standard faker methods
|
|
121
|
+
* const name = faker.person.fullName();
|
|
122
|
+
* const email = faker.internet.email();
|
|
123
|
+
*
|
|
124
|
+
* // Use custom extensions
|
|
125
|
+
* const { createdAt, updatedAt } = faker.timestamps();
|
|
126
|
+
* const id = faker.identifier('user');
|
|
127
|
+
* const orderNumber = faker.sequence('order');
|
|
128
|
+
* const productPrice = faker.price();
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
declare const faker$1: Readonly<_faker_js_faker4.Faker & {
|
|
132
|
+
timestamps: typeof timestamps;
|
|
133
|
+
identifier: typeof identifier;
|
|
134
|
+
sequence: typeof sequence;
|
|
135
|
+
resetSequence: typeof resetSequence;
|
|
136
|
+
resetAllSequences: typeof resetAllSequences;
|
|
137
|
+
price: typeof price;
|
|
138
|
+
}>;
|
|
139
|
+
/**
|
|
140
|
+
* Type definition for timestamp fields.
|
|
141
|
+
* Used by the timestamps() function to generate date fields.
|
|
142
|
+
*/
|
|
143
|
+
type Timestamps = {
|
|
144
|
+
/** The creation date */
|
|
145
|
+
createdAt: Date;
|
|
146
|
+
/** The last update date */
|
|
147
|
+
updatedAt: Date;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Type definition for the enhanced faker factory.
|
|
151
|
+
* Includes all standard faker methods plus custom extensions.
|
|
152
|
+
*/
|
|
153
|
+
type FakerFactory = typeof faker$1;
|
|
154
|
+
//#endregion
|
|
155
|
+
export { FakerFactory, Timestamps, faker$1 as faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
package/dist/faker.d.cts
ADDED
package/dist/faker.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Kysely database instance with PostgreSQL dialect and camelCase plugin.
|
|
7
|
+
* This is a convenience function for quickly setting up a Kysely connection for testing.
|
|
8
|
+
*
|
|
9
|
+
* @template Database - The database schema type
|
|
10
|
+
* @param config - PostgreSQL connection configuration (pg.Pool config)
|
|
11
|
+
* @returns A configured Kysely instance
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* interface Database {
|
|
16
|
+
* users: UsersTable;
|
|
17
|
+
* posts: PostsTable;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Create from connection string
|
|
21
|
+
* const db = createKyselyDb<Database>({
|
|
22
|
+
* connectionString: 'postgresql://user:pass@localhost:5432/testdb'
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Create with detailed config
|
|
26
|
+
* const db = createKyselyDb<Database>({
|
|
27
|
+
* host: 'localhost',
|
|
28
|
+
* port: 5432,
|
|
29
|
+
* database: 'testdb',
|
|
30
|
+
* user: 'testuser',
|
|
31
|
+
* password: 'testpass',
|
|
32
|
+
* max: 10 // connection pool size
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Use in tests
|
|
36
|
+
* const users = await db.selectFrom('users').selectAll().execute();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function createKyselyDb<Database>(config: any): Kysely<Database>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { createKyselyDb };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Kysely database instance with PostgreSQL dialect and camelCase plugin.
|
|
7
|
+
* This is a convenience function for quickly setting up a Kysely connection for testing.
|
|
8
|
+
*
|
|
9
|
+
* @template Database - The database schema type
|
|
10
|
+
* @param config - PostgreSQL connection configuration (pg.Pool config)
|
|
11
|
+
* @returns A configured Kysely instance
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* interface Database {
|
|
16
|
+
* users: UsersTable;
|
|
17
|
+
* posts: PostsTable;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Create from connection string
|
|
21
|
+
* const db = createKyselyDb<Database>({
|
|
22
|
+
* connectionString: 'postgresql://user:pass@localhost:5432/testdb'
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Create with detailed config
|
|
26
|
+
* const db = createKyselyDb<Database>({
|
|
27
|
+
* host: 'localhost',
|
|
28
|
+
* port: 5432,
|
|
29
|
+
* database: 'testdb',
|
|
30
|
+
* user: 'testuser',
|
|
31
|
+
* password: 'testpass',
|
|
32
|
+
* max: 10 // connection pool size
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Use in tests
|
|
36
|
+
* const users = await db.selectFrom('users').selectAll().execute();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function createKyselyDb<Database>(config: any): Kysely<Database>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { createKyselyDb };
|
package/dist/kysely.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
2
|
require('./faker-SMN4ira4.cjs');
|
|
3
3
|
const require_KyselyFactory = require('./KyselyFactory-Bdq1s1Go.cjs');
|
|
4
|
-
require('./PostgresMigrator-
|
|
5
|
-
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-
|
|
4
|
+
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
5
|
+
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-CfytARcA.cjs');
|
|
6
6
|
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CruLTRRi.cjs');
|
|
7
7
|
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-CIlpIO78.cjs');
|
|
8
8
|
const require_kysely = require('./kysely-CBfCXxUn.cjs');
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "./faker-DAiFK3T3.cjs";
|
|
2
|
+
import "./Factory-B9LP1L18.cjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-CrLWxJW4.cjs";
|
|
4
|
+
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-CQ3aUoy_.cjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
|
7
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-UE1J-UoP.cjs";
|
|
8
|
+
import { Kysely, Transaction } from "kysely";
|
|
9
|
+
import { TestAPI } from "vitest";
|
|
10
|
+
|
|
11
|
+
//#region src/kysely.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @template Database - The database schema type
|
|
19
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
20
|
+
* @param db - The Kysely database instance
|
|
21
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
22
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
23
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { test } from 'vitest';
|
|
28
|
+
* import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';
|
|
29
|
+
* import { db } from './database';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, db);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await trx
|
|
37
|
+
* .insertInto('users')
|
|
38
|
+
* .values({ name: 'Test User', email: 'test@example.com' })
|
|
39
|
+
* .returningAll()
|
|
40
|
+
* .executeTakeFirst();
|
|
41
|
+
*
|
|
42
|
+
* expect(user).toBeDefined();
|
|
43
|
+
* // User is automatically rolled back after test
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // With setup function for common test data
|
|
47
|
+
* const testWithSetup = wrapVitestKyselyTransaction(
|
|
48
|
+
* test,
|
|
49
|
+
* db,
|
|
50
|
+
* async (trx) => {
|
|
51
|
+
* // Create common test data
|
|
52
|
+
* await trx.insertInto('settings')
|
|
53
|
+
* .values({ key: 'test_mode', value: 'true' })
|
|
54
|
+
* .execute();
|
|
55
|
+
* }
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
59
|
+
* const setting = await trx
|
|
60
|
+
* .selectFrom('settings')
|
|
61
|
+
* .where('key', '=', 'test_mode')
|
|
62
|
+
* .selectAll()
|
|
63
|
+
* .executeTakeFirst();
|
|
64
|
+
*
|
|
65
|
+
* expect(setting?.value).toBe('true');
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function wrapVitestKyselyTransaction<Database>(api: TestAPI, db: Kysely<Database>, setup?: (trx: Transaction<Database>) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
70
|
+
trx: Transaction<Database>;
|
|
71
|
+
}>;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { IsolationLevel, KyselyFactory, PostgresKyselyMigrator, VitestKyselyTransactionIsolator, wrapVitestKyselyTransaction };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "./faker-nN9Ki6fn.mjs";
|
|
2
|
+
import "./Factory-DiZSNxC0.mjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-B-zlLbov.mjs";
|
|
4
|
+
import "./PostgresMigrator-BlvuQl7d.mjs";
|
|
5
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-_6yHZigp.mjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
|
7
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-ClCazkBO.mjs";
|
|
8
|
+
import { Kysely, Transaction } from "kysely";
|
|
9
|
+
import { TestAPI } from "vitest";
|
|
10
|
+
|
|
11
|
+
//#region src/kysely.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @template Database - The database schema type
|
|
19
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
20
|
+
* @param db - The Kysely database instance
|
|
21
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
22
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
23
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { test } from 'vitest';
|
|
28
|
+
* import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';
|
|
29
|
+
* import { db } from './database';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, db);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await trx
|
|
37
|
+
* .insertInto('users')
|
|
38
|
+
* .values({ name: 'Test User', email: 'test@example.com' })
|
|
39
|
+
* .returningAll()
|
|
40
|
+
* .executeTakeFirst();
|
|
41
|
+
*
|
|
42
|
+
* expect(user).toBeDefined();
|
|
43
|
+
* // User is automatically rolled back after test
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // With setup function for common test data
|
|
47
|
+
* const testWithSetup = wrapVitestKyselyTransaction(
|
|
48
|
+
* test,
|
|
49
|
+
* db,
|
|
50
|
+
* async (trx) => {
|
|
51
|
+
* // Create common test data
|
|
52
|
+
* await trx.insertInto('settings')
|
|
53
|
+
* .values({ key: 'test_mode', value: 'true' })
|
|
54
|
+
* .execute();
|
|
55
|
+
* }
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
59
|
+
* const setting = await trx
|
|
60
|
+
* .selectFrom('settings')
|
|
61
|
+
* .where('key', '=', 'test_mode')
|
|
62
|
+
* .selectAll()
|
|
63
|
+
* .executeTakeFirst();
|
|
64
|
+
*
|
|
65
|
+
* expect(setting?.value).toBe('true');
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function wrapVitestKyselyTransaction<Database>(api: TestAPI, db: Kysely<Database>, setup?: (trx: Transaction<Database>) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
70
|
+
trx: Transaction<Database>;
|
|
71
|
+
}>;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { IsolationLevel, KyselyFactory, PostgresKyselyMigrator, VitestKyselyTransactionIsolator, wrapVitestKyselyTransaction };
|
package/dist/kysely.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import "./Factory-z2m01hMj.mjs";
|
|
2
2
|
import "./faker-CxKkEeYi.mjs";
|
|
3
3
|
import { KyselyFactory } from "./KyselyFactory-ELiHgHVv.mjs";
|
|
4
|
-
import "./PostgresMigrator-
|
|
5
|
-
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-
|
|
4
|
+
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
5
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-Bdhl251C.mjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-BWwK-ca6.mjs";
|
|
7
7
|
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-BKGT9nEG.mjs";
|
|
8
8
|
import { wrapVitestKyselyTransaction } from "./kysely-Cx_1pZYc.mjs";
|
package/dist/objection.cjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
|
-
|
|
3
|
-
require('./
|
|
4
|
-
|
|
2
|
+
require('./faker-SMN4ira4.cjs');
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-Wq80ypMM.cjs');
|
|
4
|
+
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
5
|
+
const require_PostgresObjectionMigrator = require('./PostgresObjectionMigrator-BG6ymgnt.cjs');
|
|
5
6
|
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CruLTRRi.cjs');
|
|
6
7
|
const require_VitestObjectionTransactionIsolator = require('./VitestObjectionTransactionIsolator-DyqLp_in.cjs');
|
|
7
8
|
const require_objection = require('./objection-CCD8fMLj.cjs');
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "./faker-DAiFK3T3.cjs";
|
|
2
|
+
import "./Factory-B9LP1L18.cjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-vME-wNlq.cjs";
|
|
4
|
+
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
|
+
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-CZHHcCOv.cjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-CO2nTi9r.cjs";
|
|
8
|
+
import { Knex } from "knex";
|
|
9
|
+
import { TestAPI } from "vitest";
|
|
10
|
+
|
|
11
|
+
//#region src/objection.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
19
|
+
* @param knex - The Knex database connection instance
|
|
20
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
21
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
22
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { test } from 'vitest';
|
|
27
|
+
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
28
|
+
* import { knex } from './database';
|
|
29
|
+
* import { User, Post } from './models';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await User.query(trx)
|
|
37
|
+
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
38
|
+
*
|
|
39
|
+
* expect(user).toBeDefined();
|
|
40
|
+
* // User is automatically rolled back after test
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // With setup function for common test data
|
|
44
|
+
* const testWithSetup = wrapVitestObjectionTransaction(
|
|
45
|
+
* test,
|
|
46
|
+
* knex,
|
|
47
|
+
* async (trx) => {
|
|
48
|
+
* // Create common test data
|
|
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');
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* // Example with factory and transaction
|
|
65
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
66
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
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);
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function wrapVitestObjectionTransaction(api: TestAPI, knex: Knex, setup?: (trx: Knex.Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
85
|
+
trx: Knex.Transaction<any, any[]>;
|
|
86
|
+
}>;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "./faker-nN9Ki6fn.mjs";
|
|
2
|
+
import "./Factory-DiZSNxC0.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-CJCpvwts.mjs";
|
|
4
|
+
import "./PostgresMigrator-BlvuQl7d.mjs";
|
|
5
|
+
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-C69n7vzr.mjs";
|
|
6
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
|
7
|
+
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-D264iuPy.mjs";
|
|
8
|
+
import { TestAPI } from "vitest";
|
|
9
|
+
import { Knex } from "knex";
|
|
10
|
+
|
|
11
|
+
//#region src/objection.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Objection.js.
|
|
15
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
16
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
17
|
+
*
|
|
18
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
19
|
+
* @param knex - The Knex database connection instance
|
|
20
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
21
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
22
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { test } from 'vitest';
|
|
27
|
+
* import { wrapVitestObjectionTransaction } from '@geekmidas/testkit/objection';
|
|
28
|
+
* import { knex } from './database';
|
|
29
|
+
* import { User, Post } from './models';
|
|
30
|
+
*
|
|
31
|
+
* // Create isolated test with automatic rollback
|
|
32
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
33
|
+
*
|
|
34
|
+
* // Use in tests - each test gets its own transaction
|
|
35
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
36
|
+
* const user = await User.query(trx)
|
|
37
|
+
* .insert({ name: 'Test User', email: 'test@example.com' });
|
|
38
|
+
*
|
|
39
|
+
* expect(user).toBeDefined();
|
|
40
|
+
* // User is automatically rolled back after test
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // With setup function for common test data
|
|
44
|
+
* const testWithSetup = wrapVitestObjectionTransaction(
|
|
45
|
+
* test,
|
|
46
|
+
* knex,
|
|
47
|
+
* async (trx) => {
|
|
48
|
+
* // Create common test data
|
|
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');
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* // Example with factory and transaction
|
|
65
|
+
* const isolatedTest = wrapVitestObjectionTransaction(test, knex);
|
|
66
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
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);
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function wrapVitestObjectionTransaction(api: TestAPI, knex: Knex, setup?: (trx: Knex.Transaction) => Promise<void>, level?: IsolationLevel): TestAPI<{
|
|
85
|
+
trx: Knex.Transaction<any, any[]>;
|
|
86
|
+
}>;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { IsolationLevel, ObjectionFactory, PostgresObjectionMigrator, VitestObjectionTransactionIsolator, wrapVitestObjectionTransaction };
|
package/dist/objection.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import "./Factory-z2m01hMj.mjs";
|
|
2
|
-
import
|
|
3
|
-
import "./
|
|
4
|
-
import
|
|
2
|
+
import "./faker-CxKkEeYi.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-aqM0dDW7.mjs";
|
|
4
|
+
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
5
|
+
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-G4h5FLvU.mjs";
|
|
5
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-BWwK-ca6.mjs";
|
|
6
7
|
import { VitestObjectionTransactionIsolator } from "./VitestObjectionTransactionIsolator-BPoLUFop.mjs";
|
|
7
8
|
import { wrapVitestObjectionTransaction } from "./objection-lsMgM5gP.mjs";
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./objection": {
|
|
8
|
-
"types": "./
|
|
8
|
+
"types": "./dist/objection.d.ts",
|
|
9
9
|
"import": "./dist/objection.mjs",
|
|
10
10
|
"require": "./dist/objection.cjs"
|
|
11
11
|
},
|
|
12
12
|
"./kysely": {
|
|
13
|
-
"types": "./
|
|
13
|
+
"types": "./dist/kysely.ts",
|
|
14
14
|
"import": "./dist/kysely.mjs",
|
|
15
15
|
"require": "./dist/kysely.cjs"
|
|
16
16
|
},
|
|
17
17
|
"./faker": {
|
|
18
|
-
"types": "./
|
|
18
|
+
"types": "./dist/faker.d.ts",
|
|
19
19
|
"import": "./dist/faker.mjs",
|
|
20
20
|
"require": "./dist/faker.cjs"
|
|
21
21
|
}
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"objection": "~3.1.5",
|
|
38
38
|
"db-errors": "~0.2.3",
|
|
39
39
|
"vitest": "~3.2.4",
|
|
40
|
-
"@geekmidas/envkit": "0.0.
|
|
40
|
+
"@geekmidas/envkit": "0.0.5"
|
|
41
41
|
}
|
|
42
42
|
}
|