Getting karma server to launch upon grunt watch

Izhaki

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?

Izhaki

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' ] );
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting karma server to launch upon grunt watch

From Dev

Grunt config watch and karma:unit in single task

From Dev

Grunt config watch and karma:unit in single task

From Dev

Getting grunt karma to run one unit test

From Dev

Can I use karma to launch a test server?

From Dev

Can I use karma to launch a test server?

From Dev

HowTo launch node --debug grunt server on Windows?

From Dev

HowTo launch node --debug grunt server on Windows?

From Dev

grunt-express-server with contrib-watch

From Dev

Runing angularjs app on grunt server without watch

From Dev

How can I automatically start grunt watch upon opening a Visual Studio project/solution?

From Dev

How do I configure grunt-karma to run tests directly when starting a watch?

From Dev

Grunt-contrib-connect: how to launch the server in a specified browser

From Dev

App crashes upon launch

From Dev

Grunt watch not working

From Dev

grunt watch & connect

From Dev

Using grunt watch with livereload

From Dev

Grunt Watch - Verifiying property

From Dev

Grunt - Watch new folders

From Dev

grunt watch and stylus

From Dev

Grunt watch task not working

From Dev

grunt watch and stylus

From Dev

Uglify not working with Grunt Watch

From Dev

Difference between Karma start and grunt karma:unit

From Dev

Getting test results when creating a Karma server programmatically

From Dev

Presenting a different storyboard upon launch

From Dev

Reacting upon iTunes launch with NSDistributedNotificationCenter

From Dev

Is it possible to run Grunt Karma locally?

From Dev

Chai variable not loaded by Karma/Grunt