NodeJs testing Error: Timeout of 2000ms exceeded

NodeJs testing Error: Timeout of 2000ms exceeded

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

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);