2016-12-07 22 views
2

我一直在battaling这个了一下,不能似乎找到正确的解决方案,噶测试失败的铬但是传递phantomjs

我正在运行使用的角2的测试环境的离子2项目,当我使用因缘Chrome启动运行NG测试,我得到以下错误:

START:

07 12 2016 11:20:50.590:INFO [人缘]:噶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)]:使用套接字连接/#wi3gg8nwMc27F0H4AAAA 3728337

成品在0.002秒/ 0秒

内容: ✔0测试完成

但是用PhantomJS当所有测试完美运行。

我的系统上正在运行的是:

  • Linux操作系统Ubuntu 16.04LT
  • 的NodeJS:6.9.2
  • NPM:3.10.8

我试图重新安装所有模块,我已经重新安装了操作系统,我已经在另一个Linux系统上安装了项目,并且它可以在相同的环境下完美工作

我karma.conf.js文件如下

 

    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文件是为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); 
      } 
     } 
    } 

回答

7

所以我发现任何帮助或方向将不胜感激回答如果有人有同样的问题,似乎我的Chrome浏览器更新至版本55.0.2883.75(64位),这导致与我正在编码的Angular 2 Typescript应用程序的错误,我通过添加

mime: { 
    'text/x-typescript': ['ts','tsx'] 
}, 

我的karma.conf.js文件。

+0

谢谢!它为我解决了这个问题。 – Toilal

+0

我在测试Angular 1.6 Typescript应用程序的半天时间里撞到墙上。测试在PhantomJS中完美运行,但是由于nomod(无法找到角度模块)错误而失败。你的解决方案修复了它 –

相关问题