npm-failsafe lets you execute a sequence of NPM scripts and return the correct exit code
should any of them fail.
npm install --save-dev npm-failsafe
In your project:
You should use this to run only scripts which are defined in your
Ideally, you should also provide all args for these scripts upfront, due to the order in which scripts/args are interpreted:
Each script will be executed with
Best practice use case
In the above example, you can see that args for each script on which
failsafe will run have been provided upfront.
package.json with the following scripts defined:
"scripts":"preintegration": "bin/start_the_server.sh""integration": "bin/run_some_tests_that_require_the_server.sh""cleanup": "bin/shutdown_the_server.sh"
In this example, we want to execute the
The script runs some integration tests against some server,
which means that we need to start the server up before the tests and shut it
The server itself is started in the
(check out the node docs to learn more about the
The question is: how do we shut it down?
We could add the following
test script to our
"test": "integration && cleanup".
The problem with this is that because of how the
&& operator works, the
will only get executed when the
integration script succeeds. This is no good because we need
to shut down the server even if the
integration tests fail.
We could try to use the
|| operator instead, which executes the second script no matter the result
of the first one:
"test": "integration || cleanup".
However, the problem with this approach is that the exit code of the
"integration || cleanup"
combo will always take the value of
0, incorrectly indicating that the
test script has succeeded.
This could for example cause a continuous integration server to publish
your project even if the tests have failed...
npm-failsafe you can execute a sequence of arbitrary NPM scripts and return the correct exit code
should any of them fail:
"scripts":"preintegration": "bin/start_the_server.sh""integration": "bin/run_some_tests_that_require_the_server.sh""cleanup": "bin/shutdown_the_server.sh""test": "failsafe integration cleanup"
Your feedback matters!
Did you find this project useful? Give it a star on github! ★
Found a bug? Raise an issue or submit a pull request.
Have feedback? Let me know on twitter: @JanMolak