业力测试用例 运行 多次:Angular 单元测试

karma test cases are running multiple times: Angular unit test

我无法理解为什么我的业力测试用例在一个固定的测试用例后重新运行多次。

已断开连接,因为 50000 毫秒内没有消息。 Chrome 75.0.3770 (Windows 10.0.0):已执行 251 次断开连接中的 131 次(1 分钟 9.028 秒/18.285 秒)

Chrome 75.0.3770 (Windows 10.0.0):已执行 251 次断开连接中的 131 次(47.273 秒/18.169 秒)

Chrome 75.0.3770 (Windows 10.0.0):已执行 131 次,共 251 次断开连接(1 分钟 9.028 秒/18.285 秒)

Chrome 75.0.3770 (Windows 10.0.0):已执行 251 次断开连接中的 131 次(47.273 秒/18.169 秒)

Chrome 75.0.3770 (Windows 10.0.0):已执行 97 次,共 251 次断开连接(22.07 秒/19.87 秒)

Chrome 75.0.3770 (Windows 10.0.0):已执行 131 次,共 251 次断开连接(1 分钟 9.028 秒/18.285 秒)

Chrome 75.0.3770 (Windows 10.0.0):成功执行了 251 次中的 131 次(0 秒/17.406 秒)

Chrome 75.0.3770 (Windows 10.0.0):成功执行了 251 次中的 239 次(0 秒/20.912 秒)

下面是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,
  };
};

当我将 singleRun 更改为 false 并使用 ng test 执行测试用例时,它工作正常,只有当我将其更改为 true 时才会出现问题。

我看了几个博客并尝试通过添加更多配置来解决它

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

但对我没有任何用处。

下面是devDependencies中的配置(我使用的是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"
  }

求推荐!!!!

我通过监视其中设置了 location.href 的函数解决了上述问题。

location.href 导致页面以不同的 URL 加载,因此业力一次又一次地 运行。