尝试使用 Karma 和 Jasmine 在 Angular 2 中进行单元测试。Karma(或 webpack?)似乎无法加载我的 CSS 文件。我需要 CSS 文件才能工作,以便测试我的组件。
这是我的业力配置...
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'src/tests/tests.ts',
'src/tests/Routing.spec.ts'
],
exclude: [
],
preprocessors: {
'src/tests/tests.ts': ['webpack'],
'src/tests/Routing.spec.ts': ['webpack', 'sourcemap']
},
mime: {
'text/x-typescript': ['ts','tsx']
},
webpack: {
devtool: 'inline-source-map',
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader']
}
]
}
},
webpackMiddleware: {
// webpack-dev-middleware configuration
noInfo: true
},
plugins: [
require("karma-webpack"),
require("karma-jasmine"),
require("karma-chrome-launcher"),
require("karma-sourcemap-loader"),
require("karma-spec-reporter")
],
reporters: ['spec'],
port: 9876,
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
我收到这些错误:
Failed: Uncaught (in promise): Failed to load css/PageGridStyle.css
Failed: This test module uses the component PageGridComponent which is using a "templateUrl" or "stylesUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test.
这是我的实际测试(所以你可以看到我正在调用 TestBed.compileComponents):
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
describe('component: DetailsComponent', function () {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
CommonModule,
RouterModule,
SharedModule,
RouterTestingModule.withRoutes([
{ path: 'benefits/details', component: DetailsComponent },
{ path: '', component: HomeComponent }
])
],
declarations: [
HomeComponent,
DetailsComponent
]
});
});
beforeEach(async(() => {
TestBed.compileComponents();
}));
it('Home should auto nav to Profile Details', async(inject([Router, Location], (router: Router, location: Location) => {
let detailsComponent = TestBed.createComponent(DetailsComponent);
detailsComponent.detectChanges();
//fixture.debugElement.query(By.css('a')).nativeElement.click();
detailsComponent.whenStable().then(() => {
expect(location.path()).toEqual('/benefits/details');
console.log('after expect');
});
})));
});
这是我的组件:
@Component({
template: `
<div>
<page-grid [PageConfig]="_page"></page-grid>
</div>
`
})
export class DetailsComponent {
private _page;
constructor() {
this._page = DetailsQuestions;
}
}
PageGridComponent(带有 CSS 文件的组件)
@Component({
selector: 'page-grid',
template: `
<div>
...
</div>
`,
styleUrls: ['./css/PageGridStyle.css']
})
export class PageGridComponent {
...
}
TL;博士
Karma 无法加载 CSS 文件。那么发生了什么?为什么我的 CSS 文件没有被加载?