Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm posttest doesn't trigger if npm test fails

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.

like image 878
jimm101 Avatar asked Aug 13 '14 17:08

jimm101


People also ask

What is the use of npm test command?

The npm test command is used to test a package. This command runs the test script that is contained in a package, if you provided one.

Is it npm test or npm run test?

TL;DR there is no difference. It's just a shortcut for npm tests which run the test command in the package. json file. npm run test performs the same action in this case.

How do I run a specific test in npm?

In order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli . Then simply run your specific test with jest bar.


1 Answers

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"
}
like image 99
jimm101 Avatar answered Oct 05 '22 16:10

jimm101