最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to continue running scripts defined in package.json in case one fails? - Stack Overflow

programmeradmin0浏览0评论

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 mand:

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

> [email protected] test C:\2020\demo
> npm run test:v1 ; npm run test:v2

> [email protected] 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% pleted) in 00:00:27

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] 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 [email protected] test:v1 script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A plete 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! [email protected] test: `npm run test:v1 ; npm run test:v2`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A plete 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!

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 mand:

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

> [email protected] test C:\2020\demo
> npm run test:v1 ; npm run test:v2

> [email protected] 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% pleted) in 00:00:27

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] 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 [email protected] test:v1 script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A plete 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! [email protected] test: `npm run test:v1 ; npm run test:v2`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A plete 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!

Share Improve this question edited Jun 12, 2020 at 16:54 RobC 25.1k21 gold badges84 silver badges85 bronze badges asked Jun 12, 2020 at 11:54 mismasmismas 1,3165 gold badges33 silver badges65 bronze badges 4
  • 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 Commented Jun 12, 2020 at 15:57
  • @RobC thanks for the ment! 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 Commented Jun 12, 2020 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 ment you need to try using the single ampersand (&). For instance: "test": "npm run test:v1 & npm run test:v2" – RobC Commented Jun 12, 2020 at 16:11
  • @RobC Thanks a lot man! it helped. It works with &. Can you please add a ment as an answer so I can accept it :) – mismas Commented Jun 12, 2020 at 16:13
Add a ment  | 

2 Answers 2

Reset to default 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 mand you need to check the value of the script-shell setting. To do this you can run the following mand:

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

npm run test:v1; npm run test:v2

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

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

发布评论

评论列表(0)

  1. 暂无评论