Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/mongodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';
global.mongoose.conn = null;

const mockMongoose = { connection: 'mock' };

Check warning on line 108 in lib/mongodb.test.ts

View workflow job for this annotation

GitHub Actions / Format Β· Lint Β· Typecheck Β· Test

'mockMongoose' is assigned a value but never used
vi.mocked(mongoose.connect).mockRejectedValue(new Error('Database is disconnected'));

await expect(dbConnect()).rejects.toThrow('Database is disconnected');
Expand Down Expand Up @@ -138,4 +138,16 @@
expect(global.mongoose.conn).toBe(mockMongoose);
expect(conn).toBe(mockMongoose);
});

it('handles mongoose Connection State 3 (disconnecting) gracefully by throwing or clearing cache', async () => {
process.env.MONGODB_URI = 'mongodb://localhost:27017/test';
mockMongooseConnection.readyState = 3;

vi.mocked(mongoose.connect).mockRejectedValue(new Error('Database is disconnecting'));

await expect(dbConnect()).rejects.toThrow('Database is disconnecting');

// The promise should be cleared so it can try again
expect(global.mongoose.promise).toBeNull();
});
});
52 changes: 52 additions & 0 deletions models/User.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,56 @@ describe('User Model', () => {
readyStateSpy.mockRestore();
});
});

describe('Database Connection State 3 (Disconnecting) Handling', () => {
it('aborts/rolls back active transactions cleanly when connection is in state 3 (disconnecting)', async () => {
const { vi } = await import('vitest');

// 1. Mock mongoose.connection.readyState to return 3 (disconnecting)
const readyStateSpy = vi
.spyOn(mongoose.connection, 'readyState', 'get')
.mockReturnValue(3 as unknown as typeof mongoose.connection.readyState);

// 2. Mock a mongoose session with transaction support
const mockSession = {
startTransaction: vi.fn(),
commitTransaction: vi.fn(),
abortTransaction: vi.fn().mockResolvedValue(undefined),
endSession: vi.fn().mockResolvedValue(undefined),
} as unknown as mongoose.ClientSession;

const startSessionSpy = vi.spyOn(mongoose, 'startSession').mockResolvedValue(mockSession);

// 3. Simulate a database transaction workflow that checks connection state
const runTransactionWithCheck = async (session: mongoose.ClientSession) => {
session.startTransaction();
try {
if (mongoose.connection.readyState === 3) {
await session.abortTransaction();
return { status: 'aborted' };
}
await session.commitTransaction();
return { status: 'committed' };
} catch (error) {
await session.abortTransaction();
throw error;
} finally {
await session.endSession();
}
};

const session = await mongoose.startSession();
const result = await runTransactionWithCheck(session);

// 4. Assertions
expect(result.status).toBe('aborted');
expect(mockSession.abortTransaction).toHaveBeenCalledTimes(1);
expect(mockSession.endSession).toHaveBeenCalledTimes(1);
expect(mockSession.commitTransaction).not.toHaveBeenCalled();

// Cleanup
readyStateSpy.mockRestore();
startSessionSpy.mockRestore();
});
});
});
Loading