npm posttest doesn't trigger if npm test fails

大兔子大兔子 提交于 2020-08-02 03:15:34

问题


Is there a way to trigger npm's posttest when test fails? If package.json contains

"scripts": {
  "pretest": "echo pretest",
  "test": "some_failed_test_or_error",
  "posttest": "echo posttest"
}

$ npm test will echo "pretest" but not "posttest".

I get the same behavior if I use mocha and a test fails, even if mocha doesn't trigger any exceptions (just some simple failure assert(true==false)).

I'm launching a resource on pretest, and I'd like to kill the resource on posttest, whether the test itself passes or fails.

MacOS OS X 10.9.4, npm version 1.4.21, node v0.10.30.


回答1:


Worked it out. Not sure if the following will work in Windows. The bash || operator followed by an empty comment : changes the exit code.

For instance, using mocha:

"scripts": {
  "pretest": "echo pretest",
  "test": "mocha || :",
  "posttest": "echo posttest"
}



回答2:


The other answer to this question is neat and short, but is a little unorthodox. I know that if I used it a big questionmark would form over my head every time I came back and saw " || : ". It's so concise you might even miss it. It would need documentation somewhere.

I spent a little time looking for a more orthodox solution and found it here: https://github.com/npm/npm/issues/5493

You need npm-run-all

"test": "npm-run-all the-actual-test run-after-test-even-if-failed --continue-on-error"

However you cannot use "pretest" or "posttest" names for the pre and post scripts, because they would run twice.



来源:https://stackoverflow.com/questions/25292344/npm-posttest-doesnt-trigger-if-npm-test-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!