我正在尝试在 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 进行测试