Node.js Mocha节点HTTP测试-套接字挂起
我正在尝试学习使用node进行测试,因为我正在使用Mocha框架 我试图向express服务器发出get请求,执行两个数字的计算,并将其作为Node.js Mocha节点HTTP测试-套接字挂起,node.js,sockets,promise,mocha.js,Node.js,Sockets,Promise,Mocha.js,我正在尝试学习使用node进行测试,因为我正在使用Mocha框架 我试图向express服务器发出get请求,执行两个数字的计算,并将其作为{result:sum}结构化对象返回 然而,我得到一个未处理的Promisejection警告,原因是:套接字挂起。我已经做了一些研究,到目前为止,我的结论是,当客户端在服务器发送响应之前停止侦听请求发送的响应时,会在服务器上抛出错误。 (如果有错,我不介意澄清) 这是我的摩卡咖啡测试: const expect = require("chai").exp
{result:sum}
结构化对象返回
然而,我得到一个未处理的Promisejection警告,原因是:套接字挂起。我已经做了一些研究,到目前为止,我的结论是,当客户端在服务器发送响应之前停止侦听请求发送的响应时,会在服务器上抛出错误。
(如果有错,我不介意澄清)
这是我的摩卡咖啡测试:
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()
})
})
这是我的stacktrace:
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)
✓ 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.
提供stacktrace:
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)
✓ 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.
✓ 添加端点结果应计算为10
(节点:27175)未经处理的PromisejectionWarning:FetchError:请求https://localhost:1234/api/calc/add/5/5 失败,原因:套接字挂起
在ClientRequest。(/home/jonas/Desktop/testdemo1/node_modules/node fetch/lib/index.js:1444:11)
在emitOne(events.js:116:13)
在ClientRequest.emit(events.js:211:7)
在TLSSocket.socketErrorListener(_http_client.js:387:9)
在emitOne(events.js:116:13)
在TLSSocket.emit(events.js:211:7)
发出错误时(内部/streams/destroy.js:64:8)
at _combinedTickCallback(内部/流程/下一步_tick.js:138:11)
在进程中。_tick回调(内部/process/next_tick.js:180:9)
(节点:27175)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:1)
(节点:27175)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
您必须在测试中调用done()。done
函数被传递给it
块中使用的回调函数。此外,您应该确保始终捕捉错误
异步操作完成后,您需要调用done
,当前您正在调用done
,http服务器尚未启动,并且在关闭http服务器时从未调用done。您只需将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
})
如果这不能解决您的问题,那么it函数中以done()结尾的/add/5/5
的路径可能会出现问题,这很遗憾-无法解决警告:(我有一个完全相同的问题…@Rouz如果您找到解决方案,请发布为答案:)有趣的是,对我来说这不是确定性的。它在随机调用时失败。有时它根本不会失败。我正在尝试创建一个集成测试,所以这真的让我很难受……天哪,我确实解决了我的问题,但它肯定不是你现在遇到的问题。我甚至不认为这是一个值得回答的问题,因为这个问题对我的代码库来说是超特定的(服务器在集成测试的中间重新启动)。很抱歉mate@Rouz哈哈,没关系。不过恭喜你解决了这个问题。我将继续修复:)您无法调用。然后在it函数上?@JonasGrønbek然后
是获取
承诺链的一部分,它没有附加到它
。hotzinger it()不会返回承诺,我已经尝试了您的解决方案,它运行了,但显然没有解决警告问题。我认为您在阅读我提供的代码时遇到了问题。所以我会把它做成一条线…我已经试过了,但它不能解决它,伙计。如果你想解开这个谜,我可以去github。对不起,事实并非如此。
fetch(`${BASE_URL}add/5/5`).then(response => expect(response.result).to.be(10)).then(done).catch(done)