8
votes

I'm currently looking at unit testing with Angular 6.0. I'm using Karma and Jasmine that come bundled with a new Angular project to run the tests. The tests run fine and all, however the browsers close automatically after each test run, which means any failing tests become pretty much impossible to debug.

I've tried playing around with the configuration, to no success. I'm quite sure that nothing is overriding the configuration and I know that this configuration is used (e.g. after modifying the browsers array, I now have multiple browsers launch).

I execute tests by running the 'ng test' command. Attempting to execute 'karma start' does not seem to work - not sure if that is relevant.

Here's karma.conf.js:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-edge-launcher'),
      require('karma-firefox-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome', 'Firefox', 'Edge'],
    singleRun: false
  });
};

'singleRun' seems to have no effect either way same as autoWatch.

1

1 Answers

14
votes

It's a bug in angular 6. The github issue is here.

workaround is ng test --watch. Hat tip Ryan McCormick