NodeJs测试错误:超时超过2000ms

问题描述

我正在测试多个响应,但总是以相同的错误消息结尾:

Error: Timeout of 2000ms exceeded. For async tests and hooks,ensure "done()" is called; if returning a Promise,ensure it resolves. (/this/file/path.js)
   let invalid = 'something_invalid';

    it('With valid appid',(done) => {
        request(server).get(`/game/info/${valid}`)
            .then((err,res) => {

                let json = res.body;

                expect(200);
                expect(json.name).to.equal("Rust");
                done();
            }).catch(err => console.log(err))
    });

响应为:

{
    "name": "Rust","appid": 252490,"description": "The only aim in Rust is to survive - Overcome struggles such as hunger,thirst and cold. Build a fire. Build a shelter. Kill animals. Protect yourself from other players.","publishers": [
        "Facepunch Studios"
    ],"price_text": "€33.99","platforms": {
        "windows": true,"mac": true,"linux": false
    },"likes": 374668
}

我到处都看过,但是解决问题的方法解决不了。 对我在做什么错有任何想法吗?

解决方法

尝试在运行测试时设置更长的超时时间:

mocha --timeout 10000

或者在每个套件或每个测试中手动进行:

describe('...',function(){
  this.timeout(10000);

  it('...',function(done){
    this.timeout(10000);
    setTimeout(done,10000);
  });
});
,

这不会等待10秒,如果响应在7秒后退出测试用例,请尝试此操作

    it('With valid appid',(done) => {
        request(server).get(`/game/info/${valid}`)
            .then((err,res) => {

                let json = res.body;

                expect(200);
                expect(json.name).to.equal("Rust");
                done();
            }).catch(err => console.log(err))
    },10000);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...