我正在尝试学习使用节点的测试,因为我正在使用Mocha框架。
我正在尝试向快递服务器发出get请求,执行两个数字的计算并将其作为{result: sum}
结构化对象返回。
但是,我收到一个UnhandledPromiseRejectionWarning,原因是:套接字挂断了。我已经进行了一些研究,到目前为止,我的结论是,当客户端在服务器发送响应之前停止监听请求发送的响应时,服务器上将引发错误。 (如果有错,我不介意进行澄清)。
这是我的摩卡测试:
const expect = require("chai").expect
const PORT = require("../server").PORT
const app = require("../server").app
const fetch = require("node-fetch")
const BASE_URL = `https://localhost:${PORT}/api/calc/`
let httpServer;
describe("testing endpoints for calculator Rest API", function () {
before("starting the server..", function(done) {
httpServer = app.listen(PORT)
console.log("server is started")
done()
})
describe("testing the add endpoint", function(){
console.log("testing the API now!")
it("add endpoints result should evaluate to 10", function(){
fetch(`${BASE_URL}add/5/5`)
.then(response => expect(response.result).to.be(10))
})
})
after("shutting down the server..", function(){
httpServer.close()
})
})
这是我的堆栈跟踪:
jonas@jonas:~/Desktop/testdemo1$ mocha
testing the API now!
testing endpoints for calculator Rest API
starting server..
server started
testing the add endpoint
✓ add endpoints result should evaluate to 10
(node:1949) UnhandledPromiseRejectionWarning: FetchError: request to https://localhost:1234/api/calc/add/5/5 failed, reason: socket hang up
at ClientRequest.<anonymous> (/home/jonas/Desktop/testdemo1/node_modules/node-fetch/lib/index.js:1444:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:1949) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1949) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
1 passing (55ms)
测试通过了,但是我得到了这么长时间的警告,这是什么意思,我该如何消除呢?
编辑
describe("testing endpoints for calculator Rest API", function () {
before("starting the server..", function(done) {
httpServer = app.listen(PORT, done)
console.log(`server listening on port:${PORT}...`)
})
describe("testing the add endpoint", function(done){
it("add endpoints result should evaluate to 10", function(){
fetch(`${BASE_URL}add/5/5`).then(response => expect(response.result).to.be(10)).then(done).catch(done)
})
})
给出stacktrace:
✓ add endpoints result should evaluate to 10
(node:27175) UnhandledPromiseRejectionWarning: FetchError: request to https://localhost:1234/api/calc/add/5/5 failed, reason: socket hang up
at ClientRequest.<anonymous> (/home/jonas/Desktop/testdemo1/node_modules/node-fetch/lib/index.js:1444:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:27175) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:27175) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
答案 0 :(得分:0)
测试完成后,您必须在测试中的回调中调用done()
。 done
函数将传递给您在it
块中使用的回调。另外,您应该确保始终捕获错误https://mochajs.org/#asynchronous-code
const expect = require("chai").expect
const PORT = require("../server").PORT
const app = require("../server").app
const fetch = require("node-fetch")
const BASE_URL = `https://localhost:${PORT}/api/calc/`
let httpServer;
describe("testing endpoints for calculator Rest API", function () {
before("starting the server..", function(done) {
httpServer = app.listen(PORT)
console.log("server is started")
done()
})
describe("testing the add endpoint", function(){
console.log("testing the API now!")
it("add endpoints result should evaluate to 10", function(done){
fetch(`${BASE_URL}add/5/5`)
.then(response => {
expect(response.result).to.be(10)
done();
}).catch(e => done(e)) // or just .catch(done)
})
})
after("shutting down the server..", function(){
httpServer.close()
})
})
答案 1 :(得分:0)
您需要在异步操作完成之后调用done
,当前您正在调用done
,直到http服务器有机会启动之前,并且在关闭http服务器时永远都不会调用完成。您只需将done
传递给所有异步回调处理程序即可。
before("starting the server..", function (done) {
httpServer = app.listen(PORT, done) // <- here
})
describe("testing the add endpoint", function () {
console.log("testing the API now!")
it("add endpoints result should evaluate to 10", function (done) {
fetch(`${BASE_URL}add/5/5`)
.then(response => {
expect(response.result).to.be(10)
})
.then(done) // <- here
.catch(done) // <- here
})
})
after("shutting down the server..", function (done) {
httpServer.close(done) // <- here
})
如您在此处看到的,我尚未将then
语句附加到it
函数中。
fetch(`${BASE_URL}add/5/5`).then(response => expect(response.result).to.be(10)).then(done).catch(done)
如果这不能解决您的问题,则/add/5/5
的路由可能存在问题。