获取karma服务器以便在grunt watch上启动 [英] Getting karma server to launch upon grunt watch
问题描述
在开发过程中,我使用karma和grunt来监视文件更改并运行测试。
在命令行中,我希望能够只需输入
$ grunt手表
,并让业力服务器启动一次,然后咕噜地监视变化并在文件变化时运行各种任务(包括业力测试)。我不想输入 $ karma start
。
这怎么实现?
$可以使用 atBegin
$ b
/ code> grunt-contrib-watch
的选项。这个想法是介绍一个启动任务,它将在观察者启动时运行:
watch:{
startup:{
files:[],//这是多余的,但如果不指定文件,我们会得到一个错误。
任务:['karma:continuous:start'],
选项:{
atBegin:true,
spawn:false
}
},
}
watch:{
startup:{
files:[],//这是多余的,但如果不指定文件,我们会得到一个错误。
任务:['karma:continuous:start'],
选项:{
atBegin:true,
spawn:false
}
},
}
完整的 Gruntfile.js
:
module.exports = function(grunt){
grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),
karma:{
options:{
文件:['client / ** / *。spec.js'],
框架:['jasmine'],
记者:['progress'],
浏览器:['PhantomJS '],
singleRun:true,
autoWatch:false
},
continuous:{
singleRun:false,
背景:true
}
},
concat:{...},
ugl ify:{...},
watch:{
startup:{
files:[],//这是多余的,但如果没有指定文件则会出错。
任务:['karma:continuous:start'],
选项:{
atBegin:true,
spawn:false
}
},
js:{
files:['<%= concat.js.src%>'],
任务:['concat:js','uglify']
},
karma:{
files:['<%= concat.js.src%>','src / ** / test / ** / * .js'],
任务:['karma:continuous:run']
},
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('default',['concat','uglify','karma:unit:run']);
};
期权#2
$ grunt手表
启动因果服务器的任务:
grunt.registerTask('serve',['karma:continuous:start','watch ']);
然后调用:
$ grunt serve
完整的 Gruntfile.js
:
module.exports = function(grunt){
grunt .initConfig({
pkg:grunt.file.readJSON('package.json'),
karma:{
options:{
configFile:'karma。 conf.js'
},
单位:{
singleRun:true
},
continuous:{
//保持业力在后台运行
background:true
}
},
concat:{...},
uglify:{...},
watch:{
js:{
files:['<%= concat.js.src%>'],
tasks:['concat: js','uglify']
},
karma:{
文件:['<%= concat.js.src%>','src / ** / test / ** / *。js'],
tasks:['karma:continuous:run ']
},
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask('default',['concat','uglify','karma:unit:run']);
grunt.registerTask('serve',['karma:continuous:start','watch']);
};
While developing, I'm using karma and grunt to watch for file changes and run the tests.
In command line, I'd like to be able to simply enter
$ grunt watch
and have the karma server to start once, and thereafter having grunt watching for changes and running the various tasks (including karma tests) whenever files change. I don't want to enter $ karma start
.
How can this be achieved?
Option #1
One can use the atBegin
option of grunt-contrib-watch
. The idea is to introduce a startup task, which will run at the startup of the watcher:
watch: {
startup: {
files: [], // This is redundant, but we get an error if not specifying files.
tasks: [ 'karma:continuous:start' ],
options: {
atBegin: true,
spawn: false
}
},
...
}
The full Gruntfile.js
:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
karma: {
options: {
files: [ 'client/**/*.spec.js' ],
frameworks: [ 'jasmine' ],
reporters: [ 'progress' ],
browsers: [ 'PhantomJS' ],
singleRun: true,
autoWatch: false
},
continuous: {
singleRun: false,
background: true
}
},
concat: { ... },
uglify: { ... },
watch: {
startup: {
files: [], // This is redundant, but we get an error if not specifying files.
tasks: [ 'karma:continuous:start' ],
options: {
atBegin: true,
spawn: false
}
},
js: {
files: [ '<%= concat.js.src %>' ],
tasks: [ 'concat:js', 'uglify' ]
},
karma: {
files: [ '<%= concat.js.src %>', 'src/**/test/**/*.js' ],
tasks: [ 'karma:continuous:run' ]
},
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask( 'default', [ 'concat', 'uglify', 'karma:unit:run' ] );
};
Option #2
As shown in this and this blogs, an alternative is instead of calling
$ grunt watch
one creates another task that launch the karma server:
grunt.registerTask( 'serve', [ 'karma:continuous:start', 'watch' ] );
and then calls:
$ grunt serve
The full Gruntfile.js
:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
karma: {
options: {
configFile: 'karma.conf.js'
},
unit: {
singleRun: true
},
continuous: {
// keep karma running in the background
background: true
}
},
concat: { ... },
uglify: { ... },
watch: {
js: {
files: [ '<%= concat.js.src %>' ],
tasks: [ 'concat:js', 'uglify' ]
},
karma: {
files: [ '<%= concat.js.src %>', 'src/**/test/**/*.js' ],
tasks: [ 'karma:continuous:run' ]
},
}
});
require('load-grunt-tasks')(grunt);
grunt.registerTask( 'default', [ 'concat', 'uglify', 'karma:unit:run' ] );
grunt.registerTask( 'serve', [ 'karma:continuous:start', 'watch' ] );
};
这篇关于获取karma服务器以便在grunt watch上启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!