我有一个简单的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()
,它会正确关闭数据库连接,并且在测试完成后会开玩笑地终止。
我的问题是close
和end
在功能上是否不一样?为什么一个关闭连接,而另一个却不呢?
答案 0 :(得分:0)
您的end
函数只是执行pool.end
而没有作为承诺返回它,因此它并不相似。
为了获得更好的可视化效果,您当前的代码基本上是在这样做:
function close() {
return pool.end()
}
function end() {
pool.end()
}
module.exports = {
end,
close
};