测试运行完成后,Jest 一秒钟都没有退出。--检测打开句柄

Web*_*per 5 apollo jestjs graphql apollo-server

我将测试使用 Jest 运行的后端服务器。有时会成功,但有时会显示这样的错误。 在此输入图像描述

因此,如果我按照建议使用 --detectOpenHandles 标志,它总是成功而不显示任何错误。这是测试代码。

  it("should be able to initialize a server (development)",async (done) => {
      // Before main() is called there is no active connection:

    expect(connection.readyState).toBe(0);
    return main({
      env: "dev",
      port: PORT,
    })
    .then(async (server: ApolloServer) => {
        // After main() got called, there is an active connection:
      expect(connection.readyState).toBe(1);
      await server.stop();
      done();
    })
  });
  afterAll(async () => {
    await connection.close(); //connection is mongoose.connection
  });
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它在标记时失败。奇怪的是,有时成功,有时失败。

谢谢

Div*_*ani 0

我遇到了类似的问题,并通过在传递给 的函数中返回一个承诺来解决它afterAll。例如:

afterAll(() => {
  return connection.close(); // connection.close() returns a promise
});
Run Code Online (Sandbox Code Playgroud)

参考文档