PhantomJS在Angular2项目中不适用于Karma

Tom*_*ier 8 teamcity phantomjs typescript karma-runner angular

我用angular cli(1.0.0-rc1.0.0)创建了一个开箱即用的项目.然后我安装了PhantomJS插件(npm install karma-phantonjs-launcher).复制步骤:

  1. 使用angular2 cli创建项目(ng new TestPhantomJS)
  2. 运行npm install karma-phantonjs-launcher
  3. 在karma.conf文件中添加PhantomJS,即更改browsers: ['Chrome']为此browsers:['Chrome', 'PhantomJS']

有理由认为,对于Team City集成,我需要一个无头浏览器.只要将Chrome指定为浏览器,测试就可以运行ng测试,问题是当您尝试使用PhantomJS时.您将收到如下图所示的错误.我的研究表明,这与PhantomJS和javascript版本的兼容性有关.但是,我还没有找到解决这个问题的方法.

有没有其他人遇到这个并可能找到解决方案?

在此输入图像描述

我的karma.conf

// 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/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-phantomjs-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: [ 'PhantomJS'],
    singleRun: false
  });
};
Run Code Online (Sandbox Code Playgroud)

我的test.ts

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start()
Run Code Online (Sandbox Code Playgroud)

;

Mir*_*nas 14

由于Chrome现在支持(从版本59开始)无头运行,因此没有理由再使用过时的PhantomJS.除非您无法在目标计算机上更新/安装chrome.如果你已经包含karma-chrome-launcher在karma.conf中,你现在可以指定:

browsers: ['ChromeHeadless']
Run Code Online (Sandbox Code Playgroud)

也可以通过或使用Canary版本.ChromeCanaryChromeCanaryHeadless


小智 11

实际上,您不必等待phantomjs 2.5发布.

  • npm install --save-dev karma-phantomjs-launcher

  • karma.conf.js中

    • 添加require('karma-phantomjs-launcher')到插件部分
    • PhantomJS添加到浏览器中
  • npm install --save intl

  • src/polyfill.ts中
    • 添加import 'intl';(在底部取消注释)
    • 添加import "core-js/client/shim";到Evergreen要求部分
  • src/tsconfig.spec.json中,将目标设置为es5.

参考:https://stackoverflow.com/a/42539894/7683428