58

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.
  2. 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.
  3. 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.
David Kudera
  • 806
  • 2
  • 8
  • 14
  • 1
    How about solution 1 but with `&&` instead of a semicolon? In this way if the first command fails, the second command isn't executed and the whole command fails; if the first passes and the second fails, the whole command still fails. – Paul Mougel Dec 04 '13 at 13:43
  • Ou.. You are right and I completely forgot that option, so I will add it as the right answer. Thank you very much :) – David Kudera Dec 04 '13 at 17:41

3 Answers3

78

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.

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
  • 1
    Great, I must say, that I really like this. I also was not sure if node will use the relative modules in first place even from command line and I was too bored to try it. So really thank you for both your suggestions. – David Kudera Dec 04 '13 at 21:33
  • Thankyou so much for this, have been stuck for a while! – Josh Mc Mar 31 '14 at 09:44
  • Just a note here, I believe that this is going to try and run the first and if that fails with an exit code, then it will never run the second. I don't know if this is a good thing or bad :/ – ThomasReggi Mar 30 '15 at 20:48
  • Hi, great solution! I'm trying to adapt it a server-side test though and I'm having trouble bootstrapping my server and then running all the tests on it. Right now the best I can do is start/stop the server with each test file. Is there some way to start it once and run all the tests on it? I know this is almost an entirely separate issue but just figured I'd try :) – Ben Zifkin Jun 05 '15 at 20:11
  • @ThomasReggi - replace the "&&" with a ";" if you want the 2nd test to run regardless of the success of the first. A note : To run all the tests you use `npm test`, to run just the 'test-node' one you use `npm test test-node`. Great answer - thank you - I am brand new to mocha and was struggling getting multiple tests organised. – kris Nov 09 '16 at 20:45
  • Hmm... I was wrong, `npm test test-node` does not run that one test. What command is used to run a single test? – kris Nov 09 '16 at 21:08
4

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.

Community
  • 1
  • 1
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • are you sure that `karma run` does what you say? that command says to me `There is no server listening on port 9876` – caesarsol Jul 08 '15 at 14:35
0

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.

Dmitry Yaremenko
  • 2,542
  • 20
  • 31