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

get*_*kin 5 integration-testing mocha.js node.js chai supertest

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

\n
\n
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)\n
Run Code Online (Sandbox Code Playgroud)\n
\n
   let invalid = 'something_invalid';\n\n    it('With valid appid', (done) => {\n        request(server).get(`/game/info/${valid}`)\n            .then((err, res) => {\n\n                let json = res.body;\n\n                expect(200);\n                expect(json.name).to.equal("Rust");\n                done();\n            }).catch(err => console.log(err))\n    });\n
Run Code Online (Sandbox Code Playgroud)\n

其中响应是:

\n
{\n    "name": "Rust",\n    "appid": 252490,\n    "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.",\n    "publishers": [\n        "Facepunch Studios"\n    ],\n    "price_text": "\xe2\x82\xac33.99",\n    "platforms": {\n        "windows": true,\n        "mac": true,\n        "linux": false\n    },\n    "likes": 374668\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我到处都找过了,但问题的解决方案从未解决它。\n知道我做错了什么吗?

\n

kok*_*478 6

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

mocha --timeout 10000
Run Code Online (Sandbox Code Playgroud)

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

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

  it('...', function(done){
    this.timeout(10000);
    setTimeout(done, 10000);
  });
});
Run Code Online (Sandbox Code Playgroud)