运行摩卡的Grunt - 测试失败时中止

时间:2013-10-14 14:47:37

标签: javascript node.js unit-testing gruntjs mocha

我正在尝试学习这种自动化单元测试malarkey。我想使用Grunt自动运行我的Mocha测试,并将测试结果输出到文件。据我所知,我需要使用grunt-mocha-cov。我有点工作:当测试通过时Grunt写出结果文件OK。但当其中一个失败时,我得到了这个:

Running "mochacov:all" (mochacov) task
Warning:  Use --force to continue.

Aborted due to warnings.

并且没有创建文件。谁能告诉我哪里出错了?

我的项目组织如下:

enter image description here

我的测试文件夹包含一个文件test.js,如下所示:

   var chai = require("chai"),
   assert = chai.assert,
   expect = chai.expect;

    var foobar = {
      sayHello: function() {
        return 'Hello World!';
      }
    }

    describe('Foobar', function() {
      describe('#sayHello()', function() {
        it('should work with assert', function() {
          assert.equal(foobar.sayHello(), 'Hello World!');
        });
        it('should work with expect', function() {
          expect(foobar.sayHello()).to.equal('Hello Worlxd!');
        });
      });
    });

package.json有这个:

{
  "name": "GruntTest",
  "version": "0.0.1",
  "private": true,
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-mocha-cli": "~1.3.0",
    "grunt-contrib-qunit": "~0.3.0",
    "grunt-contrib-jshint": "~0.6.4",
    "grunt-mocha": "~0.4.1",
    "should": "~2.0.1",
    "chai": "~1.8.1",
    "grunt-mocha-cov": "0.0.7"
  },
  "description": "Grunt Test",
  "main": "grunt.js",
  "dependencies": {
    "grunt": "~0.4.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "BSD"
}

这是我的Gruntfile.js:

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    mochacov: {
       options: {
         reporter: 'XUnit',
         require: ['should'],
         output: 'test-results.xml',
         bail: false
       },
       all: ['test/*.js']
     }
  });

  grunt.loadNpmTasks('grunt-mocha-cov');
  grunt.registerTask('default', ['mochacov']);

};

修改

根据xavier的建议,我让它与mochacov和xunit文件记者合作。这是我新改进的Gruntfile,以防其他人使用:

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    mochacov: {
     options: {
       reporter: 'xunit-file',
       require: ['should'],
       bail: false
     },
     all: ['test/*.js']
   }
  });

  grunt.loadNpmTasks('grunt-mocha-cov');
  grunt.registerTask('default', ['mochacov']);

};

终端发出警告“由于警告而中止”,但mochacov会创建一个包含测试结果的文件xunit.xml。

1 个答案:

答案 0 :(得分:1)

尝试一下:https://github.com/yaymukund/grunt-simple-mocha

与下列记者之一:http://visionmedia.github.io/mocha/#reporters

或类似的内容:https://github.com/peerigon/xunit-file

但事实是你应该从服务器端放弃咕噜声,并使用Makefile!

这是我使用的经典之作:

MOCHA="node_modules/.bin/mocha"
_MOCHA="node_modules/.bin/_mocha"
JSHINT="node_modules/.bin/jshint"
ISTANBUL="node_modules/.bin/istanbul"

TESTS=$(shell find test/ -name "*.test.js")

clean:
    rm -rf reports

test:
    $(MOCHA) -R spec $(TESTS)

jshint:
    $(JSHINT) src test

coverage:
    @# check if reports folder exists, if not create it
    @test -d reports || mkdir reports
    $(ISTANBUL) cover --dir ./reports $(_MOCHA) -- -R spec $(TESTS)

.PHONY: clean test jshint coverage

就是这样,只需安装mocha,jshint和istanbul作为dev依赖项,你就可以了

有些人会告诉你全局安装这些工具,但这取决于你