Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6.0 and Karma Jasmine - browser closes after test finish

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.

like image 989
Ross Avatar asked May 10 '18 04:05

Ross


People also ask

How do you use karma in Jasmine test cases?

We can run Jasmine tests in a browser ourselves by setting up and loading a HTML file, but more commonly we use a command-line tool called Karma. Karma handles the process of creating HTML files, opening browsers and running tests and returning the results of those tests to the command line.

What is Karma and Jasmine in Angular 6?

Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.

Is jest faster than karma?

Jest is 2 to 3 times faster than karma testing The tests that took 4–5 minutes on KARMA only takes about 1–2 minutes on jest. This is particularly important when using CI-CD ( Continous Integration/Continous Delivery). Since the tests are faster the execution time of CI-CD will also reduce.

Which of the following can be used to run end to end tests in Angular?

End-to-end testing (E2E) of Angular applications is performed using the Protractor testing framework, which is created by the Angular team themselves. Protractor can perform end to end tests on Angular applications that are running in a real browser by interacting with it, similar to that of an end-user.


1 Answers

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

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

like image 154
Beartums Avatar answered Oct 10 '22 01:10

Beartums