Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I run a specific test in npm?

npm run only one test n order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli. Then simply run your specific test with jest bar.

How do I run a single test file in mocha chai?

If you just want to run one test from your entire list of test cases then, you can write only ahead of your test case. If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well. describe.


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.


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.