How to test mongoose connection with Jest ? How to mock jest?

Viewed 83

I am trying to test plain express app with mongoose connection on app.ts itself.

import mongoose, { Mongoose } from 'mongoose';
import appMock from '../src/app';
import '../src/server'
jest.mock('../src/app')

jest.mock('mongoose', () => ({
    connect: jest.fn().mockImplementation(
      (uri:any, options:any)=>  {
          return  Promise.resolve({});
      }),
      Schema:jest.fn(),
      model:jest.fn()
  }))

describe("index.js - app entry", () => {
    it("should call app.listen()", (done) => {
        expect(appMock.listen).toHaveBeenCalled();
    });
});
 FAIL  __tests__/server.spec.ts (6.077 s)
  index.js - app entry
    ✕ should call app.listen() (5003 ms)

  ● index.js - app entry › should call app.listen()

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      14 |
      15 | describe("index.js - app entry", () => {
    > 16 |     it("should call app.listen()", (done) => {
         |     ^
      17 |         expect(appMock.listen).toHaveBeenCalled();
      18 |     });
      19 | });

      at __tests__/server.spec.ts:16:5
      at Object.<anonymous> (__tests__/server.spec.ts:15:1)

Even setting timer with jest.fakeTimer() I see same error. Appreciate help ..

0 Answers
Related