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

NPM脚本无法顺序执行:e2e测试容器启动后停滞

问题原因

nerdctl compose up默认是前台运行模式,会持续占用当前终端输出容器日志,导致后续通过&&串联的命令完全无法触发执行——这才是脚本停滞的核心原因,和容器启动耗时无关。

修复方案

1. 后台启动容器

给nerdctl compose up加上-d参数,让容器在后台运行,命令执行完成后会自动释放终端,继续执行后续步骤:

"test:e2e": "nerdctl compose up -d && dotenv -e .env.test -- jest --no-cache --config ./test/jest-e2e.json && nerdctl compose down"

2. 等待服务就绪(可选但推荐)

后台启动容器后,容器内的NestJS服务可能还未完全初始化完成,直接执行测试会导致失败。可以添加一段循环检查逻辑,确保服务端口可用后再跑测试(假设服务端口为3000,根据实际情况修改):

"test:e2e": "nerdctl compose up -d && while ! nc -z localhost 3000; do sleep 1; done && dotenv -e .env.test -- jest --no-cache --config ./test/jest-e2e.json && nerdctl compose down"

3. 确保异常时也销毁容器

如果测试过程中失败,上述脚本里的nerdctl compose down不会执行,容器会留在后台。可以用Shell的trap命令捕获退出信号,不管测试成功还是失败,最后都自动销毁容器:

"test:e2e": "trap 'nerdctl compose down' EXIT && nerdctl compose up -d && while ! nc -z localhost 3000; do sleep 1; done && dotenv -e .env.test -- jest --no-cache --config ./test/jest-e2e.json"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 15:30:51