3

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!

RobC
  • 22,977
  • 20
  • 73
  • 80
mismas
  • 1,236
  • 5
  • 27
  • 55
  • If you run `npm config get script-shell` which shell does it report? Does it report either: **1)** `sh` - which is the default shell that npm utilizes for running npm scripts on _*nix_ platforms. **2)** Or does it report `cmd.exe` - which is the default shell that npm utilizes for running npm scripts on _Windows_ platforms. If it's the later, i.e. if it's `cmd.exe`, then change your `test` script to utilize a single ampersand, for instance: `"test": "npm run test:v1 & npm run test:v2"`. – RobC Jun 12 '20 at 15:57
  • @RobC thanks for the comment! This is what I get: `C:\Program Files\nodejs>npm config get script-shell null C:\Program Files\nodejs>npm config get shell C:\WINDOWS\system32\cmd.exe` – mismas Jun 12 '20 at 16:04
  • Yes so you're using Windows and the default shell that npm scripts utilizes is `cmd.exe` - that's why when you try using a semi-colon (`;`) - as per _@gzcz_ answer it doesn't work. The semi-colon (`;`) would meet your requirement on _*nix_ platforms (Linux, macOS etc) because npm utilizes `sh` as the shell for npm scripts. As per my first comment you need to try using the [single ampersand (`&`)](https://ss64.com/nt/syntax-conditional.html). For instance: `"test": "npm run test:v1 & npm run test:v2"` – RobC Jun 12 '20 at 16:11
  • @RobC Thanks a lot man! it helped. It works with `&`. Can you please add a comment as an answer so I can accept it :) – mismas Jun 12 '20 at 16:13

2 Answers2

6

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 (;).

Community
  • 1
  • 1
RobC
  • 22,977
  • 20
  • 73
  • 80
1
npm run test:v1; npm run test:v2

; means run the next command regardless of the success of the previous one.

&& means only run the next command if the previous one succeeded.

gzcz
  • 496
  • 3
  • 7
  • Thanks for the reply! I edited my question with what I have tried based on your comment. Unfortunately it didn't work. Only first script was executed and after it failed the other one was not started. – mismas Jun 12 '20 at 15:47
  • You can use [concurrently](https://www.npmjs.com/package/concurrently). Run `npm install concurrently` to install then in your package.json scripts you can do `"test": "concurrently \"npm run test:v1\" \"npm run test:v2\""`. This will basically run the two test commands concurrently. – gzcz Jun 12 '20 at 16:03