测试运行完成后一秒钟,Jest 没有退出。 --detectOpenHandles

Posted

技术标签:

【中文标题】测试运行完成后一秒钟,Jest 没有退出。 --detectOpenHandles【英文标题】:Jest did not exit one second after the test run has completed. --detectOpenHandles 【发布时间】:2019-02-05 19:41:30 【问题描述】:

我将测试运行 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
  );

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

谢谢

【问题讨论】:

【参考方案1】:

与用户的问题无关,但仍然导致标题中的问题:

testEnvironment: 'node',testEnvironment: 'jsdom', 中的 jest.config.js 似乎解决了这个问题。

【讨论】:

【参考方案2】:

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

afterAll(() => 
  return connection.close(); // connection.close() returns a promise
);

Docs for reference

【讨论】:

以上是关于测试运行完成后一秒钟,Jest 没有退出。 --detectOpenHandles的主要内容,如果未能解决你的问题,请参考以下文章