简体   繁体   中英

NodeJs express MongoDB with jest error while executing test case

Previous Question Link
Open Question

Scenario

I've trying to test if my route for GET endpoint work or not which I've set correctly and I've tested by running server. But my test case gives me following error

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

I've searched a bit and have tried all possible solutions which are stated but it still gives me same error.

Code

const request = require ('supertest');
const app = require ('../../app');
const db = require ('../../db.js');
const url = process.env.MONGO_URI || 'mongodb://localhost:27017'

beforeAll (done => {
  db.connect (url, err => {
    if (err) {
      console.log ('Unable to connect', err);
      process.exit (1);
    }else{
        console.log('Succesfully connected')
    }
  });
});

afterAll (done => {
  db.close ();
});


test ('should response the GET method',done => {
    const res = request (app).get ('/expense');
    return res
      .then (json => {
        console.log ("Length",json.body.length);
        expect (json.body.length).toBe (1, done ());
      })
      .catch (err => {});
  },10000);

Test Output

 ● Console

    console.log test/express/startupTest.test.js:12
      Succesfully connected
    console.log test/express/startupTest.test.js:26
      Length 1

  ● should response the GET method

    Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

      at pTimeout (node_modules/jest-jasmine2/build/queueRunner.js:53:21)
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:523:19)
      at ontimeout (timers.js:469:11)
      at tryOnTimeout (timers.js:304:5)
      at Timer.listOnTimeout (timers.js:264:5)

Test Suites: 1 failed, 2 passed, 3 total
Tests:       1 failed, 6 passed, 7 total
Snapshots:   1 passed, 1 total
Time:        6.58s

You need to invoke done callback after establishing connection with the DB.

beforeAll (done => {
  db.connect (url, err => {
    if (err) {
      console.log ('Unable to connect', err);
      process.exit (1);
    }else{
      console.log('Succesfully connected');
      done();
    }
  });
});

Same with afterAll :

afterAll (done => {
  db.close (() => done());
});

Also, you don't need to use done callback in test case, since you are returning a promise:

test ('should response the GET method', () => {
  const res = request (app).get ('/expense');
  return res
    .then (json => {
      console.log ("Length",json.body.length);
      expect (json.body.length).toBe (1);
    })
    .catch (err => {});
});

When you return a promise from a test case, test resolution will be delayed until the promise resolves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM