'Jest throwing Mongo error - Operation `User._findAndModify()` buffering timed out after 10000ms. But this error only occurs while testing

My code works fine while normal development, but when I am testing it using jest it always throws 400 bad req with error - "Operation User._findAndModify() buffering timed out after 10000ms."

My Code -

const request = require('supertest');
const httpStatus = require('http-status');
const app = require('../src/app');

jest.setTimeout(30000);
describe('POST /v1/login', () => {
    test('should return 200 ', async () => {

      const loginCredentials = {
        "email": "[email protected]",
        "name": "roomi",
        "token": "token"
      };

      const res = await request(app).post('/v1/login').send(loginCredentials);
      console.log('Response Body :', JSON.stringify(res.body, null, 2))
      expect(res.status).toBe(200)
    });
  });

Terminal output -

console.log
    Response Body : {
      "success": false,
      "message": "Operation `User._findAndModify()` buffering timed out after 10000ms",
      "error": {
        "subError": {
          "message": "Operation `User._findAndModify()` buffering timed out after 10000ms"
        },
        "details": "No additional details provided, Check Logs.",
        "isOperational": true,
        "name": "MongooseError"
      }
    }

The database is connected.



Solution 1:[1]

usually the error will happen when database is not connected. Try checking whether test environment setup uses the same url to DB as well as db connection code (e.g., mongoose.connect(...) ) executed as part of test loading flow.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 basdanny