NPM测试"如何从一个&QUOT运行摩卡和摩卡phantomjs试验;在Node.js的命令? [英] How to run mocha and mocha-phantomjs tests from one "npm test" command in node.js?

查看:216
本文介绍了NPM测试"如何从一个&QUOT运行摩卡和摩卡phantomjs试验;在Node.js的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览器中有几个节点程序包这Node.js的环境中工作和也。现在,我有(每个环境)两个单独的测试。什么是只有 NPM测试命令来运行这些测试的最好方法?此外,我想将这些包添加到特拉维斯。

我用摩卡摩卡phantomjs

节点测试命令

 节点./node_modules/mocha/bin/mocha ./test/node/index.js --reporter规范

浏览器测试命令

 节点./node_modules/mocha-phantomjs/bin/mocha-phantomjs ./test/browser/index.html

我的尝试:


  1. 添加这些命令到 NPM测试脚本分隔与分号

    • 问题:的当在第一个脚本错误,但在第二个脚本没有错误,命令0退出和Travis共建通过


  2. NPM测试脚本节点命令测试并为浏览器测试创建自定义脚本。不是增加这两个命令( NPM测试 NPM运行测试脚本的浏览器)到travis.yml为阵。

    • 问题:的用户必须手动运行两个独立的测试脚本


  3. NPM测试节点命令测试脚本,并添加浏览器测试,以 NPM后测命令。 Travis.yml将比已经得到只是一个脚本,用户还必须运行一个脚本(每个人都高兴)。

    • 问题:的它只是简化版,感觉不对,所以我想知道是否有一些更好的办法



解决方案

我喜欢以下内容:

 脚本:{
    测试:NPM运行测试节点和放大器;&安培; NPM运行测试的浏览器
    测试节点:摩卡-R规格./test/node/index.js
    测试的浏览器:摩卡phantomjs ./test/browser/index.html}

&放大器;&安培; 只运行第二个如果第一次传球,你可以,如果你想要么单独运行。需要注意的是故宫总是使用相对摩卡(里面node_modules),而不是全球性的,所以有在短短调用摩卡摩卡phantomjs 直接没有坏处。你可以更有效与摩卡的 -b 选项保释,这会尽快遇到错误退出。

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.

解决方案

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.

这篇关于NPM测试"如何从一个&QUOT运行摩卡和摩卡phantomjs试验;在Node.js的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆