2

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.

Nathan Friend
  • 12,155
  • 10
  • 75
  • 125

2 Answers2

0

If you need to run it manually with gulp, just make a task from it (I assume that you want to re-run server.start):

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

gulp.task('runTests', function() {
 server.start(); 
});

And then whenever you need to run test, run in command line:

gulp runTests 
Prototype
  • 276
  • 2
  • 12
  • I think this will either restart the Karma server or cause a second Karma server to run. I'm looking for way to cause the existing Karma server to reload its source files and rerun the tests - similar what `autoWatch: true` does - only manually. – Nathan Friend Apr 07 '16 at 18:42
0

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.

Nathan Friend
  • 12,155
  • 10
  • 75
  • 125