fix: defer MongoDB connection until runtime
CI Pipeline with Test Management / 🧪 Run Tests & Generate Reports (push) Waiting to run
CI Pipeline with Test Management / 🐳 Docker Integration Tests (push) Blocked by required conditions
CI Pipeline with Test Management / 🏗️ Build Docker Image (push) Blocked by required conditions
CI Pipeline with Test Management / 📊 Generate Test Summary (push) Blocked by required conditions
Test Reporting & Gherkin Analysis / 🧪 Run Tests & Generate Reports (push) Waiting to run
Test Reporting & Gherkin Analysis / 📊 Analyze Test Coverage (push) Blocked by required conditions
Test Reporting & Gherkin Analysis / 🔄 Sync with Linear (push) Blocked by required conditions
Test Reporting & Gherkin Analysis / ⚡ Performance Monitoring (push) Blocked by required conditions

This commit is contained in:
Do Siki
2026-08-17 17:00:41 +02:00
parent 82124f3d2b
commit 2c9d3d01f1
2 changed files with 33 additions and 31 deletions
+9 -9
View File
@@ -32,6 +32,7 @@ const originalEnv = process.env
describe('MongoDB Connection (Unit Tests)', () => {
beforeEach(() => {
jest.resetModules()
process.env = {
...originalEnv,
MONGODB_URI: 'mongodb://localhost:27017/test',
@@ -58,9 +59,8 @@ describe('MongoDB Connection (Unit Tests)', () => {
// Clear module cache to force re-import
jest.resetModules()
expect(() => {
require('./mongodb')
}).toThrow('Please add MONGODB_URI to your environment variables')
const { getClientPromise } = require('./mongodb')
expect(() => getClientPromise()).toThrow('Please add MONGODB_URI to your environment variables')
// Restore environment
process.env.MONGODB_URI = originalMongodbUri
@@ -70,9 +70,9 @@ describe('MongoDB Connection (Unit Tests)', () => {
describe('Database connection', () => {
it('should create MongoClient with correct URI and options', async () => {
// Import after setting up the mock
const { clientPromise } = await import('./mongodb')
const { getClientPromise } = await import('./mongodb')
await clientPromise
await getClientPromise()
expect(MockedMongoClient).toHaveBeenCalledWith(
process.env.MONGODB_URI,
@@ -131,13 +131,13 @@ describe('MongoDB Connection (Unit Tests)', () => {
describe('Connection reuse and caching', () => {
it('should reuse the same MongoClient instance for multiple calls', async () => {
const { clientPromise } = await import('./mongodb')
const { getClientPromise } = await import('./mongodb')
const client1 = await clientPromise
const client2 = await clientPromise
const client1 = await getClientPromise()
const client2 = await getClientPromise()
// Should be the same client instance
expect(client1).toBe(client2)
})
})
})
})