Running jest tests from shell script - results in crashes causing the script to exit

I have a simple package.json that runs jest using a command npm test

. The relevant parts of package.json look like this:

{
  "scripts": {
     "test": "jest",
   "jest": {
    "scriptPreprocessor": "../shared/preprocessor.js"
  }
}

      

The shell script looks like this:

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
parent_dir="$script_dir/.."

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

      

At the moment, when the jest test fails, the bash script does not exit with a failure status. I would like this to be done to create a CI loop with Jenkins. An example of a failed test:

 FAIL  js/components/__tests__/ExperienceCampaign-tests.js (13.19s)
● ExperienceCampaign › listening to ExperienceCampaignStore › it starts listening when the component is mounted
  - Expected: true toBe: false
        at Spec.<anonymous> (/Users/jonathan/experience-studio/campaign/js/components/__tests__/ExperienceCampaign-tests.js:54:26)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test failed, 0 tests passed (1 total)
Run time: 13.748s
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0 

      

Any help is appreciated.

+3


source to share


2 answers


The "npm test" command is executed for all results from the "find" command. "Nakhodka" does not care whether the test is successful or not. From what I understand, when the "npm test" command fails, everything should stop. Here is some code you can use instead of "find":



for D in $(find "$parent_dir" -maxdepth 2 -name "package.json"); do
    ( cd ${F%/*} && npm test ) || exit $?
done

      

0


source


jest has a -forceExit flag that can be used to ... force exit: D. just add it to your script.

You are probably doing some asynchronous activity in your test that does not return the callback in time. You can take a close look at this.



{
  "scripts": {
     "test": "jest --forceExit",
   "jest": {
    "scriptPreprocessor": "../shared/preprocessor.js"
  }
}

      

0


source







All Articles