gpt4 book ai didi

node.js - Mocha beforeEach 超时

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:18 25 4
gpt4 key购买 nike

我正在尝试为使用 Express 和 MongoDB 制作的 REST API 设置测试。我想使用 mochachaichai-http 但我遇到了奇怪的行为,似乎 beforeEach 函数它超过了超时,就像它从未被解决一样。我该如何解决这个问题?

//During the test the env variable is set to test
process.env.NODE_ENV = 'test';

let mongoose = require("mongoose");
let User = require('../models/User');

//Require the dev-dependencies
let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('../app');
let should = chai.should();

chai.use(chaiHttp);
//Our parent block
describe('Users', function () {
beforeEach(function (done) { //Before each test we empty the database
User.remove({}, function (err) {
done();
});
});
/*
* Test the /GET route
*/
describe('/GET users', function () {
it('it should GET all the users', function (done) {
chai.request(app)
.get('/users')
.end(function (err, res) {
res.should.have.status(200);
res.body.should.be.a('array');
res.body.length.should.be.eql(0);
done();
});
});
});

});

最佳答案

假设您的连接工作正常并且只是一个超时问题,您需要使用 this.timeout() 函数将超时时间延长到默认值(2000 毫秒)之外。例如,在 beforeEach 函数内添加 this.timeout(10000) 会将超时设置为 10 秒,这将为您的 User.remove 调用提供更多时间来完成。

这是此答案中的一个示例:How to set timeout on before hook in mocha?

关于node.js - Mocha beforeEach 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com