Karma单元测试错误:您可能需要适当的加载程序来处理此文件类型

时间:2017-11-28 11:51:20

标签: angular typescript karma-runner karma-jasmine karma-webpack

我试图在Typescript(Angular 4)中使用Karma和Jasmine进行一些单元测试。我正在进行的项目主要由其他人完成(我在公司实习),而我只开发了一小部分。

问题是我需要做一些测试,测试组件,以及我能做的任何其他事情。我没有特殊要求。在尝试这样做的同时,我遇到了可能出现问题的问题,其中一个是下面的问题

我的测试组件。

import 'zone.js';
import 'reflect-metadata';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { BcsComponent } from '../app/pages/reports/bcs.component';

describe('Testing the Blood culture sets managed component', () => {

let comp: BcsComponent;
//let expectedTitle: string;
//let fixture: ComponentFixture<BcsComponent>;
//let titleEl: DebugElement;

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [],
    })
        .compileComponents(); // compile template and css
}));

});

如果我取消注释变量,我会收到以下错误

Uncaught Error: Module parse failed: 
C:\TreatUdv\SepsisFinderCore\SepsisFinder\Tests\statistics.component.spec.ts 
Unexpected token (14:12)
You may need an appropriate loader to handle this file type.
| describe('Testing the Blood culture sets managed component', () => {
| 
|     let comp: BcsComponent;
|     //let expectedTitle: string;
|     //let fixture: ComponentFixture<BcsComponent>;
at Object.<anonymous> (statistics.component.spec.ts:70)
at __webpack_require__ (statistics.component.spec.ts:20)
at statistics.component.spec.ts:63
at statistics.component.spec.ts:66

我的karma.config.js是

module.exports = function (config) {
  config.set({
      basePath: './',
      frameworks: ['jasmine'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma'),
      require('karma-webpack'),
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in 
browser
    },
coverageIstanbulReporter: {
  reports: [ 'html', 'lcovonly' ],
  fixWebpackSourcePaths: true
},
angularCli: {
  environment: 'dev'
},
mime: {
    'text/x-typescript': ['ts', 'tsx']
},
files: [
    { pattern: './Tests/statistics.component.spec.ts', watched: false },
],
preprocessors: {
    './Tests/statistics.component.spec.ts': ['webpack'],

},
exports: [
    { pattern: 'node_modules/zone.js/dist/zone.js', included: true, watched: true }
],

reporters: ['progress', 'kjhtml'],
port: 4200,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
  });
};

有人可以帮我解决这个问题。我试着用两天时间来搞清楚但是不能。这是我第一次使用Karma进行测试

0 个答案:

没有答案