简体   繁体   English

NodeJ在执行测试用例时以开玩笑的错误表达MongoDB

[英]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. 我正在尝试测试我的GET终结点路由是否正常工作,而我已经正确设置了该路由,并且已经通过运行服务器进行了测试。 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. 与数据库建立连接后,需要调用done回调。

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相同:

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

Also, you don't need to use done callback in test case, since you are returning a promise: 另外,由于您要返回承诺,因此您无需在测试用例中使用done回调。

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. 当您从测试用例返回承诺时,测试解决方案将延迟到承诺解决为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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