gpt4 book ai didi

javascript - Grunt 运行 mocha - 测试失败时中止

转载 作者:行者123 更新时间:2023-11-30 05:43:36 24 4
gpt4 key购买 nike

我正在尝试学习这个自动化单元测试的骗局。我想使用 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-file reporter 完成了它。这是我新改进的 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。

最佳答案

试试那个: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 作为开发依赖项就可以了

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

关于javascript - Grunt 运行 mocha - 测试失败时中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com