首页 > 解决方案 > Angular Karma 未捕获浏览器

问题描述

我在 Angular 6 上有一个现有项目,其中测试未运行。它是如何发生的:在 PhpStorm 中,我在文件的上下文菜单中按下run file.specs.ts

它开始测试,打开浏览器,然后什么都没有发生。这是日志

Waiting for a captured browser... To capture a browser open http://localhost:9876/
14 10 2018 13:04:17.500:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
14 10 2018 13:04:17.799:INFO [launcher]: Trying to start Chrome again (1/2).
14 10 2018 13:05:17.808:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
14 10 2018 13:05:18.055:INFO [launcher]: Trying to start Chrome again (2/2).
14 10 2018 13:06:18.062:WARN [launcher]: Chrome have not captured in 60000 ms, killing.
14 10 2018 13:06:18.199:ERROR [launcher]: Chrome failed 2 times (timeout). Giving up.

我如何弄清楚这些测试有什么问题?我没有看到有关依赖项或其他内容的错误。有没有机会找到不捕获浏览器的原因?

在这个项目中,所有spec.ts的 s 都丢失了(看起来有人删除了它们),所以我尝试运行那些在新组件/服务上创建的。

没有这样的选项来启动新项目。我必须修复这个。

任何帮助将不胜感激。

这是我的 src/karma.conf.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: '',
    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
    },
    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'],
    singleRun: false
  });
};

标签: angulartypescriptkarma-runner

解决方案


问题很简单,通过运行显示ng test。有一些应用程序级错误破坏了 Jasmine 初始化(虽然我不明白为什么当我尝试在 PhpStorm 中对单个文件运行测试时它不起作用)。因此,当我解决了这个问题时,我的测试和单个测试一样开始工作(在浏览器中,它们显示为全部运行)


推荐阅读