如果 "test" 脚本失败,则 Yarn "posttest" 脚本不起作用

Yarn "posttest" script does not work if "test" script fails

好吧,我的 package.json 中有这些脚本,用于测试 NodeJS 中的一些代码。

"scripts": {
    "pretest": "env NODE_ENV=test sequelize db:migrate",
    "test": "jest",
    "posttest": "env NODE_ENV=test sequelize db:migrate:undo:all"
}

当测试清楚时,“后测试”运行s,但当测试失败时,我收到

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

在 VS 代码中。关于这个问题,link 上没有任何有用的信息,互联网上也没有。

所以我发现了关于 NPM 的link:

https://github.com/npm/npm/issues/5493

那个人说:

In the vast majority of cases, users are going to be unpleasantly surprised if posttest runs after test failures (you don't want your test environment being cleaned up or new versions being published if there were test failures, for instance). As such, this behavior isn't going to change. Putting something like "test":"npm run-script test-failing || npm run-script mandatory-cleanup" into your package.json will give you what you want.

这并没有解决我的问题。通过更多研究,我发现了这一点:

npm posttest doesn't trigger if npm test fails

这些解决方案也不适合我。

那么,即使测试失败,我如何运行“后测试”脚本?

嗯,通过上面的对话,我得到了这个解决方案:

这是我在 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,但最终我的问题得到了解决。如果有人有更好的解决方案并且在脚本末尾没有错误,请与我们分享。