让业力服务器在grunt手表上启动

Izh*_*aki 2 gruntjs karma-runner grunt-contrib-watch

在开发过程中,我正在使用karma和grunt来监视文件更改并运行测试.

在命令行中,我希望能够简单地输入

$ grunt watch
Run Code Online (Sandbox Code Playgroud)

让业力服务器启动一次,然后每当文件发生变化时,咕噜咕噜地看着变化并运行各种任务(包括业力测试).我不想进入$ karma start.

怎么能实现这一目标?

Izh*_*aki 6

选项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
        }
    },

    ...
}
Run Code Online (Sandbox Code Playgroud)

完整的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' ] );
};
Run Code Online (Sandbox Code Playgroud)

选项#2

如在示出博客,另一种方法是,而不是调用

$ grunt watch
Run Code Online (Sandbox Code Playgroud)

一个创建启动业力服务器的另一个任务:

grunt.registerTask( 'serve', [ 'karma:continuous:start', 'watch' ] );
Run Code Online (Sandbox Code Playgroud)

然后打电话:

$ grunt serve
Run Code Online (Sandbox Code Playgroud)

完整的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' ] );
};
Run Code Online (Sandbox Code Playgroud)