【问题标题】:Enable uninterrupted sequential command execution on npm ELIFECYCLE error在 npm ELIFECYCLE 错误上启用不间断的顺序命令执行
【发布时间】:2020-04-23 21:47:56
【问题描述】:

我有 jest 和 jest-puppeteer 设置来运行测试。运行测试很简单,使用:

jest test.test --config="path/to/config.json"

但是当我把它放到 package.json 中时:

...
"scripts": {
    "test:jest": "jest test.test --config=\"path/to/config.json\""
},
...

然后运行:

npm run test:jest

测试正常运行,但最后出现以下错误:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test@1.0.0 jest:temp: `jest test.test --config="path/to/config.json"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the test@1.0.0 jest:temp script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/obihill/.npm/_logs/2020-04-23T12_18_23_546Z-debug.log

在做了一些研究后,我发现这是因为某些测试可能会失败。果然,我设置所有测试通过,错误不再出现。

我遇到的问题是我需要运行此测试,然后依次运行第二个测试:

npm run test:jest && npm run test:postjest

但由于测试失败和生成的ELIFECYCLE 代码,第二个命令永远不会运行。经过一番研究,我找到了a way to suppress the errors。所以现在我运行以下命令:

npm run test:jest --silent && npm run test:postjest

并且没有错误,但test:postjest 永远不会运行。所有测试都必须通过 test:jest 才能运行第二个命令。

有没有办法覆盖此行为并使npm run test:postjest 无论如何按顺序运行?

【问题讨论】:

    标签: node.js unit-testing npm jestjs


    【解决方案1】:

    设法找到答案。

    我通过在命令上使用链运算符来在错误时干净地退出来让它工作。

    这是我更新后的package.json

    ...
    "scripts": {
        "test:jest": "jest test.test --config=\"path/to/config.json\" || exit 0"
    },
    ...
    

    || 是 Linux 中的链接运算符。它的工作方式是如果第一个命令 [jest test.test --config=\"path/to/config.json\"] 失败,第二个命令 [exit 0] 将运行。由于第二个命令是成功退出信号[0 == success, 1 == error],所以命令成功退出,可以运行另一个命令。如果第一个命令成功,则跳过exit 0

    我还放弃了 --silent 选项,因为它不再需要。

    如果您遇到同样的问题,希望您能尽快找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-04
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多