Skip to content

Commit 5e53f42

Browse files
test(database): verify transaction rollback on connection state 3
1 parent b251324 commit 5e53f42

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

lib/mongodb.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ describe('dbConnect', () => {
105105
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';
106106
global.mongoose.conn = null;
107107

108+
//const mockMongoose = { connection: 'mock' };
108109
vi.mocked(mongoose.connect).mockRejectedValue(new Error('Database is disconnected'));
109110

110111
await expect(dbConnect()).rejects.toThrow('Database is disconnected');
@@ -166,15 +167,14 @@ describe('dbConnect', () => {
166167
expect(conn).toBe(mockMongoose);
167168
});
168169

169-
it('handles mongoose Connection State 3 (disconnecting) gracefully by throwing or clearing cache', async () => {
170+
it('handles mongoose Connection State 3 (disconnecting) gracefully', async () => {
170171
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';
171172
mockMongooseConnection.readyState = 3;
173+
const mockMDB = { connection: 'mock' };
174+
setConnectedMongoose(mockMDB as unknown as typeof mongoose);
172175

173-
vi.mocked(mongoose.connect).mockRejectedValue(new Error('Database is disconnecting'));
174-
175-
await expect(dbConnect()).rejects.toThrow('Database is disconnecting');
176-
177-
// The promise should be cleared so it can try again
178-
expect(global.mongoose.promise).toBeNull();
176+
const res = await dbConnect();
177+
expect(mongoose.connect).toHaveBeenCalledTimes(1);
178+
expect(res).toBe(mockMDB);
179179
});
180180
});

models/User.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,37 @@ describe('User Model', () => {
284284
readyStateSpy.mockRestore();
285285
});
286286
});
287+
288+
describe('Database Connection State 3 Handling', () => {
289+
it('aborts active transactions when connection is state 3', async () => {
290+
const { vi } = await import('vitest');
291+
const readySpy = vi
292+
.spyOn(mongoose.connection, 'readyState', 'get')
293+
.mockReturnValue(3 as never);
294+
295+
const abortFn = vi.fn();
296+
const endFn = vi.fn();
297+
const startSpy = vi.spyOn(mongoose, 'startSession').mockResolvedValue({
298+
abortTransaction: abortFn,
299+
endSession: endFn,
300+
} as never);
301+
302+
const runTx = async () => {
303+
const sess = await mongoose.startSession();
304+
if (mongoose.connection.readyState === 3) await sess.abortTransaction();
305+
await sess.endSession();
306+
};
307+
308+
await runTx();
309+
310+
expect(mongoose.connection.readyState).toBe(3);
311+
expect(abortFn).toHaveBeenCalledTimes(1);
312+
expect(endFn).toHaveBeenCalledTimes(1);
313+
314+
readySpy.mockRestore();
315+
startSpy.mockRestore();
316+
});
317+
});
287318
});
288319

289320
/* ==========================================================================

0 commit comments

Comments
 (0)