javascript 如何使用 npm 脚本运行 mocha?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33729846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:50:25  来源:igfitidea点击:

How can I run mocha with npm script?

javascriptnode.jstestingnpmmocha

提问by Kris

I have implemented a testcase for my script and it runs pretty fine when I execute it in Webstorm with the configuration for mocha.

我已经为我的脚本实现了一个测试用例,当我在 Webstorm 中使用 mocha 配置执行它时,它运行得非常好。

The name of my test script is adminTest.js.

我的测试脚本的名称是 adminTest.js。

Now I like to have a npm script to run it from the console or later from the build server.

现在我喜欢使用 npm 脚本从控制台或稍后从构建服务器运行它。

Therefor I have made a script entry in my package.json

因此,我在 package.json 中创建了一个脚本条目

Here is the complete file:

这是完整的文件:

{
  "name": "cdh",
  "version": "0.0.1",
  "description": "CDH connector",
  "main": "cdh.js",
  "private" : true,
  "dependencies": {
    "cli-color": "^1.0.0",
    "handlebars": "^4.0.3",
    "sync-request": "^2.0.1",
    "jslint": "^0.9.3",
    "xmldom": "^0.1.19",
    "xpath": "^0.0.9",
    "mocha": "2.3.3 ",
    "proxyquire": "1.7.3",
    "mocha-sinon": "1.1.4",
    "jasmine" : "2.3.2",
    "chai" : "3.4.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec"
  },
  "author": "kme",
  "license": "ISC"
}

When I start the script with npm run testfrom the console it leads to this error:

当我npm run test从控制台启动脚本时,它会导致此错误:

> [email protected] test C:\src\trunk\scripts\testing
> node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec


npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\
node_modules\npm\bin\npm-cli.js" "run" "test"
npm ERR! node v4.2.0
npm ERR! npm  v2.14.7
npm ERR! code ELIFECYCLE
npm ERR! [email protected] test: `node ./node_modules/mocha/bin/mocha tests/**/*Test.js
--reporter spec`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script 'node ./node_modules/mocha/bin/moch
a tests/**/*Test.js --reporter spec'.
npm ERR! This is most likely a problem with the cdh package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter sp
ec
npm ERR! You can get their info via:
npm ERR!     npm owner ls cdh
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\src\trunk\scripts\testing\npm-debug.log

The npm-debug.logfile looks like:

npm-debug.log文件如下所示:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\Program Files\nodejs\node.exe',
1 verbose cli   'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'test' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info pretest [email protected]
6 info test [email protected]
7 verbose unsafe-perm in lifecycle true
8 info [email protected] Failed to exec test script
9 verbose stack Error: [email protected] test: `node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec`
9 verbose stack Exit status 1
9 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:214:16)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at EventEmitter.emit (events.js:172:7)
9 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:24:14)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at ChildProcess.emit (events.js:172:7)
9 verbose stack     at maybeClose (internal/child_process.js:818:16)
9 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid [email protected]
11 verbose cwd C:\src\trunk\scripts\testing
12 error Windows_NT 6.1.7601
13 error argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "test"
14 error node v4.2.0
15 error npm  v2.14.7
16 error code ELIFECYCLE
17 error [email protected] test: `node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec`
17 error Exit status 1
18 error Failed at the [email protected] test script 'node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec'.
18 error This is most likely a problem with the cdh package,
18 error not with npm itself.
18 error Tell the author that this fails on your system:
18 error     node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec
18 error You can get their info via:
18 error     npm owner ls cdh
18 error There is likely additional logging output above.
19 verbose exit [ 1, true ]

回答by Sonata

Your script definition should not include node:

您的脚本定义不应包括node

"scripts": {
    "test": "./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec"
},

Or, if mocha is installed globally:

或者,如果 mocha 是全局安装的:

"scripts": {
    "test": "mocha tests/**/*Test.js --reporter spec"
},

See also: Configure node npm package.json so that "npm test" works on both unix and windows.

另请参阅:配置节点 npm package.json 以便“npm test”适用于 unix 和 windows

Update: Ah is see you are working on windows, try changing the script as mentioned in the above post:

更新:啊,看到你在 Windows 上工作,尝试更改上面帖子中提到的脚本:

"testOnWindowsUseThis" : "node node_modules/mocha/bin/mocha"