bash - 从 shell 脚本运行 Jest 测试 - 失败导致脚本退出

标签 bash shell jenkins npm jestjs

我有一个运行 jest 的普通 package.json使用 npm test 命令进行测试。 package.json 的相关部分如下所示:

{
  "scripts": {
     "test": "jest",
   "jest": {
    "scriptPreprocessor": "../shared/preprocessor.js"
  }
}

shell 脚本如下所示:

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
parent_dir="$script_dir/.."

echo
echo "Running all tests..."
find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;

现在,当 Jest 测试失败时,bash 脚本不会以失败状态退出。我希望这样做是为了用 Jenkins 创建一个 CI 循环。 jest测试失败示例如下:

 FAIL  js/components/__tests__/ExperienceCampaign-tests.js (13.19s)
● ExperienceCampaign › listening to ExperienceCampaignStore › it starts listening when the component is mounted
  - Expected: true toBe: false
        at Spec.<anonymous> (/Users/jonathan/experience-studio/campaign/js/components/__tests__/ExperienceCampaign-tests.js:54:26)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test failed, 0 tests passed (1 total)
Run time: 13.748s
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0 

感谢任何帮助。

最佳答案

jest 有一个 --forceExit 标志,可用于...强制退出 :D。 只需将它添加到您的脚本中即可。

您可能在测试中执行了一些没有及时返回回调的异步操作。可能想仔细看看。

{
  "scripts": {
     "test": "jest --forceExit",
   "jest": {
    "scriptPreprocessor": "../shared/preprocessor.js"
  }
}

关于bash - 从 shell 脚本运行 Jest 测试 - 失败导致脚本退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122598/

相关文章:

Bash 脚本/命令打印出日期之前/之后 5 分钟

linux - Bash 脚本对于不同的结果获得相同的存在状态

jenkins - Hudson - 我该怎么做 "SVN clean up"?

java - maven针对不同的目标设置了哪些classpath?能提供一下官方文档的链接吗?

linux - 在 bash 脚本中解析 .kml 文件

linux - 用于本地(非远程)命令执行的 ssh 隧道

bash - 终端中设置的 globstar 未传播到脚本

linux - 删除输出linux中的字母

android - Activity 创建者

continuous-integration - Jenkins 不执行 Ant 任务