当我运行以下测试时:
afterAll(async () => {
await runDbBuild();
await pool.end();
});
describe("queries.newteetypes.select.all():", () => {
test("Test 1: object keys ", async () => {
const awaitedResponse = await queries.newteetypes.select.all();
expect(awaitedResponse).toStrictEqual(anticipatedResponse);
});
});
I get this error: Jest has detected the following 1 open handle potentially keeping Jest from exiting:
上面显示的代码在生产中工作得非常好。问题仅在于jest
给我一个潜在的打开句柄警告。
我可以通过引入延迟来修复它setTimeout()
and Promise
,但这个位置让我感到困惑。这是解决方案(相同的代码,但在查询调用上方添加了一行):
afterAll(async () => {
await runDbBuild();
await pool.end();
});
describe("queries.newteetypes.select.all():", () => {
test("Test 1: object keys ", async () => {
await new Promise((resolve) => setTimeout(() => resolve(), 2000)); // Putting a delay before the query solves the problem, but putting the delay after the query doesn't resolve it.
const awaitedResponse = await queries.newteetypes.select.all();
expect(awaitedResponse).toStrictEqual(anticipatedResponse);
});
});
这对我来说似乎非常违反直觉。最初我尝试将超时设置为afterAll()
函数,但无论我将其包含在哪里都没有任何区别。然后我尝试将其放在查询调用之后,在test()
功能,但这不起作用。当我把它放在before查询调用后,我停止收到错误。但为什么这会有所不同呢?