如何从 node.js 中的一个“npm test”命令运行 mocha 和 mocha-phantomjs 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20376269/
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
How to run mocha and mocha-phantomjs tests from one "npm test" command in node.js?
提问by David Kudera
I have got few node packages which works in node.js environment and also in browser. Now I have got two seperate tests (for each environment). What is the best way to run these tests with just npm testcommand? Also I want to add these packages to travis.
我有几个可以在 node.js 环境和浏览器中运行的节点包。现在我有两个单独的测试(针对每个环境)。仅使用npm test命令运行这些测试的最佳方法是什么?我也想将这些包添加到 travis。
I'm using mochaand mocha-phantomjs.
我正在使用mocha和mocha-phantomjs。
Node test command
节点测试命令
node ./node_modules/mocha/bin/mocha ./test/node/index.js --reporter spec
Browser test command
浏览器测试命令
node ./node_modules/mocha-phantomjs/bin/mocha-phantomjs ./test/browser/index.html
What I tried:
我试过的:
- Add these commands into
npm testscript seperated with semicolon- Problem:When there is an error in first script but no error in second script, command exited with 0 and travis build passed.
- Let node command test in
npm testscript and create custom script for browser tests. Than add these two commands (npm testandnpm run-script test-browser) into travis.yml as array.- Problem:Users have to run manually two independent test scripts.
- Let node command test in
npm testscript and add browser tests tonpm posttestcommand. Travis.yml will than have got just one script and users will also have to run one script (everyone is happy).- Problem:It just does't feel right, so I wanted to know if there is some better way.
- 将这些命令添加到
npm test以分号分隔的脚本中- 问题:当第一个脚本有错误但第二个脚本没有错误时,命令以 0 退出,travis 构建通过。
- 让 node 命令在
npm test脚本中测试并为浏览器测试创建自定义脚本。然后将这两个命令(npm test和npm run-script test-browser)作为数组添加到 travis.yml 中。- 问题:用户必须手动运行两个独立的测试脚本。
- 让节点命令在
npm test脚本中测试并将浏览器测试添加到npm posttest命令中。Travis.yml 将只有一个脚本,用户还必须运行一个脚本(每个人都很高兴)。- 问题:就是感觉不太对,所以想知道有没有更好的办法。
回答by Dan Kohn
I like the following:
我喜欢以下内容:
"scripts": {
"test": "npm run test-node && npm run test-browser",
"test-node": "mocha -R spec ./test/node/index.js",
"test-browser": "mocha-phantomjs ./test/browser/index.html"}
The &&only runs the second if the first passes, and you can run either separately if you want. Note that npm always uses the relative mocha(inside node_modules), not the global one, so there's no harm in just calling mochaand mocha-phantomjsdirectly. You can be even more efficient with mocha's -boption for bail, which will quit as soon as it encounters an error.
在&&只运行第二,如果第一关,你可以分开,如果你想运行。请注意,npm 始终使用相对 mocha(在 node_modules 内部),而不是全局mocha,因此直接调用mocha和没有坏处mocha-phantomjs。使用 mocha 的-b保释选项可以更高效,一旦遇到错误就会退出。
回答by Drew Noakes
Came here looking for information on configuring npmwith karma. @dankohn's answercan be adapted thusly:
来到这里寻找有关npm使用karma. @dankohn 的答案可以这样调整:
"scripts": {
"test": "npm run test-node && npm run test-browser",
"test-node": "karma run",
"test-browser": "karma start --single-run"
}
Hope this helps someone else out.
希望这可以帮助其他人。
回答by Dmitry Yaremenko
You can also use npm-run-allpackage:
您还可以使用npm-run-all包:
npm install npm-run-all --save-dev
npm install npm-run-all --save-dev
"scripts": {
"test": "npm-run-all test-mocha test-mocha-phantomjs",
"test-mocha": "mocha ./test/node/index.js --reporter spec",
"test-mocha-phantomjs": "mocha-phantomjs ./test/browser/index.html"
}
It will run local copies of mochaand mocha-phantomjs. Twitter bootstrap uses this library for development.
它将运行的本地副本mocha和mocha-phantomjs。Twitter bootstrap 使用这个库进行开发。

