【问题标题】:Grunt running mocha - aborts when test failsGrunt running mocha - 测试失败时中止
【发布时间】:2013-10-14 14:47:36
【问题描述】:

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

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

Aborted due to warnings.

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

我的项目是这样组织的:

我的测试文件夹包含一个文件 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-file 记者的合作。这是我新改进的 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。

【问题讨论】:

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


    【解决方案1】:

    试试那个:https://github.com/yaymukund/grunt-simple-mocha

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

    或类似的东西:https://github.com/peerigon/xunit-file

    但事实是,您应该从服务器端抛弃 grunt,并使用 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 安装为开发依赖项就可以了

    有些人会告诉你在全球范围内安装这些工具,但这取决于你

    【讨论】:

    • 谢谢 xavier - grunt-simple-mocha 只是输出到控制台,我无法让 xunit-file 工作。我会继续努力的。
    • 上次我在服务器端使用 grunt 是这样的:github.com/xseignard/rss-unify/tree/… 看看
    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多