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

I have several node packages that work in the node.js environment as well as in the browser. Now I have two separate tests (for each environment). What is the best way to run these tests using the npm test command? I also want to add these packages to travis.

I use mocha and mocha-phantomjs .

Node testing team

 node ./node_modules/mocha/bin/mocha ./test/node/index.js --reporter spec 

Browser check command

 node ./node_modules/mocha-phantomjs/bin/mocha-phantomjs ./test/browser/index.html 

What I tried:

  • Add these commands to the npm test script, separated by semicolons
    • Problem: when an error occurred in the first script, but without an error in the second script, the command completed with 0 and the travis construct passed.
  • Enter the node command test in the npm test script and create a custom script for browser tests. Then add these two commands ( npm test and npm run-script test-browser ) to travis.yml as an array.
    • Problem. Users must manually run two independent test cases.
  • Enter the node command test in the npm test script and add browser tests to the npm posttest . Travis.yml will have only one script, and users will also have to run one script (everyone is happy).
    • Problem: it just doesn't feel good, so I wanted to know if there is a better way.
+56
browser testing phantomjs mocha
Dec 04 '13 at
source share
3 answers

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

&& only works the second, if the first passes, and you can run it separately if you want. Note that npm always uses relative mocha (inside node_modules) and not global, so there is no harm when directly calling mocha and mocha-phantomjs . You can be even more efficient with the mocha -b option for collateral, which will go away as soon as it encounters an error.

+77
Dec 04 '13 at 19:59
source share

Here you searched for information on configuring npm using karma . @dankohn answer can be configured as follows:

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

+4
Aug 28 '14 at 17:37
source share

You can also use the 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.

0
Aug 08 '17 at 13:40
source share



All Articles