You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jest工作进程未优雅退出警告,寻求排查与解决方法

解决Jest工作进程无法优雅退出的警告问题

问题重现

添加tests/users_api.test.js文件后,Jest抛出以下警告:

A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.

已尝试以下方法但无效:

  • 添加teardown.js并配置globalTeardown
  • 设置FORCE_EXIT_DELAY
  • 使用--runInBand选项运行测试
  • 运行--detectOpenHandles未产生额外输出

仓库为fullstackopen-bloglist的92e4538ff1ba6cc9caa99f35fc501be67bc4e224版本。

排查与解决方案

1. 检查测试中的服务器关闭逻辑

确保users_api.test.js中,每个测试完成后都显式关闭Express服务器实例:

// 假设server是从app.js导出的Express服务器实例
const server = require('../app').server;

afterEach(async () => {
  await new Promise(resolve => server.close(resolve));
});

如果服务器启动时没有保存实例(比如直接app.listen()而不赋值),需要修改app.js导出服务器实例:

// app.js
const app = express();
// ... 其他配置
const server = app.listen(process.env.PORT || 3001);
module.exports = { app, server };

2. 完善MongoDB连接的清理

在全局teardown文件中确保异步关闭MongoDB连接:

// teardown.js
const mongoose = require('mongoose');

module.exports = async () => {
  await mongoose.connection.close();
};

同时在jest.config.js中确认globalTeardown路径配置正确:

module.exports = {
  // ... 其他配置
  globalTeardown: './teardown.js'
};

3. 清理未处理的定时器

检查测试代码及业务代码中是否存在未清理的setTimeout/setInterval,在测试结束前显式清除:

afterEach(() => {
  jest.clearAllTimers();
});

如果使用了第三方库创建的定时器,确保调用其提供的清理方法,或调用.unref()让定时器不阻塞进程退出。

4. 确保异步操作全部完成

检查users_api.test.js中的所有测试,确保异步操作都通过async/await或done回调处理完成,避免测试提前结束导致资源泄漏:

test('创建用户成功', async () => {
  const response = await api
    .post('/api/users')
    .send(newUser);
  
  expect(response.status).toBe(201);
  // 所有断言和异步操作都要等待完成
});

5. 显式全局清理资源

在所有测试文件的afterAll中统一清理资源:

afterAll(async () => {
  await mongoose.connection.close();
  await new Promise(resolve => server.close(resolve));
});

内容的提问来源于stack exchange,提问作者cheryllium

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:56:01