【发布时间】:2020-11-07 21:48:24
【问题描述】:
好吧,我的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 测试失败 || npm run-script 强制清理”进入你的 package.json 会给你你想要的。
这并没有解决我的问题。经过更多研究,我发现了这一点:
npm posttest doesn't trigger if npm test fails
这些解决方案也对我不起作用。
那么,即使测试失败,我该如何运行“posttest”脚本呢?
【问题讨论】:
-
这些解决方案到底有什么不适合的?这个应该适用于你的情况并且可行,stackoverflow.com/a/52073818/3731501。
-
您的意思是
npm-run-all应该可以工作吗?因为它没有。我尝试了所有我能想到的可能性,但都没有奏效。--continue-on-error是 NPM 的一个参数并且也适用于 Yarn 吗? -
是的。 --continue-on-error 在这种情况下是必不可少的。它是 npm-run-all arg,而不是 NPM。 npm-run-all 也支持 Yarn。
-
好吧,我以为
npm-run-all是npm 的原生arg,但它是一个包。根据此页面npmjs.com/package/npm-run-all 有参数npm-run-all和run-s,所以我尝试了"test": "npm-run-all jest posttest --continue-on-error"和"test": "run-s jest posttest --continue-on-error",在这两个中我都得到了ERROR: Task not found: "jest" error Command failed with exit code 1. -
你有什么想法吗?
标签: node.js visual-studio-code jestjs sequelize.js yarnpkg