如何从一个“npm 测试"运行 mocha 和 mocha-phantomjs 测试node.js 中的命令? [英] How to run mocha and mocha-phantomjs tests from one "npm test" command in node.js?
问题描述
我有几个可以在 node.js 环境和浏览器中运行的节点包.现在我有两个单独的测试(针对每个环境).仅使用 npm test
命令运行这些测试的最佳方法是什么?我也想将这些包添加到 travis.
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 test
command? Also I want to add these packages to travis.
我正在使用 mocha
和 mocha-phantomjs
.
节点测试命令
node ./node_modules/mocha/bin/mocha ./test/node/index.js --reporter spec
浏览器测试命令
node ./node_modules/mocha-phantomjs/bin/mocha-phantomjs ./test/browser/index.html
我的尝试:
- 将这些命令添加到以分号分隔的
npm test
脚本中- 问题:当第一个脚本有错误但第二个脚本没有错误时,命令以 0 退出,travis 构建通过.
- Add these commands into
npm test
script 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.
- 问题:用户必须手动运行两个独立的测试脚本.
- Problem: Users have to run manually two independent test scripts.
- 问题:感觉不太对,所以我想知道是否有更好的方法.
- Problem: It just does't feel right, so I wanted to know if there is some better way.
推荐答案
我喜欢以下内容:
"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"}
&&
仅在第一个通过时运行第二个,如果需要,您可以单独运行.请注意,npm 始终使用 相对 mocha (在 node_modules 中),而不是全局的,所以直接调用 mocha
和 mocha-phantomjs
没有坏处.你可以使用 mocha 的 -b
选项来提高效率,它会在遇到错误时立即退出.
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 mocha
and mocha-phantomjs
directly. You can be even more efficient with mocha's -b
option for bail, which will quit as soon as it encounters an error.
这篇关于如何从一个“npm 测试"运行 mocha 和 mocha-phantomjs 测试node.js 中的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!