@geekmidas/testkit 0.0.7 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/testkit",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -60,7 +60,8 @@ export class VitestObjectionTransactionIsolator extends VitestPostgresTransactio
60
60
  level: IsolationLevel,
61
61
  fn: (trx: Knex.Transaction) => Promise<void>,
62
62
  ): Promise<void> {
63
- const isolationLevel = level.toUpperCase() as Lowercase<IsolationLevel>;
63
+ const isolationLevel = level.toLowerCase() as Lowercase<IsolationLevel>;
64
+
64
65
  await conn.transaction(
65
66
  async (trx) => {
66
67
  await fn(trx);
@@ -0,0 +1,144 @@
1
+ import { Model } from 'objection';
2
+ import { it as base, describe, expect } from 'vitest';
3
+
4
+ import { createKnexDb, createTestTablesKnex } from '../../test/helpers';
5
+ import { wrapVitestObjectionTransaction } from '../objection';
6
+
7
+ // Define Objection models for testing
8
+ class User extends Model {
9
+ static get tableName() {
10
+ return 'users';
11
+ }
12
+
13
+ id!: number;
14
+ name!: string;
15
+ email!: string;
16
+ role?: string;
17
+ createdAt!: Date;
18
+ updatedAt?: Date;
19
+
20
+ static get relationMappings() {
21
+ return {
22
+ posts: {
23
+ relation: Model.HasManyRelation,
24
+ modelClass: Post,
25
+ join: {
26
+ from: 'users.id',
27
+ to: 'posts.user_id',
28
+ },
29
+ },
30
+ comments: {
31
+ relation: Model.HasManyRelation,
32
+ modelClass: Comment,
33
+ join: {
34
+ from: 'users.id',
35
+ to: 'comments.user_id',
36
+ },
37
+ },
38
+ };
39
+ }
40
+ }
41
+
42
+ class Post extends Model {
43
+ static get tableName() {
44
+ return 'posts';
45
+ }
46
+
47
+ id!: number;
48
+ title!: string;
49
+ content!: string;
50
+ userId!: number;
51
+ published?: boolean;
52
+ createdAt!: Date;
53
+ updatedAt?: Date;
54
+
55
+ static get relationMappings() {
56
+ return {
57
+ user: {
58
+ relation: Model.BelongsToOneRelation,
59
+ modelClass: User,
60
+ join: {
61
+ from: 'posts.user_id',
62
+ to: 'users.id',
63
+ },
64
+ },
65
+ comments: {
66
+ relation: Model.HasManyRelation,
67
+ modelClass: Comment,
68
+ join: {
69
+ from: 'posts.id',
70
+ to: 'comments.post_id',
71
+ },
72
+ },
73
+ };
74
+ }
75
+ }
76
+
77
+ class Comment extends Model {
78
+ static get tableName() {
79
+ return 'comments';
80
+ }
81
+
82
+ id!: number;
83
+ content!: string;
84
+ postId!: number;
85
+ userId!: number;
86
+ createdAt!: Date;
87
+
88
+ static get relationMappings() {
89
+ return {
90
+ post: {
91
+ relation: Model.BelongsToOneRelation,
92
+ modelClass: Post,
93
+ join: {
94
+ from: 'comments.post_id',
95
+ to: 'posts.id',
96
+ },
97
+ },
98
+ user: {
99
+ relation: Model.BelongsToOneRelation,
100
+ modelClass: User,
101
+ join: {
102
+ from: 'comments.user_id',
103
+ to: 'users.id',
104
+ },
105
+ },
106
+ };
107
+ }
108
+ }
109
+
110
+ // Create database connection
111
+ const knex = createKnexDb();
112
+
113
+ // Bind models to Knex instance
114
+ Model.knex(knex);
115
+
116
+ // Create wrapped test with transaction isolation
117
+ const it = wrapVitestObjectionTransaction(base, knex, async (trx) => {
118
+ // Create tables in the transaction
119
+ await createTestTablesKnex(trx);
120
+ });
121
+
122
+ describe('VitestObjectionTransactionIsolator', () => {
123
+ describe('Transaction Isolation', () => {
124
+ it('should rollback data after test completes', async ({ trx }) => {
125
+ // Create a user within the transaction
126
+ const user = await User.query(trx).insert({
127
+ name: 'Test User',
128
+ email: 'test@example.com',
129
+ role: 'user',
130
+ });
131
+
132
+ expect(user).toBeDefined();
133
+ expect(user.id).toBeDefined();
134
+ expect(user.name).toBe('Test User');
135
+
136
+ // Verify user exists in transaction
137
+ const foundUser = await User.query(trx).findById(user.id);
138
+ expect(foundUser).toBeDefined();
139
+ expect(foundUser?.email).toBe(user.email);
140
+
141
+ // Data will be rolled back after this test
142
+ });
143
+ });
144
+ });
package/test/helpers.ts CHANGED
@@ -162,7 +162,9 @@ export async function createTestTables(
162
162
  /**
163
163
  * Creates test tables using Knex
164
164
  */
165
- async function createTestTablesKnex(trx: Knex.Transaction): Promise<void> {
165
+ export async function createTestTablesKnex(
166
+ trx: Knex.Transaction,
167
+ ): Promise<void> {
166
168
  // Create users table
167
169
  await trx.schema.createTable('users', (table) => {
168
170
  table.bigIncrements('id').primary();