如何从node.js中的一个"npm test"命令运行mocha和mocha-phantomjs测试?

Dav*_*era 57 browser testing mocha.js node.js phantomjs

我有几个节点包可以在node.js环境中运行,也可以在浏览器中运行.现在我有两个单独的测试(针对每个环境).使用npm test命令运行这些测试的最佳方法是什么?另外我想将这些包添加到travis.

我正在使用mochamocha-phantomjs.

节点测试命令

node ./node_modules/mocha/bin/mocha ./test/node/index.js --reporter spec
Run Code Online (Sandbox Code Playgroud)

浏览器测试命令

node ./node_modules/mocha-phantomjs/bin/mocha-phantomjs ./test/browser/index.html
Run Code Online (Sandbox Code Playgroud)

我尝试了什么:

  1. 将这些命令添加到npm test以分号分隔的脚本中
    • 问题:当第一个脚本中出现错误但第二个脚本中没有错误时,命令退出0并传递travis构建.
  2. 让node命令在npm test脚本中测试并为浏览器测试创建自定义脚本.然后将这两个命令(npm testnpm run-script test-browser)添加到travis.yml作为数组.
    • 问题:用户必须手动运行两个独立的测试脚本.
  3. 让node命令在npm test脚本中测试并将浏览器测试添加到npm posttest命令.Travis.yml将只有一个脚本,用户也必须运行一个脚本(每个人都很高兴).
    • 问题:感觉不对,所以我想知道是否有更好的方法.

Dan*_*ohn 77

我喜欢以下内容:

  "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"}
Run Code Online (Sandbox Code Playgroud)

&&只运行第二,如果第一关,你可以,如果你想要么单独运行.请注意,npm总是使用相对的mocha(在node_modules内),而不是全局的mocha,所以直接调用mocha并没有任何危害mocha-phantomjs.使用mocha的-b保释选项可以提高效率,保释会在遇到错误时立即退出.