ng测试不起作用,无法连接到broswers



嗨,我正试图在我的项目中运行ng test命令,但ng test没有捕获浏览器。

我读了很多断断续续的答案,但没有运气。

有人面临过同样的问题吗?

我试过这个堆栈溢出。

Angular Karma未捕获浏览器

我的karma.config.js文件是这样的。

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
  config.set({
    basePath: '',
    captureTimeout: 60000,
    browserDisconnectTimeout : 100000,
    browserDisconnectTolerance : 1,
    browserNoActivityTimeout : 60000,
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma'),
      require('karma-phantomjs-launcher'),
      require('karma-junit-reporter')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true,
      thresholds: {
        emitWarning: true,
        global: {
            statements: 55,
            lines: 55,
            branches: 55,
            functions: 55
        }
      }
    },
    angularCli: {
      environment: 'local'
    },
    reporters: ['progress', 'junit', 'kjhtml'],
    // the default configuration
    junitReporter: {
      outputDir: 'coverage', // results will be saved as $outputDir/$browserName.xml
      outputFile: 'test-result.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
      useBrowserName: false, // add browser name to report and classes names
      nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
      classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: true
  });
};

软件包.json

{
  "name": "c",
  "version": "1.0.0-B162",
  "license": "C",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --aot --sourcemaps=true",
    "build": "ng build --aot --sourcemaps=true --output-hashing=all",
    "local-build": "ng build --env=local",
    "dev2-build": "ng build --env=dev2",
    "prod-build": "ng build --target production --build-optimizer --vendor-chunk",
    "test": "ng test",
    "code-coverage": "ng test -cc --browser Chrome",
    "lint": "ng lint --force --format checkstyle > ./coverage/tslint-result.xml",
    "e2e": "ng e2e",
    "package-web": "cp .htaccess dist | tar -czvf spp-web.tar.gz -C dist .",
    "package-conf": "tar -czvf spp-conf.tar.gz -C httpd/ ."
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "5.2.9",
    "@angular/common": "5.2.9",
    "@angular/compiler": "5.2.9",
    "@angular/core": "5.2.9",
    "@angular/forms": "5.2.9",
    "@angular/http": "5.2.9",
    "@angular/platform-browser": "5.2.9",
    "@angular/platform-browser-dynamic": "5.2.9",
    "@angular/router": "5.2.9",
    "angular-in-memory-web-api": "^0.5.4",
    "bootstrap": "^3.3.7",
    "chart.js": "^2.8.0",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.5.3",
    "export-to-csv": "^0.2.1",
    "font-awesome": "^4.7.0",
    "intl": "^1.2.5",
    "jquery": "^3.2.1",
    "jsencrypt": "^2.3.1",
    "lodash": "^4.17.4",
    "moment": "^2.19.2",
    "primeng": "~5.0.0",
    "rxjs": "^5.5.7",
    "web-animations-js": "^2.3.1",
    "zone.js": "^0.8.20"
  },
  "devDependencies": {
    "@angular/cli": "^1.7.3",
    "@angular/compiler-cli": "5.2.9",
    "@angular/language-service": "5.2.9",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/lodash": "4.14.108",
    "@types/moment": "^2.13.0",
    "@types/node": "~6.0.60",
    "angular-tslint-rules": "^1.0.4",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-junit-reporter": "^1.2.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "phantomjs": "^2.1.7",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "2.4.2"
  }
}

解决方案

我在尝试运行ng测试时看到了错误。

+ ng test --code-coverage --watch=false
Your global Angular CLI version (11.2.3) is greater than your local version (10.1.7). The local Angular CLI version is used.
05 03 2021 09:53:02.407:INFO [karma-server]: Karma v5.0.9 server started at http://0.0.0.0:9876/
05 03 2021 09:53:02.410:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
05 03 2021 09:53:02.449:INFO [launcher]: Starting browser Chrome
05 03 2021 09:53:14.287:ERROR [launcher]: Cannot start Chrome
    2021-03-05 09:53:02.740 Google Chrome[61832:635831] NSNotificationCenter connection invalid
[61841:775:0305/095302.753567:ERROR:gl_initializer_mac.cc(65)] Error choosing pixel format.

我发现我的构建服务器更新chrome版本89。
现在我将chrome降级到版本86,它就可以工作了。

最新更新