node.js - 如果“测试”脚本失败,纱线“后测”脚本将不起作用
问题描述
好吧,我有这些脚本package.json
用于在 NodeJS 中测试一些代码。
"scripts": {
"pretest": "env NODE_ENV=test sequelize db:migrate",
"test": "jest",
"posttest": "env NODE_ENV=test sequelize db:migrate:undo:all"
}
当测试变得清晰时,“posttest”运行,但是当测试失败时,我收到一个
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
在 VS 代码中。关于该问题的链接没有任何有用的信息,互联网上也没有。
所以我找到了这个关于 NPM 的链接:
https://github.com/npm/npm/issues/5493
那家伙说:
在绝大多数情况下,如果在测试失败后运行后测(例如,如果测试失败,您不希望清理测试环境或发布新版本),用户会感到不快。因此,这种行为不会改变。将诸如 "test":"npm run-script test-failing || npm run-scriptmandatory-cleanup" 之类的内容放入您的 package.json 将为您提供所需的内容。
这并没有解决我的问题。通过更多研究,我发现了这一点:
解决方案对我也不起作用。
那么即使测试失败,我如何运行“posttest”脚本呢?
解决方案
好吧,通过上面的对话,我得到了这个解决方案:
这是我的脚本package.json
:
"scripts": {
"dev": "nodemon src/server.js",
"pretest": "env NODE_ENV=test sequelize db:migrate",
"run-tests": "jest",
"run-after-tests": "env NODE_ENV=test sequelize db:migrate:undo:all",
"test": "npm-run-all run-tests run-after-tests --continue-on-error"
},
我安装了npm-run-all
软件包,run-after-test
即使测试失败,它也会运行脚本。现在我得到了错误
error Command failed with exit code 1.
从test
脚本中,和
ERROR: "test" exited with 1.
error Command failed with exit code 1.
从run-after-test
,但最终我的问题得到了解决。如果有人在脚本末尾有没有错误的更好解决方案,请与我们分享。