Вариант № 1
Можно использовать atBegin
вариант grunt-contrib-watch
. Идея заключается в том, чтобы ввести задачу запуска, которая будет запускаться при старте наблюдателе:
watch: {
startup: {
files: [], // This is redundant, but we get an error if not specifying files.
tasks: [ 'karma:continuous:start' ],
options: {
atBegin: true,
spawn: false
}
},
...
}
полный 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' ]);
};
Вариант № 2
Как показано в this и this блоги, альтернатива вместо звонка
$ grunt watch
один создает еще одну задачу, запустить сервер карма:
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'
},
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' ]);
};
следует избегать комментариев, как "спасибо", но. .. благодаря!! – demonkoryu