【发布时间】:2020-12-13 19:28:58
【问题描述】:
我有以下代码
const https = require("https");
it("wait for some result", function (done) {
this.timeout(15000);
const options = {
hostname: "httpbin.org",
path: "/get",
headers: {
Authorization: "bearer ",
},
};
https.get(options, (resp) => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", (chunk) => {
data += chunk;
console.log(data);
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
console.log(JSON.parse(data).explanation);
});
resp.on("error", (err) => {
console.log("Error: " + err.message);
});
done();
});
});
返回:
- 应在帖子中使用重定向响应: 错误:超过 15000 毫秒的超时。确保在此测试中调用了 done() 回调。
由于 mocha,我试图使这个异步,这就是我使用回调的原因。我什至在等待 15000 而不是默认的 2000ms。
此代码正常工作,只有 mocha 失败。 不知道如何解决这个问题。任何帮助表示赞赏。 提前致谢。
【问题讨论】:
-
你试过把
done放到resp.on("end")回调中吗? -
@Yevhenii 是的,我得到了同样的结果
标签: javascript node.js rest https mocha.js