简体   繁体   English

如何从node.js中的一个“npm test”命令运行mocha和mocha-phantomjs测试?

[英]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 . 我正在使用mochamocha-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: 我尝试了什么:

  1. Add these commands into npm test script seperated with semicolon 将这些命令添加到以分号分隔的npm test脚本中
    • Problem: When there is an error in first script but no error in second script, command exited with 0 and travis build passed. 问题:当第一个脚本中出现错误但第二个脚本中没有错误时,命令退出0并传递travis构建。
  2. Let node command test in npm test script and create custom script for browser tests. 让node命令在npm test脚本中测试,并为浏览器测试创建自定义脚本。 Than add these two commands ( npm test and npm run-script test-browser ) into travis.yml as array. 然后将这两个命令( npm testnpm run-script test-browser )添加到travis.yml作为数组。
    • Problem: Users have to run manually two independent test scripts. 问题:用户必须手动运行两个独立的测试脚本。
  3. Let node command test in npm test script and add browser tests to npm posttest command. 让node命令在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). Travis.yml将只有一个脚本,用户也必须运行一个脚本(每个人都很高兴)。
    • Problem: It just does't feel right, so I wanted to know if there is some better way. 问题:感觉不对,所以我想知道是否有更好的方法。

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内),而不是全局的mochamocha-phantomjs直接调用mochamocha-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 . 它将运行mochamocha-phantomjs本地副本。 Twitter bootstrap uses this library for development. Twitter引导程序使用此库进行开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM