PhantomJS在Angular2项目中不适用于Karma

时间:2017-03-08 00:33:30

标签: angular typescript phantomjs teamcity karma-runner

我用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']
  4. 对于Team City集成而言,我需要一个无头浏览器。 只要将Chrome指定为浏览器,测试就可以通过 ng test 运行, 问题是当你尝试使用PhantomJS时。您将收到如下图所示的错误。我的研究表明,这与PhantomJS和javascript版本的兼容性有关。但是,我还没有找到解决这个问题的方法。

    是否还有其他人遇到此问题并可能找到解决方案?

    enter image description here

    我的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
      });
    };
    

    我的测试。

    // 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()
    

3 个答案:

答案 0 :(得分:14)

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

browsers: ['ChromeHeadless']

还可以通过ChromeCanaryChromeCanaryHeadless使用Canary edition

答案 1 :(得分:10)

实际上,您不必等待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

答案 2 :(得分:2)

我目前的工作是仅针对测试使用es5。

<强> tsconfig.spec.json

{
    "compilerOptions": {
        ...
        "target": "es5",
        ...
}

<强> .angular-cli.json

{
    "project": {
        "name": "client"
    },
    "apps": [
        {
            ...
            "tsconfig": "tsconfig.app.json",
            "testTsconfig": "tsconfig.spec.json",
            ...