【发布时间】:2020-10-02 05:47:05
【问题描述】:
我在 package.json 文件中有这个:
{
"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"
},
...
}
运行此命令时:
npm run test
那么如果test:v1 失败,那么test:v2 脚本根本不会执行。
有没有办法配置它,以便所有脚本都运行,而不管其中一些脚本是否失败?
编辑: 当我在 package.json 中尝试这个时:
"test": "npm run test:v1 ; npm run test:v2"
然后我仍然没有执行两个脚本,只是 test:v1 执行错误。
这是我在控制台中得到的:
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
如果我在 package.json 中这样尝试:
"test": "npm run test:v1; npm run test:v2"
然后我在控制台中得到这个:
npm ERR! missing script: test:v1;
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! test:v1
npm ERR! test:v2
谢谢!
【问题讨论】:
-
如果你运行
npm config get script-shell它会报告哪个shell?它是否报告:1)sh- 这是 npm 用于在 *nix 平台上运行 npm 脚本的默认 shell。 2) 或者它是否报告cmd.exe- 这是 npm 用于在 Windows 平台上运行 npm 脚本的默认 shell。如果是后者,即cmd.exe,则更改您的test脚本以使用单个 & 符号,例如:"test": "npm run test:v1 & npm run test:v2"。 -
@RobC 感谢您的评论!这就是我得到的:
C:\Program Files\nodejs>npm config get script-shell null C:\Program Files\nodejs>npm config get shell C:\WINDOWS\system32\cmd.exe -
是的,所以您使用的是 Windows,npm 脚本使用的默认 shell 是
cmd.exe- 这就是为什么当您尝试使用分号 (;) 时 - 根据 @ gzcz 回答它不起作用。分号 (;) 将满足您在 *nix 平台(Linux、macOS 等)上的要求,因为 npm 使用sh作为 npm 脚本的外壳。根据我的第一条评论,您需要尝试使用single ampersand (&)。例如:"test": "npm run test:v1 & npm run test:v2" -
@RobC 非常感谢!它有帮助。它适用于
&。您能否添加评论作为答案,以便我接受:)
标签: javascript node.js package.json webdriver-io npm-scripts