Skip to content

Commit ff9c152

Browse files
authored
test(mongodb): verify User schema behaviors under connection state 3 (disconnecting) (Variation 3) (JhaSourav07#2006)
## Description Fixes JhaSourav07#1418 Added test coverage for MongoDB connection state `3` (`disconnecting`) in `lib/mongodb.test.ts` and `models/User.test.ts` to ensure the connection flow and User model remain usable during disconnecting state. ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview Test verification for MongoDB connection state 3 (disconnecting): <img width="831" height="171" alt="Test Results" src="https://github.com/user-attachments/assets/183168ff-a4e0-4c1f-9f54-83b77c75a70c" /> ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally. * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors. * [x] I have updated `README.md` if I added a new theme or URL parameter. * [x] My commits follow the Conventional Commits format. * [x] I have started the repo. * [x] I have made sure that I have only one commit to merge in this PR. * [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard. * [x] (Recommended) I joined the CommitPulse Discord community.
2 parents 3ec3e38 + 277375f commit ff9c152

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

lib/mongodb.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ describe('dbConnect', () => {
113113
expect(global.mongoose.promise).toBeNull();
114114
});
115115

116+
it('handles mongoose Connection State 3 (disconnecting) gracefully', async () => {
117+
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';
118+
global.mongoose.conn = null;
119+
mockMongooseConnection.readyState = 3;
120+
121+
const mockMongoose = { connection: 'mock' };
122+
setConnectedMongoose(mockMongoose as unknown as typeof mongoose);
123+
124+
const conn = await dbConnect();
125+
126+
expect(mongoose.connect).toHaveBeenCalledTimes(1);
127+
expect(conn).toBe(mockMongoose);
128+
expect(global.mongoose.conn).toBe(mockMongoose);
129+
});
130+
131+
it('returns the cached connection immediately when mongoose is already connected', async () => {
132+
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';
133+
134+
const mockMongoose = { connection: 'mock' };
135+
global.mongoose.conn = mockMongoose as unknown as typeof mongoose;
136+
mockMongooseConnection.readyState = 1;
137+
138+
const conn = await dbConnect();
139+
140+
expect(conn).toBe(mockMongoose);
141+
expect(mongoose.connect).not.toHaveBeenCalled();
142+
});
143+
116144
it('throws when called from the Edge runtime', async () => {
117145
vi.stubEnv('NEXT_RUNTIME', 'edge');
118146
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';

models/User.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,20 @@ describe('User Model', () => {
270270
connectSpy.mockRestore();
271271
});
272272
});
273+
274+
describe('Database Connection State 1 Handling', () => {
275+
it('keeps the User model usable while mongoose is connected', async (): Promise<void> => {
276+
const readyStateSpy = vi
277+
.spyOn(mongoose.connection, 'readyState', 'get')
278+
.mockReturnValue(1 as unknown as typeof mongoose.connection.readyState);
279+
280+
expect(mongoose.connection.readyState).toBe(1);
281+
expect(User).toBeDefined();
282+
expect(User.modelName).toBe('User');
283+
284+
readyStateSpy.mockRestore();
285+
});
286+
});
273287
});
274288

275289
/* ==========================================================================

0 commit comments

Comments
 (0)