Github Actions集成Jest测试覆盖率任务突然终止求助
Github Actions TEST-COVERAGE 任务执行失败排查求助
问题概述
本地使用Jest执行所有测试用例均通过,但Github Actions执行TEST-COVERAGE任务时突然无输出终止,返回Error: Process completed with exit code 1。最初将npm install替换为npm ci后问题恢复,但新增功能扩展后问题再次出现。
CI任务配置
name: TEST-COVERAGE on: push: branches: - main - dev pull_request: branches: - main - dev jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] steps: - name: Checkout source code uses: actions/checkout@v3 - name: Set up Nest.js uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - name: Set up Node_modules run: | cd ./client rm -rf node_modules yarn install --immutable --immutable-cache --check-cache cd .. cd ./server/gateway rm -rf node_modules npm ci cd ../services/core rm -rf node_modules npm ci cd ../auth rm -rf node_modules npm ci cd ../ticket rm -rf node_modules npm ci - name: Run Test Coverage run: | rm -rf node_modules npm ci npm run coveralls - name: Coveralls uses: coverallsapp/github-action@master with: github-token: ${{ secrets.GITHUB_TOKEN }}
项目目录结构
- client
- server
- gateway
- auth
- core
- ticket
package.json 脚本配置
"scripts": { "test": "jest --detectOpenHandles --forceExit", "coverage": "jest --detectOpenHandles --coverage --forceExit", "coveralls": "jest --coverage --detectOpenHandles --forceExit && coveralls < ./coverage/lcov.info" },
排查建议
- 增加执行日志:拆分
Run Test Coverage步骤的命令,分步执行并添加日志输出,定位具体失败环节:rm -rf node_modules npm ci echo "当前工作目录: $(pwd)" npm run coverage echo "覆盖率文件检查:" ls -la ./coverage coveralls < ./coverage/lcov.info - 限制Jest并发数:Github Actions runner内存有限,大量测试可能触发OOM。在
jest.config.js中添加:
或者直接修改脚本:module.exports = { // ...其他配置 maxWorkers: 2, // 根据runner核心数调整,默认是CPU核心数 };"coveralls": "jest --coverage --detectOpenHandles --forceExit --maxWorkers=2 && coveralls < ./coverage/lcov.info" - 添加Node内存参数:给Jest进程增加内存限制,避免内存溢出:
"coveralls": "NODE_OPTIONS=--max-old-space-size=4096 jest --coverage --detectOpenHandles --forceExit && coveralls < ./coverage/lcov.info" - 验证依赖一致性:确保所有子项目的
package-lock.json(或yarn.lock)已提交到仓库,npm ci会严格按照锁文件安装依赖,避免版本不一致导致的问题。 - 检查CI环境变量:确认测试用例依赖的环境变量是否在Github Actions中配置完整,部分本地测试通过但CI失败的情况是因为环境变量缺失。
- 开启CI调试日志:在CI任务中添加环境变量
ACTIONS_STEP_DEBUG: true,获取更详细的执行日志,排查隐藏的报错信息。
内容的提问来源于stack exchange,提问作者Kim Jinsung
相关产品推荐
相关产品推荐

