0

I have jest and jest-puppeteer setup to run tests. Running tests is simple enough using:

jest test.test --config="path/to/config.json"

However when I put this into the package.json:

...
"scripts": {
    "test:jest": "jest test.test --config=\"path/to/config.json\""
},
...

And then run:

npm run test:jest

The test runs normally, but then I get the following error at the end:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test@1.0.0 jest:temp: `jest test.test --config="path/to/config.json"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the test@1.0.0 jest:temp script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/obihill/.npm/_logs/2020-04-23T12_18_23_546Z-debug.log

After doing some research, I found out that this is because some tests might be failing. Sure enough, I setup all tests to pass, and the error no longer appears.

The issue I have is that I need to run this test and then a second test in sequence:

npm run test:jest && npm run test:postjest

But because of the failing tests and the resulting ELIFECYCLE code, the second command never gets run. After some research, I found a way to suppress the errors. So now I run the following:

npm run test:jest --silent && npm run test:postjest

And there are no errors, but test:postjest never runs. All the tests have to pass in test:jest for the second command to run.

Is there a way to override this behavior and enable npm run test:postjest to run in sequence regardless?

ObiHill
  • 11,448
  • 20
  • 86
  • 135

1 Answers1

0

Managed to find an answer.

I got it to work by using a chain operator on the command to exit cleanly on error.

Here's my updated package.json:

...
"scripts": {
    "test:jest": "jest test.test --config=\"path/to/config.json\" || exit 0"
},
...

|| is a chaining operator in Linux. The way it works is if the first command [jest test.test --config=\"path/to/config.json\"] fails, the second command [exit 0] will run. Since the second command is a successful exit signal [0 == success, 1 == error], the command exits successfully and another command can be run. And if the first command is successful, then exit 0 is skipped.

I also discarded the --silent option as it's no longer needed.

Hope you find this quickly if you run into same issue.

ObiHill
  • 11,448
  • 20
  • 86
  • 135