Search code examples
angulartddkarma-jasmine

karma parallel executing test cases twice


I am upgrading my angular from 4 to version 7. I had karma-parallel to run the tdd and it was working as expected with Angular 4. Now after upgrading to 7 the same tests are running twice before stop executing. My karma.conf.js is as below,

const path = require('path');
module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: [ 'parallel', 'jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-parallel'),
      require('karma-jasmine'),
      require('karma-spec-reporter'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    parallelOptions: {
      executors: 3, // For Jenkins enterprise, stick to 6 executors. For local laptop, change to 3-5
      shardStrategy: 'round-robin'
    },
    client: {
      jasmine: {
        random: false
      },
      clearContext: false
    },
    coverageIstanbulReporter: {
      reports: ['html', 'json', 'text-summary'],
      dir: path.join(__dirname, 'coverage'),
      fixWebpackSourcePaths: true
    },    
    reporters: ['spec', 'kjhtml'],
    specReporter: {
         maxLogLines: 5,
         suppressErrorSummary: true,
         suppressFailed: false,
         suppressPassed: false,
         suppressSkipped: true,
         showSpecTiming: true,
         failFast: false
      },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
        ChromeHeadlessNoSandbox: {
            base: 'ChromeHeadless',
            flags: [
                '--no-sandbox', // required to run without privileges in docker
                '--user-data-dir=/tmp/chrome-test-profile',
                '--disable-web-security',
                '--no-proxy=http://0.0.0.0:9876/'
            ]
        }
    },
    singleRun: true,
    concurrency: Infinity,
    captureTimeout: 180000,
    browserDisconnectTimeout: 90000,
    browserNoActivityTimeout: 180000
  });
};

The command used to run the test cases is as below,

node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng test --watch=false --code-coverage --source-map=false

Please advice.


Solution

  • When you run ng test it will run the tests for all projects.

    For instance, if you have these two projects in your angular.json:

    • hello-world (with 10 tests)
    • utilities (with 100 tests)

    then when you run ng test it will run tests for both hello-world and utilities.

    However if you are using Karma with watch files or auto-watch enabled, it will stop at the first project so you will only see 10 tests run (or 100 tests depending on the order of the projects). Then when you Ctrl+C and exit the Karma process, it will continue to the next project, compile it and run the tests. So you will have two test runs.

    What to do?

    • If you have multiple Angular applications but are deploying one to production you should use ng test <app> with the project name that you are deploying.
    • If you have an Angular application and a library you will want to test the application and the library: ng test <app> && ng test <library>
    • If you want to run the full test suite, just do ng test and don't forget to change Karma to run a single test run and not to auto-watch for changed files!
    • Update the karma.conf.js for each project to ensure it has the correct settings (no watching, single run, correct browsers to use)