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.
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.
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.
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.
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.
It's a bug in angular 6. The github issue is here.
workaround is ng test --watch
. Hat tip Ryan McCormick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With