'ng test ELIFECYCLE error when run from npm script, but not when run directly from command line

Within my Angular (v10) project, I am trying to write a script for automated testing. But when executing from that script, I'm getting an npm ELIFECYCLE error. Works fine if I perform the same command directly from command line though.

For example

$ ng test {module} --code-coverage --no-watch

works correctly.

But

$ npm run blah

where, "blah": "ng test {module} --code-coverage --no-watch" in my package.json results in

npm ERR! code ELIFECYCLE
npm ERR! errno 1

And there are no helpful details in the error message or log file.

Appreciate any insights for this issue. Thx!



Solution 1:[1]

Are you using Karma to run your test ?

I had the same issue, my tests were running with Karma and by default, it exits with an error if there is no test to run.

To avoid this problem, you can add failOnEmptyTestSuite: false to your karma.conf.js

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bertrand Baudeur