Skip to content

Commit 6a7b423

Browse files
authored
test(database): verify transaction rollback on connection state 3 (JhaSourav07#1954)
## Description Fixes JhaSourav07#1422 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [ ] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have starred the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents efa99bf + 5e53f42 commit 6a7b423

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,16 +167,15 @@ 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

181181
it('reuses an in-flight promise when state 3 triggers concurrent dbConnect calls', async () => {

models/User.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,37 @@ describe('User Model', () => {
343343
connectSpy.mockRestore();
344344
});
345345
});
346+
347+
describe('Database Connection State 3 Handling', () => {
348+
it('aborts active transactions when connection is state 3', async () => {
349+
const { vi } = await import('vitest');
350+
const readySpy = vi
351+
.spyOn(mongoose.connection, 'readyState', 'get')
352+
.mockReturnValue(3 as never);
353+
354+
const abortFn = vi.fn();
355+
const endFn = vi.fn();
356+
const startSpy = vi.spyOn(mongoose, 'startSession').mockResolvedValue({
357+
abortTransaction: abortFn,
358+
endSession: endFn,
359+
} as never);
360+
361+
const runTx = async () => {
362+
const sess = await mongoose.startSession();
363+
if (mongoose.connection.readyState === 3) await sess.abortTransaction();
364+
await sess.endSession();
365+
};
366+
367+
await runTx();
368+
369+
expect(mongoose.connection.readyState).toBe(3);
370+
expect(abortFn).toHaveBeenCalledTimes(1);
371+
expect(endFn).toHaveBeenCalledTimes(1);
372+
373+
readySpy.mockRestore();
374+
startSpy.mockRestore();
375+
});
376+
});
346377
});
347378

348379
/* ==========================================================================

0 commit comments

Comments
 (0)