How to run mocha and mocha-phantomjs tests from one "npm test" command in node.js?

node.jsTestingBrowserPhantomjsmocha.js

node.js Problem Overview


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.

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.
  1. 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.
  1. 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.

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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

"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.

Solution 3 - node.js

You can also use npm-run-all package:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid KuderaView Question on Stackoverflow
Solution 1 - node.jsDan KohnView Answer on Stackoverflow
Solution 2 - node.jsDrew NoakesView Answer on Stackoverflow
Solution 3 - node.jsDmitry YaremenkoView Answer on Stackoverflow