javascriptunit-testinggulpkarma-runnergulp-karma

Manually trigger Karma to rerun tests


I know that Karma has a built-in autoWatch option that will cause my tests to be rerun when a test file changes:

var server = new karmaServer({
    autoWatch: true,
    autoWatchBatchDelay: 250,
});
server.start();

Is there a way to trigger this rerun manually? I would like to have more control over when my tests are rerun.


Solution

  • I learned a bit more about Karma and discovered karma.runner.run(), which triggers an already-running server (for example, a Karma server you started in a different command window) to rerun its tests. In my gulp task I now do something like this:

    gulp.task('run-tests', function() {
        gulp.watch('/glob/to/my/files').on('change', function() {
            karma.runner.run({ configFile: 'karma.conf.js' });
        });    
    });
    

    Note that if you run this task from the same process that spawned your Karma server, you will see duplicate test results since both the server and the runner report their results to the command line. To only show one set of test results, you can start your Karma server in a background process using something like this.