12

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.

i_trope
  • 1,452
  • 1
  • 19
  • 37
  • Have similar quesiton ... – Mingtao Zhang Sep 09 '15 at 19:23
  • just found this question, which is similar to mine. Two years and no answers... – Felipe Jun 03 '18 at 00:13
  • There is a related question that may solve this problem here https://stackoverflow.com/questions/17830326/ignoring-specific-errors-in-a-shell-script – EGS Sep 02 '20 at 10:55
  • Does this answer your question? [Ignoring specific errors in a shell script](https://stackoverflow.com/questions/17830326/ignoring-specific-errors-in-a-shell-script) – EGS Sep 02 '20 at 11:04

2 Answers2

4

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
Han
  • 453
  • 3
  • 11
  • 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: https://stackoverflow.com/questions/17830326/ignoring-specific-errors-in-a-shell-script – EGS Sep 02 '20 at 10:57
0

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.