i_trope
i_trope

Reputation: 1604

Catch npm ERR! when running npm test from shell script

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.

Upvotes: 16

Views: 6479

Answers (2)

Han
Han

Reputation: 758

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

Upvotes: 7

Delmys Asteroraena
Delmys Asteroraena

Reputation: 35

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.

Upvotes: 1

Related Questions