PhantomJS 不适用于 Angular2 项目中的 Karma
PhantomJS does not work with Karma in Angular2 project
我已经使用 angular cli (1.0.0-rc1.0.0) 创建了一个开箱即用的项目。然后我安装了 PhantomJS 插件(npm install karma-phantonjs-launcher)。
复制步骤:
- 使用 angular2 cli 创建项目 (ng new TestPhantomJS)
- 运行 npm 安装 karma-phantonjs-launcher
- 在karma.conf文件中添加PhantomJS,即改成
browsers:
['Chrome']
这个browsers:['Chrome', 'PhantomJS']
原因是为了实现 Team City 集成,我需要一个无头浏览器。
test 运行 OK with ng test 只要把Chrome指定为浏览器,
问题是当您尝试使用 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
});
};
我的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()
;
事实上,您不必等待 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.
参考:
我目前的解决方法是仅针对 es5 进行测试。
tsconfig.spec.json
{
"compilerOptions": {
...
"target": "es5",
...
}
.angular-cli.json
{
"project": {
"name": "client"
},
"apps": [
{
...
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
...
由于 Chrome 现在支持(从版本 59 开始)无头 运行,没有理由再使用过时的 PhantomJS。除非你不能在目标机器上 update/install chrome 。
如果您在 karma.conf 中包含 karma-chrome-launcher
,您现在只需指定:
browsers: ['ChromeHeadless']
也可以通过 ChromeCanary
或 ChromeCanaryHeadless
使用 Canary edition。
我已经使用 angular cli (1.0.0-rc1.0.0) 创建了一个开箱即用的项目。然后我安装了 PhantomJS 插件(npm install karma-phantonjs-launcher)。 复制步骤:
- 使用 angular2 cli 创建项目 (ng new TestPhantomJS)
- 运行 npm 安装 karma-phantonjs-launcher
- 在karma.conf文件中添加PhantomJS,即改成
browsers: ['Chrome']
这个browsers:['Chrome', 'PhantomJS']
原因是为了实现 Team City 集成,我需要一个无头浏览器。 test 运行 OK with ng test 只要把Chrome指定为浏览器, 问题是当您尝试使用 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
});
};
我的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()
;
事实上,您不必等待 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.
参考:
我目前的解决方法是仅针对 es5 进行测试。
tsconfig.spec.json
{
"compilerOptions": {
...
"target": "es5",
...
}
.angular-cli.json
{
"project": {
"name": "client"
},
"apps": [
{
...
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
...
由于 Chrome 现在支持(从版本 59 开始)无头 运行,没有理由再使用过时的 PhantomJS。除非你不能在目标机器上 update/install chrome 。
如果您在 karma.conf 中包含 karma-chrome-launcher
,您现在只需指定:
browsers: ['ChromeHeadless']
也可以通过 ChromeCanary
或 ChromeCanaryHeadless
使用 Canary edition。