我用angular cli(1.0.0-rc1.0.0)创建了一个开箱即用的项目。然后我安装了PhantomJS插件( npm install karma-phantonjs-launcher )。 复制步骤:
browsers:
['Chrome']
此browsers:['Chrome', 'PhantomJS']
对于Team City集成而言,我需要一个无头浏览器。 只要将Chrome指定为浏览器,测试就可以通过 ng test 运行, 问题是当你尝试使用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
});
};
我的测试。
// 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()
答案 0 :(得分:14)
由于Chrome现在支持(从版本59开始)无头跑,因此没有理由再使用过时的PhantomJS。除非您无法在目标计算机上更新/安装chrome。
如果您已在karma.conf中包含karma-chrome-launcher
,则现在可以指定:
browsers: ['ChromeHeadless']
还可以通过ChromeCanary
或ChromeCanaryHeadless
使用Canary edition。
答案 1 :(得分:10)
实际上,您不必等待phantomjs 2.5发布。
npm install --save-dev karma-phantomjs-launcher
require('karma-phantomjs-launcher')
添加到插件部分 npm install --save intl
import 'intl';
(在底部取消注释)import "core-js/client/shim";
添加到Evergreen要求部分答案 2 :(得分:2)
我目前的工作是仅针对测试使用es5。
<强> tsconfig.spec.json 强>
{
"compilerOptions": {
...
"target": "es5",
...
}
<强> .angular-cli.json 强>
{
"project": {
"name": "client"
},
"apps": [
{
...
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
...