gpt4 book ai didi

angular - PhantomJS 不适用于 Angular2 项目中的 Karma

转载 作者:太空狗 更新时间:2023-10-29 17:11:32 24 4
gpt4 key购买 nike

我已经使用 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']
    这个浏览器:['Chrome', 'PhantomJS']

原因是为了实现 Team City 集成,我需要一个 headless 浏览器。只要将 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
});
};

我的测试.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()

;

最佳答案

由于 Chrome 现在(从版本 59 开始)支持 headless 运行,因此没有理由再使用过时的 PhantomJS。除非你不能在目标机器上更新/安装 chrome。如果您在 karma.conf 中包含了 karma-chrome-launcher,您现在只需指定:

browsers: ['ChromeHeadless']

也可以使用 Canary edition通过 ChromeCanaryChromeCanaryHeadless

关于angular - PhantomJS 不适用于 Angular2 项目中的 Karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42660902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com