@geekmidas/testkit 0.0.7 → 0.0.9

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.
Files changed (49) hide show
  1. package/dist/PostgresObjectionMigrator-B88aTT0m.cjs +122 -0
  2. package/dist/PostgresObjectionMigrator-DydSgYFv.mjs +117 -0
  3. package/dist/PostgresObjectionMigrator.cjs +4 -0
  4. package/dist/PostgresObjectionMigrator.mjs +4 -0
  5. package/dist/{VitestKyselyTransactionIsolator-AfxPJEwR.mjs → VitestKyselyTransactionIsolator-BKGT9nEG.mjs} +1 -1
  6. package/dist/{VitestKyselyTransactionIsolator-YWnSJiIH.cjs → VitestKyselyTransactionIsolator-CIlpIO78.cjs} +1 -1
  7. package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
  8. package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
  9. package/dist/{VitestObjectionTransactionIsolator-BZRYy8iW.mjs → VitestObjectionTransactionIsolator-BPoLUFop.mjs} +2 -2
  10. package/dist/{VitestObjectionTransactionIsolator-0uX6DW5G.cjs → VitestObjectionTransactionIsolator-DyqLp_in.cjs} +2 -2
  11. package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
  12. package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
  13. package/dist/VitestTransactionIsolator.cjs +1 -1
  14. package/dist/VitestTransactionIsolator.mjs +1 -1
  15. package/dist/__tests__/KyselyFactory.spec.cjs +5 -5
  16. package/dist/__tests__/KyselyFactory.spec.mjs +5 -5
  17. package/dist/__tests__/ObjectionFactory.spec.cjs +1 -1
  18. package/dist/__tests__/ObjectionFactory.spec.mjs +1 -1
  19. package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
  20. package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
  21. package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +432 -0
  22. package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +431 -0
  23. package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +130 -0
  24. package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +129 -0
  25. package/dist/__tests__/integration.spec.cjs +5 -5
  26. package/dist/__tests__/integration.spec.mjs +5 -5
  27. package/dist/{helpers-CukcFAU9.mjs → helpers-BEmjyUVE.mjs} +1 -1
  28. package/dist/{helpers-Bnm3Jy9X.cjs → helpers-CNMBePuj.cjs} +12 -0
  29. package/dist/helpers.cjs +1 -1
  30. package/dist/helpers.mjs +1 -1
  31. package/dist/{kysely-B-GOhABm.cjs → kysely-CBfCXxUn.cjs} +2 -2
  32. package/dist/{kysely-CqfoKVXs.mjs → kysely-Cx_1pZYc.mjs} +2 -2
  33. package/dist/kysely.cjs +3 -3
  34. package/dist/kysely.mjs +3 -3
  35. package/dist/objection-CCD8fMLj.cjs +87 -0
  36. package/dist/objection-lsMgM5gP.mjs +82 -0
  37. package/dist/objection.cjs +7 -81
  38. package/dist/objection.mjs +6 -81
  39. package/package.json +1 -1
  40. package/src/PostgresObjectionMigrator.ts +138 -0
  41. package/src/VitestObjectionTransactionIsolator.ts +2 -1
  42. package/src/__tests__/PostgresObjectionMigrator.spec.ts +634 -0
  43. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +144 -0
  44. package/src/objection.ts +1 -0
  45. package/test/helpers.ts +3 -1
  46. /package/dist/{VitestTransactionIsolator-kFL36T8x.mjs → VitestTransactionIsolator-BWwK-ca6.mjs} +0 -0
  47. /package/dist/{VitestTransactionIsolator-DcOz0LZF.cjs → VitestTransactionIsolator-CruLTRRi.cjs} +0 -0
  48. /package/dist/{helpers-CKMlwSYT.mjs → helpers-BuPmgzyQ.mjs} +0 -0
  49. /package/dist/{helpers-H4hO5SZR.cjs → helpers-nEUtQ7eo.cjs} +0 -0
@@ -0,0 +1,432 @@
1
+ const require_chunk = require('../chunk-CUT6urMc.cjs');
2
+ require('../PostgresMigrator-BtAWdLss.cjs');
3
+ const require_PostgresObjectionMigrator = require('../PostgresObjectionMigrator-B88aTT0m.cjs');
4
+ const require_helpers = require('../helpers-CNMBePuj.cjs');
5
+ const pg = require_chunk.__toESM(require("pg"));
6
+ const vitest = require_chunk.__toESM(require("vitest"));
7
+ const knex = require_chunk.__toESM(require("knex"));
8
+ const node_fs = require_chunk.__toESM(require("node:fs"));
9
+ const node_path = require_chunk.__toESM(require("node:path"));
10
+
11
+ //#region src/__tests__/PostgresObjectionMigrator.spec.ts
12
+ (0, vitest.describe)("PostgresObjectionMigrator", () => {
13
+ let testDbName;
14
+ let cleanupDb;
15
+ let consoleSpy;
16
+ let consoleErrorSpy;
17
+ let testMigrationsDir;
18
+ (0, vitest.beforeAll)(async () => {
19
+ testDbName = `test_postgres_objection_migrator_${Date.now()}`;
20
+ cleanupDb = await require_helpers.createTestDatabase(testDbName);
21
+ testMigrationsDir = node_path.default.join(process.cwd(), "test-migrations", Date.now().toString());
22
+ await node_fs.promises.mkdir(testMigrationsDir, { recursive: true });
23
+ await node_fs.promises.writeFile(node_path.default.join(testMigrationsDir, "001_create_users.js"), `
24
+ exports.up = function(knex) {
25
+ return knex.schema.createTable('users', function(table) {
26
+ table.increments('id').primary();
27
+ table.string('name');
28
+ table.string('email').unique();
29
+ table.timestamps(true, true);
30
+ });
31
+ };
32
+
33
+ exports.down = function(knex) {
34
+ return knex.schema.dropTable('users');
35
+ };
36
+ `);
37
+ await node_fs.promises.writeFile(node_path.default.join(testMigrationsDir, "002_create_posts.js"), `
38
+ exports.up = function(knex) {
39
+ return knex.schema.createTable('posts', function(table) {
40
+ table.increments('id').primary();
41
+ table.string('title');
42
+ table.text('content');
43
+ table.integer('user_id').unsigned().references('id').inTable('users');
44
+ table.timestamps(true, true);
45
+ });
46
+ };
47
+
48
+ exports.down = function(knex) {
49
+ return knex.schema.dropTable('posts');
50
+ };
51
+ `);
52
+ });
53
+ (0, vitest.beforeEach)(() => {
54
+ consoleSpy = vitest.vi.spyOn(console, "log").mockImplementation(() => {});
55
+ consoleErrorSpy = vitest.vi.spyOn(console, "error").mockImplementation(() => {});
56
+ });
57
+ (0, vitest.afterEach)(() => {
58
+ consoleSpy.mockRestore();
59
+ consoleErrorSpy.mockRestore();
60
+ });
61
+ (0, vitest.afterAll)(async () => {
62
+ await cleanupDb();
63
+ await node_fs.promises.rm(testMigrationsDir, {
64
+ recursive: true,
65
+ force: true
66
+ });
67
+ });
68
+ (0, vitest.describe)("constructor", () => {
69
+ (0, vitest.it)("should create a PostgresObjectionMigrator instance", () => {
70
+ const knexInstance = (0, knex.default)({
71
+ client: "pg",
72
+ connection: `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`
73
+ });
74
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
75
+ uri: `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
76
+ knex: knexInstance
77
+ });
78
+ (0, vitest.expect)(migrator).toBeInstanceOf(require_PostgresObjectionMigrator.PostgresObjectionMigrator);
79
+ knexInstance.destroy();
80
+ });
81
+ });
82
+ (0, vitest.describe)("migrate method", () => {
83
+ (0, vitest.it)("should run migrations to latest", async () => {
84
+ const newDbName = `test_migrate_${Date.now()}`;
85
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
86
+ const knexInstance = (0, knex.default)({
87
+ client: "pg",
88
+ connection: uri,
89
+ migrations: { directory: testMigrationsDir }
90
+ });
91
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
92
+ uri,
93
+ knex: knexInstance
94
+ });
95
+ const cleanup = await migrator.start();
96
+ (0, vitest.expect)(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${newDbName}`);
97
+ (0, vitest.expect)(consoleSpy).toHaveBeenCalledWith(vitest.expect.stringContaining("Applied batch"));
98
+ const verifyClient = new pg.Client({
99
+ host: "localhost",
100
+ port: 5432,
101
+ user: "geekmidas",
102
+ password: "geekmidas",
103
+ database: newDbName
104
+ });
105
+ await verifyClient.connect();
106
+ const tablesResult = await verifyClient.query(`
107
+ SELECT table_name FROM information_schema.tables
108
+ WHERE table_schema = 'public'
109
+ AND table_name IN ('users', 'posts')
110
+ ORDER BY table_name
111
+ `);
112
+ (0, vitest.expect)(tablesResult.rows).toEqual([{ table_name: "posts" }, { table_name: "users" }]);
113
+ await verifyClient.end();
114
+ await cleanup();
115
+ });
116
+ (0, vitest.it)("should handle no pending migrations", async () => {
117
+ const newDbName = `test_no_pending_${Date.now()}`;
118
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
119
+ const knexInstance1 = (0, knex.default)({
120
+ client: "pg",
121
+ connection: uri,
122
+ migrations: { directory: testMigrationsDir }
123
+ });
124
+ const migrator1 = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
125
+ uri,
126
+ knex: knexInstance1
127
+ });
128
+ const cleanup = await migrator1.start();
129
+ consoleSpy.mockClear();
130
+ const knexInstance2 = (0, knex.default)({
131
+ client: "pg",
132
+ connection: uri,
133
+ migrations: { directory: testMigrationsDir }
134
+ });
135
+ const migrator2 = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
136
+ uri,
137
+ knex: knexInstance2
138
+ });
139
+ await migrator2.migrate();
140
+ (0, vitest.expect)(consoleSpy).toHaveBeenCalledWith("No pending migrations to apply");
141
+ await cleanup();
142
+ });
143
+ (0, vitest.it)("should handle migration errors", async () => {
144
+ const newDbName = `test_migration_error_${Date.now()}`;
145
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
146
+ const badMigrationsDir = node_path.default.join(process.cwd(), "bad-migrations", Date.now().toString());
147
+ await node_fs.promises.mkdir(badMigrationsDir, { recursive: true });
148
+ await node_fs.promises.writeFile(node_path.default.join(badMigrationsDir, "001_bad_migration.js"), `
149
+ exports.up = function(knex) {
150
+ throw new Error('Migration failed on purpose');
151
+ };
152
+
153
+ exports.down = function(knex) {
154
+ return Promise.resolve();
155
+ };
156
+ `);
157
+ const knexInstance = (0, knex.default)({
158
+ client: "pg",
159
+ connection: uri,
160
+ migrations: { directory: badMigrationsDir }
161
+ });
162
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
163
+ uri,
164
+ knex: knexInstance
165
+ });
166
+ await (0, vitest.expect)(migrator.start()).rejects.toThrow("Migration failed on purpose");
167
+ (0, vitest.expect)(consoleErrorSpy).toHaveBeenCalledWith("Failed to apply migrations:", vitest.expect.any(Error));
168
+ await node_fs.promises.rm(badMigrationsDir, {
169
+ recursive: true,
170
+ force: true
171
+ });
172
+ const cleanupClient = new pg.Client({
173
+ host: "localhost",
174
+ port: 5432,
175
+ user: "geekmidas",
176
+ password: "geekmidas",
177
+ database: "postgres"
178
+ });
179
+ await cleanupClient.connect();
180
+ await cleanupClient.query(`DROP DATABASE IF EXISTS "${newDbName}"`);
181
+ await cleanupClient.end();
182
+ });
183
+ (0, vitest.it)("should destroy knex connection after migration", async () => {
184
+ const newDbName = `test_destroy_${Date.now()}`;
185
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
186
+ const knexInstance = (0, knex.default)({
187
+ client: "pg",
188
+ connection: uri,
189
+ migrations: { directory: testMigrationsDir }
190
+ });
191
+ const destroySpy = vitest.vi.spyOn(knexInstance, "destroy");
192
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
193
+ uri,
194
+ knex: knexInstance
195
+ });
196
+ const cleanup = await migrator.start();
197
+ (0, vitest.expect)(destroySpy).toHaveBeenCalled();
198
+ await cleanup();
199
+ });
200
+ });
201
+ (0, vitest.describe)("rollback method", () => {
202
+ (0, vitest.it)("should rollback last migration batch", async () => {
203
+ const newDbName = `test_rollback_${Date.now()}`;
204
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
205
+ const knexInstance1 = (0, knex.default)({
206
+ client: "pg",
207
+ connection: uri,
208
+ migrations: { directory: testMigrationsDir }
209
+ });
210
+ const migrator1 = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
211
+ uri,
212
+ knex: knexInstance1
213
+ });
214
+ const cleanup = await migrator1.start();
215
+ consoleSpy.mockClear();
216
+ const knexInstance2 = (0, knex.default)({
217
+ client: "pg",
218
+ connection: uri,
219
+ migrations: { directory: testMigrationsDir }
220
+ });
221
+ const migrator2 = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
222
+ uri,
223
+ knex: knexInstance2
224
+ });
225
+ await migrator2.rollback();
226
+ (0, vitest.expect)(consoleSpy).toHaveBeenCalledWith(vitest.expect.stringContaining("Rolled back batch"));
227
+ const verifyClient = new pg.Client({
228
+ host: "localhost",
229
+ port: 5432,
230
+ user: "geekmidas",
231
+ password: "geekmidas",
232
+ database: newDbName
233
+ });
234
+ await verifyClient.connect();
235
+ const tablesResult = await verifyClient.query(`
236
+ SELECT table_name FROM information_schema.tables
237
+ WHERE table_schema = 'public'
238
+ AND table_name IN ('users', 'posts')
239
+ `);
240
+ (0, vitest.expect)(tablesResult.rows).toEqual([]);
241
+ await verifyClient.end();
242
+ await cleanup();
243
+ });
244
+ (0, vitest.it)("should handle no migrations to rollback", async () => {
245
+ const newDbName = `test_no_rollback_${Date.now()}`;
246
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
247
+ const createClient = new pg.Client({
248
+ host: "localhost",
249
+ port: 5432,
250
+ user: "geekmidas",
251
+ password: "geekmidas",
252
+ database: "postgres"
253
+ });
254
+ await createClient.connect();
255
+ await createClient.query(`CREATE DATABASE "${newDbName}"`);
256
+ await createClient.end();
257
+ const knexInstance = (0, knex.default)({
258
+ client: "pg",
259
+ connection: uri,
260
+ migrations: { directory: testMigrationsDir }
261
+ });
262
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
263
+ uri,
264
+ knex: knexInstance
265
+ });
266
+ await migrator.rollback();
267
+ (0, vitest.expect)(consoleSpy).toHaveBeenCalledWith("No migrations to rollback");
268
+ const cleanupClient = new pg.Client({
269
+ host: "localhost",
270
+ port: 5432,
271
+ user: "geekmidas",
272
+ password: "geekmidas",
273
+ database: "postgres"
274
+ });
275
+ await cleanupClient.connect();
276
+ await cleanupClient.query(`DROP DATABASE IF EXISTS "${newDbName}"`);
277
+ await cleanupClient.end();
278
+ });
279
+ (0, vitest.it)("should handle rollback errors", async () => {
280
+ const newDbName = `test_rollback_error_${Date.now()}`;
281
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
282
+ const knexInstance = (0, knex.default)({
283
+ client: "pg",
284
+ connection: uri,
285
+ migrations: { directory: testMigrationsDir }
286
+ });
287
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
288
+ uri,
289
+ knex: knexInstance
290
+ });
291
+ await (0, vitest.expect)(migrator.rollback()).rejects.toThrow();
292
+ (0, vitest.expect)(consoleErrorSpy).toHaveBeenCalledWith("Failed to rollback migrations:", vitest.expect.any(Error));
293
+ await knexInstance.destroy();
294
+ });
295
+ });
296
+ (0, vitest.describe)("status method", () => {
297
+ (0, vitest.it)("should return migration status", async () => {
298
+ const newDbName = `test_status_${Date.now()}`;
299
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
300
+ const knexInstance1 = (0, knex.default)({
301
+ client: "pg",
302
+ connection: uri,
303
+ migrations: { directory: testMigrationsDir }
304
+ });
305
+ const migrator1 = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
306
+ uri,
307
+ knex: knexInstance1
308
+ });
309
+ const cleanup = await migrator1.start();
310
+ const knexInstance2 = (0, knex.default)({
311
+ client: "pg",
312
+ connection: uri,
313
+ migrations: { directory: testMigrationsDir }
314
+ });
315
+ const migrator2 = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
316
+ uri,
317
+ knex: knexInstance2
318
+ });
319
+ const status = await migrator2.status();
320
+ (0, vitest.expect)(status).toHaveProperty("completed");
321
+ (0, vitest.expect)(status).toHaveProperty("pending");
322
+ (0, vitest.expect)(Array.isArray(status.completed)).toBe(true);
323
+ (0, vitest.expect)(Array.isArray(status.pending)).toBe(true);
324
+ await cleanup();
325
+ });
326
+ (0, vitest.it)("should destroy connection after getting status", async () => {
327
+ const newDbName = `test_status_destroy_${Date.now()}`;
328
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
329
+ const knexInstance = (0, knex.default)({
330
+ client: "pg",
331
+ connection: uri,
332
+ migrations: { directory: testMigrationsDir }
333
+ });
334
+ const destroySpy = vitest.vi.spyOn(knexInstance, "destroy");
335
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
336
+ uri,
337
+ knex: knexInstance
338
+ });
339
+ const createClient = new pg.Client({
340
+ host: "localhost",
341
+ port: 5432,
342
+ user: "geekmidas",
343
+ password: "geekmidas",
344
+ database: "postgres"
345
+ });
346
+ await createClient.connect();
347
+ await createClient.query(`CREATE DATABASE "${newDbName}"`);
348
+ await createClient.end();
349
+ await migrator.status();
350
+ (0, vitest.expect)(destroySpy).toHaveBeenCalled();
351
+ const cleanupClient = new pg.Client({
352
+ host: "localhost",
353
+ port: 5432,
354
+ user: "geekmidas",
355
+ password: "geekmidas",
356
+ database: "postgres"
357
+ });
358
+ await cleanupClient.connect();
359
+ await cleanupClient.query(`DROP DATABASE IF EXISTS "${newDbName}"`);
360
+ await cleanupClient.end();
361
+ });
362
+ });
363
+ (0, vitest.describe)("integration scenarios", () => {
364
+ (0, vitest.it)("should handle complete workflow with complex migrations", async () => {
365
+ const integrationDbName = `test_integration_${Date.now()}`;
366
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
367
+ const knexInstance = (0, knex.default)({
368
+ client: "pg",
369
+ connection: uri,
370
+ migrations: { directory: testMigrationsDir }
371
+ });
372
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
373
+ uri,
374
+ knex: knexInstance
375
+ });
376
+ const cleanup = await migrator.start();
377
+ const testClient = new pg.Client({
378
+ host: "localhost",
379
+ port: 5432,
380
+ user: "geekmidas",
381
+ password: "geekmidas",
382
+ database: integrationDbName
383
+ });
384
+ await testClient.connect();
385
+ const userResult = await testClient.query(`INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id`, ["Test User", "test@example.com"]);
386
+ const userId = userResult.rows[0].id;
387
+ await testClient.query(`INSERT INTO posts (title, content, user_id) VALUES ($1, $2, $3)`, [
388
+ "Test Post",
389
+ "This is a test post",
390
+ userId
391
+ ]);
392
+ const postResult = await testClient.query(`SELECT * FROM posts WHERE user_id = $1`, [userId]);
393
+ (0, vitest.expect)(postResult.rowCount).toBe(1);
394
+ (0, vitest.expect)(postResult.rows[0].title).toBe("Test Post");
395
+ await testClient.end();
396
+ await cleanup();
397
+ });
398
+ (0, vitest.it)("should work with transaction-based tests", async () => {
399
+ const transactionDbName = `test_transaction_${Date.now()}`;
400
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${transactionDbName}`;
401
+ const knexInstance = (0, knex.default)({
402
+ client: "pg",
403
+ connection: uri,
404
+ migrations: { directory: testMigrationsDir }
405
+ });
406
+ const migrator = new require_PostgresObjectionMigrator.PostgresObjectionMigrator({
407
+ uri,
408
+ knex: knexInstance
409
+ });
410
+ const cleanup = await migrator.start();
411
+ const testKnex = (0, knex.default)({
412
+ client: "pg",
413
+ connection: uri
414
+ });
415
+ await testKnex.transaction(async (trx) => {
416
+ await trx("users").insert({
417
+ name: "Transaction User",
418
+ email: "trx@example.com"
419
+ });
420
+ const users$1 = await trx("users").select("*");
421
+ (0, vitest.expect)(users$1.length).toBeGreaterThan(0);
422
+ throw new Error("Rollback transaction");
423
+ }).catch(() => {});
424
+ const users = await testKnex("users").select("*");
425
+ (0, vitest.expect)(users.length).toBe(0);
426
+ await testKnex.destroy();
427
+ await cleanup();
428
+ });
429
+ });
430
+ });
431
+
432
+ //#endregion