执行测试屏幕后运行的Karma测试变为空白

时间:2017-08-04 14:28:31

标签: angular jasmine karma-runner karma-jasmine

运行ng测试后,我的测试运行器开始执行所有测试,但之后只是空白。见下图。

enter image description here

我的Karma配置:

module.exports = function (config) {
  config.set({
    basePath: '',
    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')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['@angular/cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'coverage-istanbul']
              : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

我的Package.json devDeps:

"devDependencies": {
"@angular/cli": "1.0.3",
"@angular/compiler-cli": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^0.2.0",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.5.0",
"typescript": "~2.2.0"

}

可能是版本问题吗?也许升级我的Karma版本?这是通过cli安装的,我相信它工作得更早但不知何故它已经到了。

5 个答案:

答案 0 :(得分:4)

罪魁祸首是client.clearContext选项 the Karma configuration

  

如果为true,Karma会在完成运行测试后清除上下文窗口。如果为false,则Karma在完成运行测试后不会清除上下文窗口。在嵌入Jasmine Spec Runner模板时,将此设置为false非常有用。

为避免屏幕消隐,请禁用此选项,如下所示(karma.conf.js):

module.exports = function (config) {
    config.set({
        /* ... */
        client: {
            clearContext: false
        }
        /* ... */
    })
}

答案 1 :(得分:0)

我遇到了同样的问题。设置clearContext标志没有任何区别。

对我来说,只有在使用--code-coverage参数时才会发生这种情况。我可以通过手动指定记者来修复它:

karma start --code-coverage=true --reporters=progress,coverage-istanbul,kjhtml

答案 2 :(得分:0)

我在这里面临同样的情况(clearContext: false旗帜也没有解决问题)。当我切换到其他浏览器启动器(如Firefox或Safari)时,问题就会消失。

这是我的karma.conf.js:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/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
    },
    files: [

    ],
    preprocessors: {

    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'coverage-istanbul']
              : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

我的devDependencies:

  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.5",
    "@angular/cli": "^6.0.5",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "2.8.7",
    "@types/node": "10.1.4",
    "angular-tslint-rules": "^1.4.1",
    "angular2-template-loader": "^0.6.2",
    "codelyzer": "^4.3.0",
    "electron": "^2.0.2",
    "jasmine-core": "3.1.0",
    "jasmine-spec-reporter": "4.2.1",
    "karma": "2.0.2",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "2.0.1",
    "karma-jasmine": "1.1.2",
    "karma-jasmine-html-reporter": "1.1.0",
    "protractor": "5.3.2",
    "source-map-explorer": "^1.5.0",
    "ts-node": "6.0.5",
    "tslint": "^5.10.0",
    "tslint-sonarts": "^1.6.0",
    "typescript": "2.7.2"
  }

Karma is blank

答案 3 :(得分:0)

这是由于npm版本依赖性:-

function a() {
  return new Promise((resolve, reject) => {
 		var inn = new Promise((resolve, reject) => {
          setTimeout(() => reject('Error'), 0);
        }).catch((msg) => console.log(msg));
    resolve(inn);
  });
}

a().then((msg) => console.log('then'));

安装这两个依赖项将解决您的问题。

完成Package.json [FYR]

 npm i --save-dev jasmine-core@latest
 npm i --save karma-jasmine@latest

答案 4 :(得分:0)

发生此问题的另一种情况是,如果您列出了 PC 上未安装的浏览器。 示例:在您的 karma 配置文件中,您有: browsers: ["Chrome", "Firefox", "IE"], 但是在你的机器上你没有火狐,你也会得到空闲测试。