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

时间:2022-07-07 01:12:12

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.

我有几个节点包在node中工作。js环境和浏览器。现在,我有两个分离测试(针对每个环境)。使用npm测试命令运行这些测试的最佳方式是什么?我还想把这些包添加到travis中。

I'm using mocha and 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:

我试着什么:

  1. 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.
    • 问题:当第一个脚本出现错误时,第二个脚本中没有错误,命令退出0,travis构建通过。
  2. 将这些命令添加到带有分号问题的npm测试脚本中:当第一个脚本中有错误而第二个脚本中没有错误时,命令以0退出并通过travis build。
  3. Let node command test in npm test script and create custom script for browser tests. Than add these two commands (npm test and npm run-script test-browser) into travis.yml as array.
    • Problem: Users have to run manually two independent test scripts.
    • 问题:用户必须手动运行两个独立的测试脚本。
  4. 让节点在npm测试脚本中进行测试,并为浏览器测试创建自定义脚本。将这两个命令(npm测试和npm运行脚本测试浏览器)添加到travis中。yml数组。问题:用户必须手动运行两个独立的测试脚本。
  5. Let node command test in npm test script and add browser tests to npm posttest command. 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.
    • 问题:感觉不太对,所以我想知道是否有更好的方法。
  6. 让节点命令在npm测试脚本中进行测试,并将浏览器测试添加到npm后测试命令中。特拉维斯。yml将不会只有一个脚本,用户也必须运行一个脚本(每个人都很高兴)。问题:感觉不太对,所以我想知道是否有更好的方法。

3 个解决方案

#1


77  

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. 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(在node_modules中),而不是全局的mocha,因此直接调用mocha和mocha-phantomjs没有什么害处。你甚至可以更有效地使用mocha的b -b选择保释,一旦遇到错误就会退出。

#2


4  

Came here looking for information on configuring npm with karma. @dankohn's answer can be adapted thusly:

来到这里寻找关于使用karma配置npm的信息。@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.

希望这能帮助别人。

#3


0  

You can also use npm-run-all package:

你也可以使用npm-run-all软件包:

npm install npm-run-all --save-dev

npm安装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. Twitter bootstrap uses this library for development.

它将运行当地的摩卡和摩卡-鬼子。Twitter bootstrap使用这个库进行开发。

#1


77  

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. 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(在node_modules中),而不是全局的mocha,因此直接调用mocha和mocha-phantomjs没有什么害处。你甚至可以更有效地使用mocha的b -b选择保释,一旦遇到错误就会退出。

#2


4  

Came here looking for information on configuring npm with karma. @dankohn's answer can be adapted thusly:

来到这里寻找关于使用karma配置npm的信息。@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.

希望这能帮助别人。

#3


0  

You can also use npm-run-all package:

你也可以使用npm-run-all软件包:

npm install npm-run-all --save-dev

npm安装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. Twitter bootstrap uses this library for development.

它将运行当地的摩卡和摩卡-鬼子。Twitter bootstrap使用这个库进行开发。