Search code examples
unit-testingjasminekarma-jasminepackage.jsonangular-unit-test

karma test cases are running multiple times: Angular unit test


I am not able to understand why my karma test cases re runs multiple times after a fixed test case.

Disconnected, because no message in 50000 ms. Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 DISCONNECTED (1 min 9.028 secs / 18.285 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 DISCONNECTED (47.273 secs / 18.169 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 DISCONNECTED (1 min 9.028 secs / 18.285 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 DISCONNECTED (47.273 secs / 18.169 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 97 of 251 DISCONNECTED (22.07 secs / 19.87 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 DISCONNECTED (1 min 9.028 secs / 18.285 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 131 of 251 SUCCESS (0 secs / 17.406 secs)

Chrome 75.0.3770 (Windows 10.0.0): Executed 239 of 251 SUCCESS (0 secs / 20.912 secs)

Below is the configuration of karma.conf.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

const { join } = require('path');
const { constants } = require('karma');

module.exports = () => {
  return {
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-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
      jasmine: {
        random: false
      }
    },
    coverageIstanbulReporter: {
      dir: join(__dirname, '../../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: constants.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: true,
  };
};

when I change singleRun to false and execute test case using ng test, it works fine, the problem is only when I change it to true.

I saw couple of blogs and tried to resolve it by adding some more configuration like

       browserNoActivityTimeout: 50000,
        captureTimeout: 60 * 1000,
        // captureTimeout: 210000,
        browserDisconnectTolerance: 1,
        // browserDisconnectTimeout: 210000,
        // browserNoActivityTimeout: 210000

but nothing has worked for me.

Below is the configuration in devDependencies (I am using angular cli version 8)

 "devDependencies": {
    "@angular-devkit/build-angular": "0.800.3",
    "@angular-devkit/build-ng-packagr": "0.800.3",
    "@angular/cli": "^8.0.3",
    "@angular/compiler-cli": "~8.0.0",
    "@angular/language-service": "^6.1.0",
    "@ngrx/store-devtools": "6.0.1",
    "@nrwl/schematics": "~8.0.0",
    "@ruf/schematics": "next",
    "@ruf/wrench": "next",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^8.10.10",
    "codelyzer": "5.1.0",
    "dotenv": "6.2.0",
    "jasmine-core": "~3.4.0",
    "jasmine-marbles": "0.3.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.5",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "~1.4.2",
    "ngrx-store-freeze": "0.2.4",
    "prettier": "1.16.4",
    "protractor": "~5.3.0",
    "scss-bundle": "^2.1.2",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.4.5",
    "istanbul-instrumenter-loader": "^3.0.1",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^3.0.4",
    "source-map-loader": "^0.2.4"
  }

Please suggest !!!!


Solution

  • I fixed the above issue by spying on functions which has location.href set inside it.

    location.href was causing the page to load with different URL and because of that karma was running again and again.