我有一些用mocha / chai编写的JS测试,我想在使用webapp生成器的脚手架中运行它们。
我将测试放在Yeoman结构的“test”文件夹中,测试运行正常。但问题是grunt test
命令仅在控制台中显示测试结果,而不是在浏览器中显示。
我正在寻找一种运行命令的方法,并在浏览器中显示测试。我怎么能这样做?
感谢您的帮助!
答案 0 :(得分:1)
请考虑connect
配置的这一部分(更确切地说,这是connect
的子任务):
test : {
options : {
middleware : function(connect) {
return [
require('connect-livereload')(),
mountFolder(connect, '.tmp'),
mountFolder(connect, 'test')
];
},
port : grunt.option('port') ? Number(grunt.option('port')) + 1 : 9001
}
}
以上内容将通过http提供指定文件夹中的文件。
.tmp
是我编译的coffeescript和SCSS作为常规JS / CSS登陆的地方test
是我的测试所在的位置,还有一个非常简单的index.html
文件,它将所有JS / CSS连接在一起,包括mocha.js
和mocha.css
稍后在我的Gruntfile中,我注册了test
任务:
grunt.registerTask('test', function(target) {
var tasks = [
'clean:server',
'coffee',
'jst',
'connect:test'
];
if (target === 'browser') {
tasks.push('open:test');
tasks.push('watch');
} else {
tasks.push('mocha');
}
grunt.task.run(tasks);
});
与您的问题相关的部分是'connect:test'
,这使得可以通过浏览器访问测试,而这一部分:
if (target === 'browser') {
tasks.push('open:test');
tasks.push('watch');
} else {
tasks.push('mocha');
}
只要您没有将browser
指定为test
目标,测试就会在控制台中无头地运行。但是如果你像grunt test:browser
那样,Grunt会因open:test
而打开浏览器。供您参考,我还包括我的open:test
配置:
test : {
path : 'http://localhost:<%= connect.test.options.port %>'
}