Javascript 如何使用npm脚本运行mocha?
我已经为我的脚本实现了一个测试用例,当我在Webstorm中使用mocha配置执行它时,它运行得非常好 我的测试脚本的名称是adminTest.js 现在我希望有一个npm脚本从控制台或更高版本的构建服务器上运行它 因此,我在package.json中创建了一个脚本条目 以下是完整的文件:Javascript 如何使用npm脚本运行mocha?,javascript,node.js,testing,npm,mocha.js,Javascript,Node.js,Testing,Npm,Mocha.js,我已经为我的脚本实现了一个测试用例,当我在Webstorm中使用mocha配置执行它时,它运行得非常好 我的测试脚本的名称是adminTest.js 现在我希望有一个npm脚本从控制台或更高版本的构建服务器上运行它 因此,我在package.json中创建了一个脚本条目 以下是完整的文件: { "name": "cdh", "version": "0.0.1", "description": "CDH connector", "main": "cdh.js", "privat
{
"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"
}
当我从控制台用npm run test
启动脚本时,会导致以下错误:
> cdh@0.0.1 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! cdh@0.0.1 test: `node ./node_modules/mocha/bin/mocha tests/**/*Test.js
--reporter spec`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cdh@0.0.1 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
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 npm@2.14.7
3 info using node@v4.2.0
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info pretest cdh@0.0.1
6 info test cdh@0.0.1
7 verbose unsafe-perm in lifecycle true
8 info cdh@0.0.1 Failed to exec test script
9 verbose stack Error: cdh@0.0.1 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 cdh@0.0.1
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 cdh@0.0.1 test: `node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec`
17 error Exit status 1
18 error Failed at the cdh@0.0.1 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 ]
0信息如果它以ok结尾,它就工作了
1详细cli['C:\\Program Files\\nodejs\\node.exe',
1 verbose cli'C:\\Program Files\\nodejs\\node\u modules\\npm\\bin\\npm cli.js',
1详细cli“运行”,
1详细cli“测试”]
2信息使用npm@2.14.7
3信息使用node@v4.2.0
4详细的运行脚本[‘预测试’、‘测试’、‘后测试’]
5信息预测试cdh@0.0.1
6信息测试cdh@0.0.1
7生命周期中的详细不安全perm true
8信息cdh@0.0.1无法执行测试脚本
9详细堆栈错误:cdh@0.0.1test:`node./node_modules/mocha/bin/mocha tests/***test.js--reporter spec`
9详细堆栈退出状态1
9 EventEmitter上的详细堆栈。(C:\Program Files\nodejs\node\u modules\npm\lib\utils\lifecycle.js:214:16)
emitTwo上的9个详细堆栈(events.js:87:13)
9 EventEmitter.emit上的详细堆栈(events.js:172:7)
9子进程上的详细堆栈。(C:\Program Files\nodejs\node\u modules\npm\lib\utils\spawn.js:24:14)
emitTwo上的9个详细堆栈(events.js:87:13)
9 ChildProcess.emit上的详细堆栈(events.js:172:7)
9 maybeClose的详细堆栈(internal/child_process.js:818:16)
9 Process.ChildProcess.\u handle.onexit处的详细堆栈(internal/child\u Process.js:211:5)
10详细的pkgidcdh@0.0.1
11详细cwd C:\src\trunk\scripts\testing
12错误窗口\u NT 6.1.7601
13错误argv“C:\\Program Files\\nodejs\\node.exe”“C:\\Program Files\\nodejs\\node\u modules\\npm\\bin\\npm cli.js”“运行”“测试”
14错误节点v4.2.0
15错误npm v2.14.7
16错误代码ELIFECYCLE
17错误cdh@0.0.1test:`node./node_modules/mocha/bin/mocha tests/***test.js--reporter spec`
17错误退出状态1
18错误在测试时失败cdh@0.0.1测试脚本“node./node_modules/mocha/bin/mocha tests/***test.js--reporter spec”。
18错误这很可能是cdh包的问题,
18错误与npm本身无关。
18错误告诉作者此操作在您的系统上失败:
18错误节点./node_modules/mocha/bin/mocha tests/***Test.js——报告程序规范
18错误您可以通过以下方式获取他们的信息:
18错误npm所有者ls cdh
18错误上面可能有其他日志输出。
19详细退出[1,true]
脚本定义不应包括节点
:
"scripts": {
"test": "./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec"
},
或者,如果摩卡在全球安装:
"scripts": {
"test": "mocha tests/**/*Test.js --reporter spec"
},
另见:
更新:如果您正在windows上工作,请尝试更改上述帖子中提到的脚本:
"testOnWindowsUseThis" : "node node_modules/mocha/bin/mocha"
如果
npm运行测试
不起作用,请使用npx摩卡。我假设我的摩卡有问题。它总是返回退出代码1,但没有进一步的错误消息。也许您可以向bin/mocha
脚本和/或lib/mocha.js
添加一些console.log
,查看它们是否被调用以及如何被调用。您不需要/node\u模块/mocha/bin/
,npm脚本在路径中运行node_modules/.bin
-如果mocha 4.x或更高版本:“脚本”:{“测试”:“mocha--exit”},glob应该用双引号括起来,例如:“脚本”:{“测试”:“/node_modules/mocha/bin/mocha\”/test/unit/***.spec.js\“--记者闵”}