Search code examples
javascriptnode.jspackage.jsonwebdriver-ionpm-scripts

How to continue running scripts defined in package.json in case one fails?


I have this in package.json file:

{
 "name": "test demo",
 "version": "1.0.0",
 "description": "test demo scripts",
 "scripts": {
   "test:v1": "wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts",
   "test:v2": "wdio src/resources/config/wdio.firefox.conf.js --spec test/Tests.spec.ts",

   "test": "npm run test:v1 && npm run test:v2"
   },

    ...
}

When run this command:

npm run test

then if test:v1 fails, then test:v2 script is not executed at all.
Is there a way to configure this so that all scripts run regardless if some of them failed?

EDIT: When I try this in package.json:

"test": "npm run test:v1 ; npm run test:v2"

Then I still don't get both scripts executed, just test:v1 gets executed with error.
This is what I get in the console:

C:\2020\demo>npm run test

> demo@1.0.0 test C:\2020\demo
> npm run test:v1 ; npm run test:v2

> demo@1.0.0 test:v1 C:\2020\demo
> wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts ";" 
  "npm" "run" "test:v2"

Execution of 1 spec files started at 2020-06-12T15:16:42.936Z

[0-0] RUNNING in chrome - C:\2020\demo\Tests.spec.ts

... other log with failing tests ...

Spec Files:      0 passed, 1 failed, 1 total (100% completed) in 00:00:27

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test:v1: `wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts ";" "npm" "run" "test:v2"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo@1.0.0 test:v1 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!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06- 
12T15_17_10_512Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test: `npm run test:v1 ; npm run test:v2`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo@1.0.0 test 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!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06- 
12T15_17_10_548Z-debug.log

If I try like this in package.json:

"test": "npm run test:v1; npm run test:v2"

Then I get this in the console:

npm ERR! missing script: test:v1;
npm ERR!
npm ERR! Did you mean one of these?
npm ERR!     test:v1
npm ERR!     test:v2

Thanks!


Solution

  • The solution for this differs depending on which OS you are using because NPM utilizes a different shell for running npm scripts.

    • On *nix, (Linux, macOS, etc..), the default shell that npm utilizes for running npm scripts is sh.
    • On Windows the default shell that npm utilizes for running npm scripts is cmd.exe.

    Check which shell npm is using?

    Using the npm config command you need to check the value of the script-shell setting. To do this you can run the following command:

    npm config get script-shell
    

    It will typically return/print either the pathname to cmd.exe or sh - depending on which OS your using.


    Solution:

    1. If you're running Windows and the default shell is cmd.exe

      Then change your test script in package.json to the following:

      "test": "npm run test:v1 & npm run test:v2"
      

      Note the double ampersand (&&) has been replace with a single ampersand (&).

    Or...

    1. If you're running *nix and the default shell is sh

      Then change your test script in package.json to the following:

      "test": "npm run test:v1; npm run test:v2"
      

      Note the double ampersand (&&) has been replace with a single semi-colon (;).