@geekmidas/testkit 0.0.6 → 0.0.8
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-WMhTNZ9S.cjs +56 -0
- package/dist/Factory-z2m01hMj.mjs +50 -0
- package/dist/Factory.cjs +1 -1
- package/dist/Factory.mjs +1 -1
- package/dist/KyselyFactory-Bdq1s1Go.cjs +215 -0
- package/dist/KyselyFactory-ELiHgHVv.mjs +210 -0
- package/dist/KyselyFactory.cjs +3 -3
- package/dist/KyselyFactory.mjs +3 -3
- package/dist/ObjectionFactory-89p-FFEw.mjs +178 -0
- package/dist/ObjectionFactory-C47B03Ot.cjs +183 -0
- package/dist/ObjectionFactory.cjs +2 -2
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/PostgresKyselyMigrator-Bs31emFd.cjs +87 -0
- package/dist/PostgresKyselyMigrator-ChIpZFYB.mjs +81 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-BtAWdLss.cjs +151 -0
- package/dist/PostgresMigrator-BzqksJcW.mjs +145 -0
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-AfxPJEwR.mjs +58 -0
- package/dist/VitestKyselyTransactionIsolator-YWnSJiIH.cjs +63 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/VitestObjectionTransactionIsolator-0uX6DW5G.cjs +66 -0
- package/dist/VitestObjectionTransactionIsolator-BZRYy8iW.mjs +61 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +4 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +4 -0
- package/dist/VitestTransactionIsolator-DcOz0LZF.cjs +129 -0
- package/dist/VitestTransactionIsolator-kFL36T8x.mjs +117 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/__tests__/Factory.spec.cjs +1 -1
- package/dist/__tests__/Factory.spec.mjs +1 -1
- package/dist/__tests__/KyselyFactory.spec.cjs +10 -10
- package/dist/__tests__/KyselyFactory.spec.mjs +10 -10
- package/dist/__tests__/ObjectionFactory.spec.cjs +3 -3
- package/dist/__tests__/ObjectionFactory.spec.mjs +3 -3
- package/dist/__tests__/PostgresMigrator.spec.cjs +2 -2
- package/dist/__tests__/PostgresMigrator.spec.mjs +2 -2
- package/dist/__tests__/faker.spec.cjs +1 -1
- package/dist/__tests__/faker.spec.mjs +1 -1
- package/dist/__tests__/integration.spec.cjs +10 -10
- package/dist/__tests__/integration.spec.mjs +10 -10
- package/dist/example.cjs +3 -3
- package/dist/example.mjs +3 -3
- package/dist/faker-CxKkEeYi.mjs +227 -0
- package/dist/faker-SMN4ira4.cjs +263 -0
- package/dist/faker.cjs +1 -1
- package/dist/faker.mjs +1 -1
- package/dist/helpers-CKMlwSYT.mjs +47 -0
- package/dist/helpers-H4hO5SZR.cjs +53 -0
- package/dist/helpers.cjs +1 -1
- package/dist/helpers.mjs +1 -1
- package/dist/kysely-B-GOhABm.cjs +72 -0
- package/dist/kysely-CqfoKVXs.mjs +67 -0
- package/dist/kysely.cjs +10 -8
- package/dist/kysely.mjs +9 -9
- package/dist/objection.cjs +86 -3
- package/dist/objection.mjs +83 -3
- package/package.json +2 -2
- package/src/Factory.ts +97 -0
- package/src/KyselyFactory.ts +180 -0
- package/src/ObjectionFactory.ts +145 -3
- package/src/PostgresKyselyMigrator.ts +54 -0
- package/src/PostgresMigrator.ts +90 -0
- package/src/VitestKyselyTransactionIsolator.ts +46 -0
- package/src/VitestObjectionTransactionIsolator.ts +74 -0
- package/src/VitestTransactionIsolator.ts +95 -0
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +144 -0
- package/src/faker.ts +158 -7
- package/src/helpers.ts +34 -0
- package/src/kysely.ts +63 -0
- package/src/objection.ts +95 -0
- package/test/helpers.ts +3 -1
- package/dist/Factory-DREHoms3.cjs +0 -15
- package/dist/Factory-DlzMkMzb.mjs +0 -9
- package/dist/KyselyFactory-BX7Kv2uP.cjs +0 -65
- package/dist/KyselyFactory-pOMOFQWE.mjs +0 -60
- package/dist/ObjectionFactory-BlkzSEqo.cjs +0 -41
- package/dist/ObjectionFactory-ChuX8sZN.mjs +0 -36
- package/dist/PostgresKyselyMigrator-D8fm35-s.mjs +0 -27
- package/dist/PostgresKyselyMigrator-JTY2LfwD.cjs +0 -33
- package/dist/PostgresMigrator-Bz-tnjB6.cjs +0 -67
- package/dist/PostgresMigrator-CEoRKTdq.mjs +0 -61
- package/dist/VitestKyselyTransactionIsolator-D-qpeVKO.mjs +0 -12
- package/dist/VitestKyselyTransactionIsolator-jF6Ohyu_.cjs +0 -17
- package/dist/VitestTransactionIsolator-BK9UsrKt.cjs +0 -53
- package/dist/VitestTransactionIsolator-e-R3p_X8.mjs +0 -41
- package/dist/faker-BwaXA_RF.mjs +0 -85
- package/dist/faker-caz-8zt8.cjs +0 -121
- package/dist/helpers-DN4sJO4i.mjs +0 -13
- package/dist/helpers-DOtYCEvZ.cjs +0 -19
- package/dist/kysely-C1-aHdnU.mjs +0 -11
- package/dist/kysely-DL3C2eM4.cjs +0 -16
- /package/dist/{helpers-B2CfbaTC.cjs → helpers-Bnm3Jy9X.cjs} +0 -0
- /package/dist/{helpers-Rf5F71r9.mjs → helpers-CukcFAU9.mjs} +0 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { faker } from "@faker-js/faker";
|
|
2
|
+
|
|
3
|
+
//#region src/faker.ts
|
|
4
|
+
/**
|
|
5
|
+
* Atomic counter implementation for thread-safe sequence generation.
|
|
6
|
+
* Provides a clean abstraction for generating sequential numbers in tests.
|
|
7
|
+
* While JavaScript is single-threaded, this class makes the intent explicit.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const counter = new AtomicCounter(100);
|
|
12
|
+
* console.log(counter.increment()); // 101
|
|
13
|
+
* console.log(counter.increment()); // 102
|
|
14
|
+
* console.log(counter.get()); // 102
|
|
15
|
+
* counter.reset(200);
|
|
16
|
+
* console.log(counter.increment()); // 201
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
var AtomicCounter = class {
|
|
20
|
+
/**
|
|
21
|
+
* The current counter value.
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
value;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new atomic counter.
|
|
27
|
+
* @param initialValue - The starting value (default: 0)
|
|
28
|
+
*/
|
|
29
|
+
constructor(initialValue = 0) {
|
|
30
|
+
this.value = initialValue;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Increments the counter and returns the new value.
|
|
34
|
+
* @returns The incremented value
|
|
35
|
+
*/
|
|
36
|
+
increment() {
|
|
37
|
+
return ++this.value;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Gets the current counter value without incrementing.
|
|
41
|
+
* @returns The current value
|
|
42
|
+
*/
|
|
43
|
+
get() {
|
|
44
|
+
return this.value;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resets the counter to a specific value.
|
|
48
|
+
* @param value - The new value (default: 0)
|
|
49
|
+
*/
|
|
50
|
+
reset(value = 0) {
|
|
51
|
+
this.value = value;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Generates random timestamp fields for database records.
|
|
56
|
+
* Creates a createdAt date in the past and an updatedAt date between creation and now.
|
|
57
|
+
* Milliseconds are set to 0 for cleaner database storage.
|
|
58
|
+
*
|
|
59
|
+
* @returns Object with createdAt and updatedAt Date fields
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const { createdAt, updatedAt } = timestamps();
|
|
64
|
+
* console.log(createdAt); // 2023-05-15T10:30:00.000Z
|
|
65
|
+
* console.log(updatedAt); // 2023-11-20T14:45:00.000Z
|
|
66
|
+
*
|
|
67
|
+
* // Use in factory
|
|
68
|
+
* const user = {
|
|
69
|
+
* name: 'John Doe',
|
|
70
|
+
* ...timestamps()
|
|
71
|
+
* };
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
function timestamps() {
|
|
75
|
+
const createdAt = faker$1.date.past();
|
|
76
|
+
const updatedAt = faker$1.date.between({
|
|
77
|
+
from: createdAt,
|
|
78
|
+
to: /* @__PURE__ */ new Date()
|
|
79
|
+
});
|
|
80
|
+
createdAt.setMilliseconds(0);
|
|
81
|
+
updatedAt.setMilliseconds(0);
|
|
82
|
+
return {
|
|
83
|
+
createdAt,
|
|
84
|
+
updatedAt
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Generates a reverse domain name identifier.
|
|
89
|
+
* Useful for creating unique identifiers that follow domain naming conventions.
|
|
90
|
+
*
|
|
91
|
+
* @param suffix - Optional suffix to append to the identifier
|
|
92
|
+
* @returns A reverse domain name string (e.g., "com.example.feature123")
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* console.log(identifier()); // "com.example.widget1"
|
|
97
|
+
* console.log(identifier('user')); // "org.acme.user"
|
|
98
|
+
* console.log(identifier('api')); // "net.demo.api"
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function identifier(suffix) {
|
|
102
|
+
return [
|
|
103
|
+
faker$1.internet.domainSuffix(),
|
|
104
|
+
faker$1.internet.domainWord(),
|
|
105
|
+
suffix ? suffix : faker$1.internet.domainWord() + sequence("identifier")
|
|
106
|
+
].join(".");
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Storage for named sequence counters.
|
|
110
|
+
* Each sequence maintains its own independent counter.
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
114
|
+
/**
|
|
115
|
+
* Generates sequential numbers for a named sequence.
|
|
116
|
+
* Useful for creating unique IDs or numbered test data.
|
|
117
|
+
* Each named sequence maintains its own counter.
|
|
118
|
+
*
|
|
119
|
+
* @param name - The sequence name (default: 'default')
|
|
120
|
+
* @returns The next number in the sequence
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* console.log(sequence()); // 1
|
|
125
|
+
* console.log(sequence()); // 2
|
|
126
|
+
* console.log(sequence('user')); // 1
|
|
127
|
+
* console.log(sequence('user')); // 2
|
|
128
|
+
* console.log(sequence()); // 3
|
|
129
|
+
*
|
|
130
|
+
* // Use in factories
|
|
131
|
+
* const email = `user${sequence('email')}@example.com`;
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function sequence(name = "default") {
|
|
135
|
+
if (!sequences.has(name)) sequences.set(name, new AtomicCounter());
|
|
136
|
+
const counter = sequences.get(name);
|
|
137
|
+
return counter.increment();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Resets a named sequence counter to a specific value.
|
|
141
|
+
* Useful for resetting sequences between test suites.
|
|
142
|
+
*
|
|
143
|
+
* @param name - The sequence name to reset (default: 'default')
|
|
144
|
+
* @param value - The new starting value (default: 0)
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* sequence('user'); // 1
|
|
149
|
+
* sequence('user'); // 2
|
|
150
|
+
* resetSequence('user');
|
|
151
|
+
* sequence('user'); // 1
|
|
152
|
+
*
|
|
153
|
+
* resetSequence('order', 1000);
|
|
154
|
+
* sequence('order'); // 1001
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
function resetSequence(name = "default", value = 0) {
|
|
158
|
+
if (sequences.has(name)) {
|
|
159
|
+
const counter = sequences.get(name);
|
|
160
|
+
counter.reset(value);
|
|
161
|
+
} else sequences.set(name, new AtomicCounter(value));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Resets all sequence counters.
|
|
165
|
+
* Useful for cleaning up between test suites to ensure predictable sequences.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // In test setup
|
|
170
|
+
* beforeEach(() => {
|
|
171
|
+
* resetAllSequences();
|
|
172
|
+
* });
|
|
173
|
+
*
|
|
174
|
+
* it('starts sequences from 1', () => {
|
|
175
|
+
* expect(sequence()).toBe(1);
|
|
176
|
+
* expect(sequence('user')).toBe(1);
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
function resetAllSequences() {
|
|
181
|
+
sequences.clear();
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Generates a random price as a number.
|
|
185
|
+
* Converts faker's string price to a numeric value.
|
|
186
|
+
*
|
|
187
|
+
* @returns A random price number
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const productPrice = price(); // 29.99
|
|
192
|
+
* const total = price() * quantity; // Numeric calculation
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
function price() {
|
|
196
|
+
return +faker$1.commerce.price();
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Enhanced faker instance with additional utility methods for testing.
|
|
200
|
+
* Extends @faker-js/faker with custom methods for common test data generation patterns.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* import { faker } from '@geekmidas/testkit';
|
|
205
|
+
*
|
|
206
|
+
* // Use standard faker methods
|
|
207
|
+
* const name = faker.person.fullName();
|
|
208
|
+
* const email = faker.internet.email();
|
|
209
|
+
*
|
|
210
|
+
* // Use custom extensions
|
|
211
|
+
* const { createdAt, updatedAt } = faker.timestamps();
|
|
212
|
+
* const id = faker.identifier('user');
|
|
213
|
+
* const orderNumber = faker.sequence('order');
|
|
214
|
+
* const productPrice = faker.price();
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
const faker$1 = Object.freeze(Object.assign({}, faker, {
|
|
218
|
+
timestamps,
|
|
219
|
+
identifier,
|
|
220
|
+
sequence,
|
|
221
|
+
resetSequence,
|
|
222
|
+
resetAllSequences,
|
|
223
|
+
price
|
|
224
|
+
}));
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
export { faker$1 as faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const __faker_js_faker = require_chunk.__toESM(require("@faker-js/faker"));
|
|
3
|
+
|
|
4
|
+
//#region src/faker.ts
|
|
5
|
+
/**
|
|
6
|
+
* Atomic counter implementation for thread-safe sequence generation.
|
|
7
|
+
* Provides a clean abstraction for generating sequential numbers in tests.
|
|
8
|
+
* While JavaScript is single-threaded, this class makes the intent explicit.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const counter = new AtomicCounter(100);
|
|
13
|
+
* console.log(counter.increment()); // 101
|
|
14
|
+
* console.log(counter.increment()); // 102
|
|
15
|
+
* console.log(counter.get()); // 102
|
|
16
|
+
* counter.reset(200);
|
|
17
|
+
* console.log(counter.increment()); // 201
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
var AtomicCounter = class {
|
|
21
|
+
/**
|
|
22
|
+
* The current counter value.
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
value;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new atomic counter.
|
|
28
|
+
* @param initialValue - The starting value (default: 0)
|
|
29
|
+
*/
|
|
30
|
+
constructor(initialValue = 0) {
|
|
31
|
+
this.value = initialValue;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Increments the counter and returns the new value.
|
|
35
|
+
* @returns The incremented value
|
|
36
|
+
*/
|
|
37
|
+
increment() {
|
|
38
|
+
return ++this.value;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Gets the current counter value without incrementing.
|
|
42
|
+
* @returns The current value
|
|
43
|
+
*/
|
|
44
|
+
get() {
|
|
45
|
+
return this.value;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resets the counter to a specific value.
|
|
49
|
+
* @param value - The new value (default: 0)
|
|
50
|
+
*/
|
|
51
|
+
reset(value = 0) {
|
|
52
|
+
this.value = value;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Generates random timestamp fields for database records.
|
|
57
|
+
* Creates a createdAt date in the past and an updatedAt date between creation and now.
|
|
58
|
+
* Milliseconds are set to 0 for cleaner database storage.
|
|
59
|
+
*
|
|
60
|
+
* @returns Object with createdAt and updatedAt Date fields
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const { createdAt, updatedAt } = timestamps();
|
|
65
|
+
* console.log(createdAt); // 2023-05-15T10:30:00.000Z
|
|
66
|
+
* console.log(updatedAt); // 2023-11-20T14:45:00.000Z
|
|
67
|
+
*
|
|
68
|
+
* // Use in factory
|
|
69
|
+
* const user = {
|
|
70
|
+
* name: 'John Doe',
|
|
71
|
+
* ...timestamps()
|
|
72
|
+
* };
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function timestamps() {
|
|
76
|
+
const createdAt = faker.date.past();
|
|
77
|
+
const updatedAt = faker.date.between({
|
|
78
|
+
from: createdAt,
|
|
79
|
+
to: /* @__PURE__ */ new Date()
|
|
80
|
+
});
|
|
81
|
+
createdAt.setMilliseconds(0);
|
|
82
|
+
updatedAt.setMilliseconds(0);
|
|
83
|
+
return {
|
|
84
|
+
createdAt,
|
|
85
|
+
updatedAt
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Generates a reverse domain name identifier.
|
|
90
|
+
* Useful for creating unique identifiers that follow domain naming conventions.
|
|
91
|
+
*
|
|
92
|
+
* @param suffix - Optional suffix to append to the identifier
|
|
93
|
+
* @returns A reverse domain name string (e.g., "com.example.feature123")
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* console.log(identifier()); // "com.example.widget1"
|
|
98
|
+
* console.log(identifier('user')); // "org.acme.user"
|
|
99
|
+
* console.log(identifier('api')); // "net.demo.api"
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
function identifier(suffix) {
|
|
103
|
+
return [
|
|
104
|
+
faker.internet.domainSuffix(),
|
|
105
|
+
faker.internet.domainWord(),
|
|
106
|
+
suffix ? suffix : faker.internet.domainWord() + sequence("identifier")
|
|
107
|
+
].join(".");
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Storage for named sequence counters.
|
|
111
|
+
* Each sequence maintains its own independent counter.
|
|
112
|
+
* @private
|
|
113
|
+
*/
|
|
114
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
115
|
+
/**
|
|
116
|
+
* Generates sequential numbers for a named sequence.
|
|
117
|
+
* Useful for creating unique IDs or numbered test data.
|
|
118
|
+
* Each named sequence maintains its own counter.
|
|
119
|
+
*
|
|
120
|
+
* @param name - The sequence name (default: 'default')
|
|
121
|
+
* @returns The next number in the sequence
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* console.log(sequence()); // 1
|
|
126
|
+
* console.log(sequence()); // 2
|
|
127
|
+
* console.log(sequence('user')); // 1
|
|
128
|
+
* console.log(sequence('user')); // 2
|
|
129
|
+
* console.log(sequence()); // 3
|
|
130
|
+
*
|
|
131
|
+
* // Use in factories
|
|
132
|
+
* const email = `user${sequence('email')}@example.com`;
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
function sequence(name = "default") {
|
|
136
|
+
if (!sequences.has(name)) sequences.set(name, new AtomicCounter());
|
|
137
|
+
const counter = sequences.get(name);
|
|
138
|
+
return counter.increment();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Resets a named sequence counter to a specific value.
|
|
142
|
+
* Useful for resetting sequences between test suites.
|
|
143
|
+
*
|
|
144
|
+
* @param name - The sequence name to reset (default: 'default')
|
|
145
|
+
* @param value - The new starting value (default: 0)
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* sequence('user'); // 1
|
|
150
|
+
* sequence('user'); // 2
|
|
151
|
+
* resetSequence('user');
|
|
152
|
+
* sequence('user'); // 1
|
|
153
|
+
*
|
|
154
|
+
* resetSequence('order', 1000);
|
|
155
|
+
* sequence('order'); // 1001
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
function resetSequence(name = "default", value = 0) {
|
|
159
|
+
if (sequences.has(name)) {
|
|
160
|
+
const counter = sequences.get(name);
|
|
161
|
+
counter.reset(value);
|
|
162
|
+
} else sequences.set(name, new AtomicCounter(value));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Resets all sequence counters.
|
|
166
|
+
* Useful for cleaning up between test suites to ensure predictable sequences.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* // In test setup
|
|
171
|
+
* beforeEach(() => {
|
|
172
|
+
* resetAllSequences();
|
|
173
|
+
* });
|
|
174
|
+
*
|
|
175
|
+
* it('starts sequences from 1', () => {
|
|
176
|
+
* expect(sequence()).toBe(1);
|
|
177
|
+
* expect(sequence('user')).toBe(1);
|
|
178
|
+
* });
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
function resetAllSequences() {
|
|
182
|
+
sequences.clear();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Generates a random price as a number.
|
|
186
|
+
* Converts faker's string price to a numeric value.
|
|
187
|
+
*
|
|
188
|
+
* @returns A random price number
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* const productPrice = price(); // 29.99
|
|
193
|
+
* const total = price() * quantity; // Numeric calculation
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
function price() {
|
|
197
|
+
return +faker.commerce.price();
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Enhanced faker instance with additional utility methods for testing.
|
|
201
|
+
* Extends @faker-js/faker with custom methods for common test data generation patterns.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* import { faker } from '@geekmidas/testkit';
|
|
206
|
+
*
|
|
207
|
+
* // Use standard faker methods
|
|
208
|
+
* const name = faker.person.fullName();
|
|
209
|
+
* const email = faker.internet.email();
|
|
210
|
+
*
|
|
211
|
+
* // Use custom extensions
|
|
212
|
+
* const { createdAt, updatedAt } = faker.timestamps();
|
|
213
|
+
* const id = faker.identifier('user');
|
|
214
|
+
* const orderNumber = faker.sequence('order');
|
|
215
|
+
* const productPrice = faker.price();
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
const faker = Object.freeze(Object.assign({}, __faker_js_faker.faker, {
|
|
219
|
+
timestamps,
|
|
220
|
+
identifier,
|
|
221
|
+
sequence,
|
|
222
|
+
resetSequence,
|
|
223
|
+
resetAllSequences,
|
|
224
|
+
price
|
|
225
|
+
}));
|
|
226
|
+
|
|
227
|
+
//#endregion
|
|
228
|
+
Object.defineProperty(exports, 'faker', {
|
|
229
|
+
enumerable: true,
|
|
230
|
+
get: function () {
|
|
231
|
+
return faker;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
Object.defineProperty(exports, 'identifier', {
|
|
235
|
+
enumerable: true,
|
|
236
|
+
get: function () {
|
|
237
|
+
return identifier;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
Object.defineProperty(exports, 'resetAllSequences', {
|
|
241
|
+
enumerable: true,
|
|
242
|
+
get: function () {
|
|
243
|
+
return resetAllSequences;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
Object.defineProperty(exports, 'resetSequence', {
|
|
247
|
+
enumerable: true,
|
|
248
|
+
get: function () {
|
|
249
|
+
return resetSequence;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
Object.defineProperty(exports, 'sequence', {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
get: function () {
|
|
255
|
+
return sequence;
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
Object.defineProperty(exports, 'timestamps', {
|
|
259
|
+
enumerable: true,
|
|
260
|
+
get: function () {
|
|
261
|
+
return timestamps;
|
|
262
|
+
}
|
|
263
|
+
});
|
package/dist/faker.cjs
CHANGED
package/dist/faker.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { faker, identifier, resetAllSequences, resetSequence, sequence, timestamps } from "./faker-
|
|
1
|
+
import { faker, identifier, resetAllSequences, resetSequence, sequence, timestamps } from "./faker-CxKkEeYi.mjs";
|
|
2
2
|
|
|
3
3
|
export { faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
|
|
2
|
+
import pg from "pg";
|
|
3
|
+
|
|
4
|
+
//#region src/helpers.ts
|
|
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
|
+
function createKyselyDb(config) {
|
|
40
|
+
return new Kysely({
|
|
41
|
+
dialect: new PostgresDialect({ pool: new pg.Pool(config) }),
|
|
42
|
+
plugins: [new CamelCasePlugin()]
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { createKyselyDb };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const kysely = require_chunk.__toESM(require("kysely"));
|
|
3
|
+
const pg = require_chunk.__toESM(require("pg"));
|
|
4
|
+
|
|
5
|
+
//#region src/helpers.ts
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Kysely database instance with PostgreSQL dialect and camelCase plugin.
|
|
8
|
+
* This is a convenience function for quickly setting up a Kysely connection for testing.
|
|
9
|
+
*
|
|
10
|
+
* @template Database - The database schema type
|
|
11
|
+
* @param config - PostgreSQL connection configuration (pg.Pool config)
|
|
12
|
+
* @returns A configured Kysely instance
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* interface Database {
|
|
17
|
+
* users: UsersTable;
|
|
18
|
+
* posts: PostsTable;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // Create from connection string
|
|
22
|
+
* const db = createKyselyDb<Database>({
|
|
23
|
+
* connectionString: 'postgresql://user:pass@localhost:5432/testdb'
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Create with detailed config
|
|
27
|
+
* const db = createKyselyDb<Database>({
|
|
28
|
+
* host: 'localhost',
|
|
29
|
+
* port: 5432,
|
|
30
|
+
* database: 'testdb',
|
|
31
|
+
* user: 'testuser',
|
|
32
|
+
* password: 'testpass',
|
|
33
|
+
* max: 10 // connection pool size
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Use in tests
|
|
37
|
+
* const users = await db.selectFrom('users').selectAll().execute();
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function createKyselyDb(config) {
|
|
41
|
+
return new kysely.Kysely({
|
|
42
|
+
dialect: new kysely.PostgresDialect({ pool: new pg.default.Pool(config) }),
|
|
43
|
+
plugins: [new kysely.CamelCasePlugin()]
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
Object.defineProperty(exports, 'createKyselyDb', {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () {
|
|
51
|
+
return createKyselyDb;
|
|
52
|
+
}
|
|
53
|
+
});
|
package/dist/helpers.cjs
CHANGED
package/dist/helpers.mjs
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
2
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-YWnSJiIH.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/kysely.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a wrapped Vitest test API with automatic transaction rollback for Kysely.
|
|
7
|
+
* Each test runs in an isolated database transaction that is rolled back after completion.
|
|
8
|
+
* This ensures tests don't affect each other's data and run faster than truncating tables.
|
|
9
|
+
*
|
|
10
|
+
* @template Database - The database schema type
|
|
11
|
+
* @param api - The Vitest test API (usually `test` from vitest)
|
|
12
|
+
* @param db - The Kysely database instance
|
|
13
|
+
* @param setup - Optional setup function to run before each test in the transaction
|
|
14
|
+
* @param level - Transaction isolation level (defaults to REPEATABLE_READ)
|
|
15
|
+
* @returns A wrapped test API that provides transaction isolation
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { test } from 'vitest';
|
|
20
|
+
* import { wrapVitestKyselyTransaction } from '@geekmidas/testkit/kysely';
|
|
21
|
+
* import { db } from './database';
|
|
22
|
+
*
|
|
23
|
+
* // Create isolated test with automatic rollback
|
|
24
|
+
* const isolatedTest = wrapVitestKyselyTransaction(test, db);
|
|
25
|
+
*
|
|
26
|
+
* // Use in tests - each test gets its own transaction
|
|
27
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
28
|
+
* const user = await trx
|
|
29
|
+
* .insertInto('users')
|
|
30
|
+
* .values({ name: 'Test User', email: 'test@example.com' })
|
|
31
|
+
* .returningAll()
|
|
32
|
+
* .executeTakeFirst();
|
|
33
|
+
*
|
|
34
|
+
* expect(user).toBeDefined();
|
|
35
|
+
* // User is automatically rolled back after test
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // With setup function for common test data
|
|
39
|
+
* const testWithSetup = wrapVitestKyselyTransaction(
|
|
40
|
+
* test,
|
|
41
|
+
* db,
|
|
42
|
+
* async (trx) => {
|
|
43
|
+
* // Create common test data
|
|
44
|
+
* await trx.insertInto('settings')
|
|
45
|
+
* .values({ key: 'test_mode', value: 'true' })
|
|
46
|
+
* .execute();
|
|
47
|
+
* }
|
|
48
|
+
* );
|
|
49
|
+
*
|
|
50
|
+
* testWithSetup('should have test settings', async ({ trx }) => {
|
|
51
|
+
* const setting = await trx
|
|
52
|
+
* .selectFrom('settings')
|
|
53
|
+
* .where('key', '=', 'test_mode')
|
|
54
|
+
* .selectAll()
|
|
55
|
+
* .executeTakeFirst();
|
|
56
|
+
*
|
|
57
|
+
* expect(setting?.value).toBe('true');
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function wrapVitestKyselyTransaction(api, db, setup, level = require_VitestTransactionIsolator.IsolationLevel.REPEATABLE_READ) {
|
|
62
|
+
const wrapper = new require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator(api);
|
|
63
|
+
return wrapper.wrapVitestWithTransaction(db, setup, level);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
Object.defineProperty(exports, 'wrapVitestKyselyTransaction', {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
get: function () {
|
|
70
|
+
return wrapVitestKyselyTransaction;
|
|
71
|
+
}
|
|
72
|
+
});
|