[英]How to run mocha and mocha-phantomjs tests from one “npm test” command in node.js?
我有幾個節點包可以在node.js環境中運行,也可以在瀏覽器中運行。 現在我有兩個單獨的測試(針對每個環境)。 使用npm test
命令運行這些測試的最佳方法是什么? 另外我想將這些包添加到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
腳本中
npm test
腳本中測試,並為瀏覽器測試創建自定義腳本。 然后將這兩個命令( npm test
和npm run-script test-browser
)添加到travis.yml作為數組。
npm test
腳本中npm test
,並將瀏覽器測試添加到npm posttest
命令。 Travis.yml將只有一個腳本,用戶也必須運行一個腳本(每個人都很高興)。
我喜歡以下內容:
"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
和mocha-phantomjs
沒有任何害處。 使用mocha的-b
選項可以更有效地保釋,一旦遇到錯誤就會退出。
來到這里尋找有關使用karma
配置npm
信息。 @ dankohn的回答可以這樣調整:
"scripts": {
"test": "npm run test-node && npm run test-browser",
"test-node": "karma run",
"test-browser": "karma start --single-run"
}
希望這有助於其他人。
您還可以使用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"
}
它將運行mocha
和mocha-phantomjs
本地副本。 Twitter引導程序使用此庫進行開發。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.