16

I have a shell script that recursively searches for package.json from a base directory and runs npm test like so.

find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;

I want to intercept tests failure in the form of:

npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

So I can have the shell script exit with an error status.

Any help is appreciated.

4
  • Have similar quesiton ... Commented Sep 9, 2015 at 19:23
  • just found this question, which is similar to mine. Two years and no answers... Commented Jun 3, 2018 at 0:13
  • There is a related question that may solve this problem here stackoverflow.com/questions/17830326/… Commented Sep 2, 2020 at 10:55
  • Does this answer your question? Ignoring specific errors in a shell script Commented Sep 2, 2020 at 11:04

2 Answers 2

7

Another 3 year late and was looking for solution in this too.

Finally found it and if these help others in future. I used shell script $? command, which returns the exit status of the last executed command. I.e. if a shell script fails it returns 1 while a 0 if it's success. NPM implements this return hence.

npm run test
if [ $? -eq 0 ]
then
  echo "SUCCESS"
else
  echo "FAIL"
fi
Sign up to request clarification or add additional context in comments.

1 Comment

This did not worked for me. The script kept breaking at the line of the test. I finally solved with the answer from this question: stackoverflow.com/questions/17830326/…
1

If I understood you correctly you want to execute something or stop execution of script if npm ERR! has been thrown? This might help:

 if ./somecommand | grep -q 'npm ERR!'; then
      #what you want to do
      # exit executing the script
      return
    fi

Sorry what I am two years late, just joined the SO community.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.