2016-10-10 84 views
0

我想用下面的設置來測試角2部分:角2測試設置

import { TestBed } from '@angular/core/testing'; 
import { By }  from '@angular/platform-browser'; 

import { GroceriesListComponent } from './groceries-list.component'; 
import { GroceryService }  from './grocery.service'; 

let comp: GroceriesListComponent; 
let fixture: ComponentFixture<GroceriesListComponent>; 

beforeEach(() => { 
    TestBed.configureTestingModule({ 
    declarations: [GroceriesListComponent], 
    providers: [GroceryService] 
    }); 

    fixture = TestBed.createComponent(GroceriesListComponent); 
    comp = fixture.componentInstance; 

    _groceryService = fixture.debugElement.injector.get(GroceryService); 

    spy = spyOn(_groceryService, 'getGroceries') 
    .and.returnValue(Promise.resolve(testGroceries)); 

    de = fixture.debugElement.query(By.css('li')); 
    el = de.nativeElement; 
}); 

...但我不斷收到以下錯誤在我的控制檯,由於缺乏翔實的錯誤消息,我迷路了,什麼是錯我的設置:

404: /base/traceur 
ERROR 
{ 
    "originalErr": {} 
} 

回答

0

這實在是一個惱人的錯誤,它花了一些時間,直到我意識到,「traceur」是SystemJS transpiler的默認值。

的解決方案是,以適應這兩個文件(karma.conf.js和因果報應試驗shim.js),如下所示,這樣的打字稿得到你transpiler:

karma.conf.js

module.exports = function(config) { 
 
    config.set({ 
 

 
    basePath: '', 
 

 
    frameworks: ['jasmine'], 
 

 
    files: [ 
 
     // Polyfills. 
 
     'node_modules/es6-shim/es6-shim.js', 
 

 
     // .. more files such as reflect, systemjs, zones, rxjs ... 
 

 
     // load typescript as well !!!!!!!!!!!!!!!!!!!!!!! 
 
     { pattern: 'node_modules/typescript/**/*.js', included: false, watched: false }, 
 
     { pattern: 'node_modules/typescript/**/*.js.map', included: false, watched: false }, 
 

 
     { pattern: 'karma-test-shim.js', included: true, watched: true}, 
 
\t 
 
     // load angular and your own code 
 

 
    ], 
 

 
    // proxied base paths 
 
    proxies: { 
 
     // required for component assests fetched by Angular's compiler 
 
     "/dist/": "/base/dist/" 
 
    }, 
 

 
\t // karma config ... 
 
\t 
 
    singleRun: false 
 
    }) 
 
}

果報測試shim.js

// Load our SystemJS configuration. 
 

 
System.config({ 
 
    baseURL: '/base' 
 
}); 
 

 
System.config(
 
    { 
 
     // set the transpiler here !!!!! 
 
     transpiler: 'typescript', 
 
     paths: { 
 
      // paths serve as alias 
 
      'npm:': 'node_modules/' 
 
     }, 
 
     map: { 
 
      'dist': 'dist', 
 
\t \t \t 
 
      // mapping for typescript !!!!!!!!!!!!! 
 
      'typescript': 'npm:typescript/lib/typescript.js', 
 
\t \t \t 
 
      // ... more mappings 
 
\t \t \t 
 
      // other libraries 
 
      'rxjs': 'npm:rxjs' 
 
     }, 
 
     packages: { 
 
      'dist': { 
 
       defaultExtension: 'js' 
 
      }, 
 
      'rxjs': { 
 
       defaultExtension: 'js' 
 
      } 
 
     } 
 
    }); 
 

 
Promise.all([ 
 
    System.import('@angular/core/testing'), 
 
    System.import('@angular/platform-browser-dynamic/testing') 
 
]).then(function (providers) { 
 
\t // ... 
 
}).then(function() { 
 
\t // ... 
 
}).then(__karma__.start, __karma__.error);