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

前端 未结 3 1172
慢半拍i
慢半拍i 2020-12-07 17:11

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 th

相关标签:
3条回答
  • 2020-12-07 17:51

    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.

    0 讨论(0)
  • 2020-12-07 17:54

    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.

    0 讨论(0)
  • 2020-12-07 18:06

    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.

    0 讨论(0)
提交回复
热议问题