I have a very simple shell script, test.sh
#!/usr/bin/env bash
exit 1
I'm calling this from my test
run script in package.json
"scripts": {
"test": "test.sh && echo \"unexpected output\""
},
Running npm test
I get this:
$ npm test
> testtest@1.0.0 test C:\Users\[path]\testtest
> test.sh && echo "unexpected output"
"unexpected output"
It seems as if npm doesn't care about the non-zero exit code of test.sh. I was not expecting to see "unexpected output".
How do I make the execution of the "test"
command stop when one of the steps it performs (test.sh
in this case) exits with an error?
With this in my package.json: "test": "test.sh && echo $?",
This is the output:
$ npm test
> testtest@1.0.0 test C:\Users\[path]\testtest
> test.sh && echo $?
$?
With this: "test": "test.sh && echo \"$?\"",
I get this:
$ npm test
> testtest@1.0.0 test C:\Users\[path]\testtest
> test.sh && echo "$?"
"$?"
As a sanity-check I added an echo after the exit in test.sh
. Thankfully it doesn't print :)
#!/usr/bin/env bash
exit 1
echo "This should not print"
The "This should not print" text never shows up in my console.
Actually, adding an echo
before exit 1
also doesn't print anything to my GitBash console. Instead it prints to a temporary cmd window that npm launches. So I'm guessing the exit status is lost inside that cmd session.