【问题标题】:Jest did not exit one second after the test run has completed (with Nuxt and Jest)测试运行完成后一秒钟 Jest 没有退出(使用 Nuxt 和 Jest)
【发布时间】:2023-03-13 05:00:01
【问题描述】:

由于未知原因,我的 Jest 测试似乎在我的 CI 结束时通过 Travis 阻塞。

Travis 日志如下:

测试套件:5 个通过,总共 5 个
测试:31 次通过,总共 31 次
快照:共 0 个
时间:21.993s

Jest 在测试运行完成后一秒没有退出。

这通常意味着在您的测试中存在未停止的异步操作。考虑使用 --detectOpenHandles 运行 Jest 来解决此问题。

请注意,--detectOpenHandles 不显示任何内容。

您可以看到我的测试通过但没有退出,即使我执行了以下操作:

describe('[INTEGRATION] Index.test.js', () => {
  beforeAll(async () => {
    const rootDir = resolve(__dirname, '../..');
    let config = {};
    // ... config

    nuxt = new Nuxt(config);
    new Builder(nuxt).build();
    await nuxt.listen(3000, 'localhost');
    homePage = await nuxt.renderAndGetWindow('http://localhost:3000/');
  }, 30000);

  // This is called after every suite (even during the CI process)
  afterAll(() => {
    nuxt.close();
  });

  // ... my tests
});

我的测试在本地运行良好,但仅在 CI 过程中通过 Travis 进行。

这是我的 Jest 配置文件的内容:

module.exports = {
  verbose: true,
  moduleFileExtensions: ['js', 'vue', 'json'],
  transform: {
    '^.+\\.js$': 'babel-jest',
    '.*\\.(vue)$': 'jest-vue-preprocessor',
  },
  setupTestFrameworkScriptFile: './jest.setup.js',
  silent: true,
};

jest.setup.js 包含 jest.setTimeout(30000);。

最后,这是我的.travis.yml 配置文件:

language: 'node_js'
node_js: '8'

cache:
  directories:
    - 'node_modules'

before_script:
  - npm run build
  - npm run lint
  - npm install
  - npm run generate

什么可能导致这个问题?超时不应该是它,因为它是执行我的所有集成测试所必需的,我在集成测试套件之后关闭了我的nuxtsession。

从昨天开始工作到今天,没有发生重大变化。

【问题讨论】:

    标签: travis-ci jestjs nuxt.js


    【解决方案1】:

    我认为您可能需要确保等待nuxt.close() 呼叫解决。 It looks like close is asynchronous 并发出一个承诺,当连接实际关闭时解决。所以 Jest 可能会在异步操作完成之前完成。这确实是一个时间问题,因此 CI 机器的运行方式可能略有不同,导致 close 调用比在本地机器上花费的时间更长。

    尝试将您的 afterAll 更改为以下内容:

    afterAll(async () => { await nuxt.close(); });

    【讨论】:

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