Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJs在执行测试用例时使用jest错误表示MongoDB_Javascript_Node.js_Express_Jasmine_Jestjs - Fatal编程技术网

Javascript NodeJs在执行测试用例时使用jest错误表示MongoDB

Javascript NodeJs在执行测试用例时使用jest错误表示MongoDB,javascript,node.js,express,jasmine,jestjs,Javascript,Node.js,Express,Jasmine,Jestjs,上一个问题链接 场景 我试图测试我的获取端点的路由是否正确,我已经通过运行服务器进行了测试。但是我的测试用例给了我以下错误 Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 我已经搜索了一些,并尝试了所有可能的解决方案,但它仍然给我同样的错误 代码 const request = require ('supertest'); const

上一个问题链接

场景

我试图测试我的获取端点的路由是否正确,我已经通过运行服务器进行了测试。但是我的测试用例给了我以下错误

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
我已经搜索了一些,并尝试了所有可能的解决方案,但它仍然给我同样的错误

代码

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);
测试输出

 ● 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

在与数据库建立连接后,需要调用
done
回调

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

afterAll (done => {
  db.close (() => done());
});
另外,您不需要在测试用例中使用
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 => {});
});

当您从测试用例返回承诺时,测试解决将延迟到承诺解决为止。

谢谢,它解决了我的问题,我认为我编写的测试用例给了我问题,所以我在测试用例中使用了done。该死,这些错误有时会误导人。再次感谢你。