从shell脚本运行jest测试-失败会导致脚本退出



我有一个普通的package.json,它使用npm test命令运行jest测试。包.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循环。一个笑话测试失败的例子如下:

 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"
  }
}

对"find"命令的所有结果执行"npm test"命令。"发现"并不真正关心测试是否成功完成。据我所知,一旦"npm test"命令失败,一切都应该停止。以下是一些你可以使用的代码,而不是"查找":

for D in $(find "$parent_dir" -maxdepth 2 -name "package.json"); do
    ( cd ${F%/*} && npm test ) || exit $?
done

最新更新