node.js - 使用 Mocha 时出现超时错误

标签 node.js mocha.js es6-promise

在使用 Mocha 进行测试时,我在运行 server.test.js 时遇到以下错误

1) "before each" hook for "should get all todos":
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

server.test.js

const expect = require('expect');
const request =  require('supertest');

const {app} = require('./../server');
const {Todo} = require('./../todos');


const todos = [
{
    text: 'This is text 1'
},
{
    text: 'This is text 2'
}
];


beforeEach((done) => {

Todo.remove({}).then(() => {
    return Todo.insertMany(todos);
}).then(() => done());
});


describe('GET /todos', () => {
it('should get all todos', (done) => {

    request(app)
        .get('/todos')
        .expect(200)
        .expect(res => {
            expect(res.body.length).toBe(2);
        })
        .end(done);
});
});

但是如果我在 beforeEach() 方法中做一些改变,比如:

更新了 server.test.js

const expect = require('expect');
const request =  require('supertest');

const {app} = require('./../server');
const {Todo} = require('./../todos');


const todos = [
{
    text: 'This is text 1'
},
{
    text: 'This is text 2'
}
];

beforeEach((done) => {

Todo.remove({}).then(() => {
    Todo.insertMany(todos);
    done();
})
});


describe('GET /todos', () => {
it('should get all todos', (done) => {

    request(app)
        .get('/todos')
        .expect(200)
        .expect(
            expect(res.body.length).toBe(2);
        })
        .end(done);
});
});

然后我就没有错误了。基本上,通过在 beforeEach() 方法中链接 promise ,我遇到了一个错误,但没有它一切都很好。 谁能解释为什么会这样?

server.js

var express = require('express');
var body_parser = require('body-parser');

const {mongoose} = require('./mongoose.js');
const {Todo} = require('./todos');
const {Todo_1} = require('./todos');

var app = express();

app.use(body_parser.json());



//  using GET method
app.get('/todos', (req, res) => {
Todo.find().then((todos) => {
    res.send(todos);
}, (err) => {
    res.status(400).send(err);
});
});

module.exports = {app}

app.listen(3000, () => {
console.log('Server is up on the port 3000');

})

最佳答案

这是处理 promise 的不正确方式:

Todo.remove({}).then(() => {
    Todo.insertMany(todos);
    done();
})
});

Todo.insertMany 可能是异步的并返回一个 promise ,并且它与 promise 链分离。如果有错误,它们将导致未处理的 promise 拒绝,并且由于测试依赖于插入的行,这将导致竞争条件:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

意思就是它所说的。 done 要么未被调用,要么超时。 done 可能永远不会被调用,因为没有处理错误。它应该是 .then(done, done)

由于Mocha支持promises,正确的做法是:

beforeEach(() => 
  Todo.remove({})
  .then(() => Todo.insertMany(todos))
);

出现在 promise 链中某处的每个 promise 都应该被返回。

关于node.js - 使用 Mocha 时出现超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910007/

相关文章:

javascript - 如何在 JavaScript 中 stub 库函数

javascript - 使用 JS Promise 或 Generator 并行执行最多 N 个任务

Firebase V9 离线时不会在 catch 中给出错误

javascript - array.map() 内的 Sails Waterline ORM 查询

javascript - 给定一个字符串格式的函数,我如何将它转换回 javascript 中的函数?

node.js - 如何在 Mongoose 中执行查找查询?

node.js - 如何使用不同的 mongo 数据库设置不同的 node.js 环境?

typescript - 如何运行用 tsx 编写的 Mocha 测试?

node.js - 我应该在/usr/lib 还是/usr/local 中安装 Meteor 的 npm 模块?

node.js - aws-sdk 上传到 S3 在 KOA 中可以工作,但通过 Mocha 调用时会停止工作