javascript - Jest 检测到以下 1 个打开的句柄可能会阻止 Jest 退出
问题描述
这是我的 HTTP 路由
app.get('/', (req, res) => {
res.status(200).send('Hello World!')
})
app.post('/sample', (req, res) => {
res.status(200).json({
x:1,y:2
});
})
我想测试以下
1)GET
要求工作正常。
2) /sample
响应包含属性x
和y
const request = require('supertest');
const app = require('../app');
describe('Test the root path', () => {
test('It should response the GET method', () => {
return request(app).get('/').expect(200);
});
})
describe('Test the post path', () => {
test('It should response the POST method', (done) => {
return request(app).post('/sample').expect(200).end(err,data=>{
expect(data.body.x).toEqual('1');
});
});
})
但是在运行测试时出现以下错误
Jest 检测到以下 1 个可能阻止 Jest 退出的打开句柄:
返回请求(app).get('/').expect(200);
解决方案
你需要done()
调用end()
方法
const request = require("supertest");
const app = require("../app");
let server = request(app);
it("should return 404", done =>
server
.get("/")
.expect(404)
.end(done);
});
推荐阅读
- c# - 创建 .net Web 服务时出错:无法创建类型“xxxxxxxxxx”
- docker - Docker容器中的IPFS公共节点?
- git - git commit 和 pull request 分开的分支
- node.js - Heroku 构建成功,但应用程序错误
- python - 修改变量(玩家属性)Python 3
- c# - 错误模块名称:ChakraCore.DLL
- android - AVD 停止工作,进程已完成,退出代码为 1
- javascript - YouTube 是如何运作的?
- typescript - 如何在打字稿中专门化泛型类的模板类型?
- javascript - 如何在浏览器中获取 Google Cloud Translation API 的身份验证令牌(chrome 扩展)