[英]How to run mocha and mocha-phantomjs tests from one “npm test” command in node.js?
I have got few node packages which works in node.js environment and also in browser. 我有几个节点包可以在node.js环境中运行,也可以在浏览器中运行。 Now I have got two seperate tests (for each environment).
现在我有两个单独的测试(针对每个环境)。 What is the best way to run these tests with just
npm test
command? 使用
npm test
命令运行这些测试的最佳方法是什么? Also I want to add these packages to travis. 另外我想将这些包添加到travis。
I'm using mocha
and 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: 我尝试了什么:
npm test
script seperated with semicolon npm test
脚本中
npm test
script and create custom script for browser tests. npm test
脚本中测试,并为浏览器测试创建自定义脚本。 Than add these two commands ( npm test
and npm run-script test-browser
) into travis.yml as array. npm test
和npm run-script test-browser
)添加到travis.yml作为数组。
npm test
script and add browser tests to npm posttest
command. npm test
脚本中npm test
,并将浏览器测试添加到npm posttest
命令。 Travis.yml will than have got just one script and users will also have to run one script (everyone is happy). 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 mocha
and mocha-phantomjs
directly. 请注意,npm总是使用相对的mocha (在node_modules内),而不是全局的
mocha
, mocha-phantomjs
直接调用mocha
和mocha-phantomjs
没有任何害处。 You can be even more efficient with mocha's -b
option for bail, which will quit as soon as it encounters an error. 使用mocha的
-b
选项可以更有效地保释,一旦遇到错误就会退出。
Came here looking for information on configuring npm
with karma
. 来到这里寻找有关使用
karma
配置npm
信息。 @dankohn's answer can be adapted thusly: @ 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. 希望这有助于其他人。
You can also use npm-run-all
package: 您还可以使用
npm-run-all
包:
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 mocha
and mocha-phantomjs
. 它将运行
mocha
和mocha-phantomjs
本地副本。 Twitter bootstrap uses this library for development. Twitter引导程序使用此库进行开发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.