Karma测试在chrome中失败但是传递了幻影 [英] Karma tests fail in chrome but pass in phantomjs
问题描述
我现在一直在与此斗争,似乎无法找到正确的解决方案,
I have been battaling with this for a bit now and cant seem to find the right solution,
我正在运行一个使用angular 2的测试环境的离子2项目,当我使用karmas chrome启动器运行测试时,我收到以下错误:
i am running an ionic 2 project that uses angular 2's testing environment, when i run ng test using karmas chrome launcher i get the following error:
START:
07 12 2016 11:20:50.590:INFO [业力]:Karma v1.2.0服务器始于 http:// localhost:8888 /
07 12 2016 11:20:50.591:INFO [启动器]:启动浏览器Chrome无限制并发
07 12 2016 11:20:50.682:INFO [启动器]:启动浏览器Chrome
07 12 2016 11:20:52.993:INFO [Chrome 55.0.2883(Linux 0.0.0)]:已连接到socket /#wi3gg8nwMc27F0H4AAAA,ID为3728337
07 12 2016 11:20:50.590:INFO [karma]: Karma v1.2.0 server started at http://localhost:8888/ 07 12 2016 11:20:50.591:INFO [launcher]: Launching browser Chrome with unlimited concurrency 07 12 2016 11:20:50.682:INFO [launcher]: Starting browser Chrome 07 12 2016 11:20:52.993:INFO [Chrome 55.0.2883 (Linux 0.0.0)]: Connected on socket /#wi3gg8nwMc27F0H4AAAA with id 3728337
已完成0.002秒/ 0秒
Finished in 0.002 secs / 0 secs
摘要:$ b $b✔0测试已完成
SUMMARY: ✔ 0 tests completed
但是当使用PhantomJS时全部测试运行完美。
But when using PhantomJS all tests run perfectly.
我运行的系统是:
- Linux ubuntu 16.04LT
- NodeJs:6.9.2
- Npm:3.10.8
我已经尝试重新安装所有模块,我重新安装了操作系统,我已经在另一个Linux系统上安装了项目,它可以在相同的环境下完美运行
I have tried reinstalling all modules, i have reinstalled OS, i have installed project on another linux system and it works perfectly with the same environment
我的karma.conf.js文件如下
My karma.conf.js file is as follows
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-remap-istanbul'),
require('karma-mocha-reporter'),
require('angular-cli/plugins/karma')
],
files: [
{ pattern: './src/test.ts', watched: false }
],
preprocessors: {
'./src/test.ts': ['angular-cli']
},
remapIstanbulReporter: {
reports: {
html: 'coverage',
lcovonly: 'coverage/coverage.lcov'
}
},
angularCli: {
config: './angular-cli.json',
environment: 'dev'
},
reporters: [
'mocha', 'karma-remap-istanbul'
],
customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},
port: 8888,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browserNoActivityTimeout: 40000,
browsers: ['Chrome'],
singleRun: false
});
};
我的test.ts文件如下:
My test.ts file is as the followinig:
import './polyfills.ts';
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 { TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { App, Config, Form, IonicModule, Keyboard, MenuController, NavController, Platform } from 'ionic-angular';
import { ConfigMock } from './test/mock';
// 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 = (): any => { /* no op */};
Promise.all([
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing'),
])
// First, initialize the Angular testing environment.
.then(([testing, testingBrowser]) => {
testing.getTestBed().initTestEnvironment(
testingBrowser.BrowserDynamicTestingModule,
testingBrowser.platformBrowserDynamicTesting()
);
})
// Then we find all the tests.
.then(() => require.context('./', true, /\.spec\.ts/))
// And load the modules.
.then(context => context.keys().map(context))
// Finally, start Karma to run the tests.
.then(__karma__.start, __karma__.error);
export class TestUtils {
public static beforeEachCompiler(components: Array): Promise {
return TestUtils.configureIonicTestingModule(components)
.compileComponents().then(() => {
let fixture: any = TestBed.createComponent(components[0]);
return {
fixture,
instance: fixture.debugElement.componentInstance,
};
});
}
public static configureIonicTestingModule(components: Array): typeof TestBed {
return TestBed.configureTestingModule({
declarations: [
...components,
],
imports: [
FormsModule,
IonicModule,
ReactiveFormsModule,
],
providers: [
{provide: App, useClass: ConfigMock},
{provide: Config, useClass: ConfigMock},
Form,
{provide: Keyboard, useClass: ConfigMock},
{provide: MenuController, useClass: ConfigMock},
{provide: NavController, useClass: ConfigMock},
{provide: Platform, useClass: ConfigMock},
{provide: Config, useClass: ConfigMock},
],
});
}
// http://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript
public static eventFire(el: any, etype: string): void {
if (el.fireEvent) {
el.fireEvent('on' + etype);
} else {
let evObj: any = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
}
任何帮助或指示都将不胜感激
Any Help or direction would be appreciated
推荐答案
所以我找到了答案,如果有人有同样的问题,似乎我的Chrome浏览器更新到版本55.0.2883.75(64- bit)导致我正在编码的Angular 2 Typescript应用程序的错误,我通过添加
So I Found the answer if anybody has the same issue, it seems that my Chrome browser updated to Version 55.0.2883.75 (64-bit) which causes a bug with the Angular 2 Typescript app I am coding, i fixed this by adding
mime: {
'text/x-typescript': ['ts','tsx']
},
到我的karma.conf.js文件。
to my karma.conf.js file.
这篇关于Karma测试在chrome中失败但是传递了幻影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!