简体   繁体   English

咕running的摩卡咖啡-测试失败时中止

[英]Grunt running mocha - aborts when test fails

I'm trying to learn this automated unit test malarkey. 我正在尝试学习这种自动化的单元测试恶意代码。 I want to use Grunt to run my Mocha tests automatically, and output test results to a file. 我想使用Grunt自动运行Mocha测试,并将测试结果输出到文件中。 As far as I can make out, I need to use the grunt-mocha-cov for this. 据我所知,我需要为此使用grunt-mocha-cov I've sort of got it working: when the tests pass Grunt writes out the results file OK. 我已经开始工作了:当测试通过时,Grunt会写出结果文件。 But when one of them fails I get this: 但是当其中一个失败时,我得到以下信息:

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

Aborted due to warnings.

And no file is created. 并且没有文件被创建。 Can anyone tell me where I'm going wrong? 谁能告诉我我要去哪里错了?

My project's organised like this: 我的项目是这样组织的:

在此处输入图片说明

My test folder contains a single file, test.js, which looks like this: 我的测试文件夹包含一个文件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 has this: 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"
}

And here's my Gruntfile.js: 这是我的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']);

};

EDIT 编辑

Following xavier's advice I got it working with mochacov and the xunit-file reporter. 遵循xavier的建议,我使它与mochacov和xunit-file记者一起工作。 Here's my new improved Gruntfile, in case useful to anyone else: 这是我新改进的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']);

};

The terminal gives the warning "Aborted due to warnings", but mochacov creates a file xunit.xml with the test results. 终端发出警告“由于警告而中止”,但是mochacov创建了包含测试结果的文件xunit.xml。

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

with one of the following reporter: http://visionmedia.github.io/mocha/#reporters 与以下记者之一: http : //visionmedia.github.io/mocha/#reporters

or something like this: https://github.com/peerigon/xunit-file 或类似这样的东西: https : //github.com/peerigon/xunit-file

but the truth is that you should ditch grunt from the server side, and use a Makefile! 但事实是您应该从服务器端放弃咕gr声,并使用Makefile!

here is a classic one i use: 这是我使用的经典:

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

that's it, just install mocha, jshint and istanbul as dev dependencies and you're good to go 就是这样,只需将mocha,jshint和istanbul安装为开发依赖项,您就可以开始了

some people will tell you to install these tools globaly, but it's up to you 有人会告诉您全局安装这些工具,但这取决于您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM