javascript - 测试后连接没有关闭?
问题描述
我有一个简单的 postgres.js 包装文件。
const pg = require("pg")
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
function close() {
return pool.end()
}
module.exports = {
end: pool.end,
close
};
运行一个利用上述 postgres 库的笑话测试用例,如下所示:
const postgres = require("./path/to/postgres.js");
describe("Foo", () => {
afterAll(() => {
return postgres.end();
})
it(...)
});
将产生“这通常意味着在您的测试中没有停止异步操作。” 错误消息并挂在那里。
但是,如果我将行更改postgres.end()
为postgres.close()
,它会正确关闭数据库连接并在测试完成后终止 jest。
我的问题是它在功能上不做同样close
的end
事情吗?为什么一个关闭连接而另一个没有?
解决方案
你的end
函数只是执行pool.end
而不是作为一个承诺返回它,所以它不相似。为了更好的可视化,您当前的代码基本上是这样做的:
function close() {
return pool.end()
}
function end() {
pool.end()
}
module.exports = {
end,
close
};
推荐阅读
- windows - 使用批处理关闭文件资源管理器中所有打开的文件夹
- python - python如何按索引删除子字符串
- php - Laravel Dusk $browser->attach() 机器升级后不再工作
- sql-server - 如何使用掩码字符或下面字符串中的任何其他技巧过滤出年份?
- r - 如何在 R 中使用 ggplot2 绘制平均值的误差线?
- java - Java 中的递归和 While 循环
- c# - 对 2captcha 使用 POST 方法时出错
- javascript - 子组件不是从一个父级的状态更改重新渲染,而是从另一个父级重新渲染?
- arrays - 将 ndgrid 的输出转换为单个数组
- recursion - 不能在简单的递归序言程序中使用 n1。不断收到未定义的程序:n/1