【问题标题】:Jest did not exit one second after the test run has completed. --detectOpenHandles测试运行完成后一秒钟,Jest 没有退出。 --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
  });

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

谢谢

【问题讨论】:

    标签: graphql jestjs apollo apollo-server


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      Docs for reference

      【讨论】:

        猜你喜欢
        • 2018-12-08
        • 2021-03-28
        • 2019-09-16
        • 1970-01-01
        • 2019-05-24
        • 2023-03-13
        • 2019-06-15
        • 2020-08-26
        • 2021-01-13
        相关资源
        最近更新 更多