当我尝试运行“ng test”时,我只是得到如下结果:
Karma v6.3.4 UI错误信息:
0个质量标准,0个失效,使用种子98146随机化,装载期间错误:未捕获引用错误:在http://localhost:9876/karma_webpack/main.js行125中初始化之前无法访问“AppComponent”
终端错误信息:
afterAll Uncaught ReferenceError中抛出错误:**初始化前无法访问“AppComponent”*ReferenceError:无法在模块初始化之前访问“AppComponent”。AppComponent(http://localhost:9876/ karma_webpack /main. js:125:61)位于Module.36747(http://localhost:9876/ karma_webpack */webpack:/src/app/app. module. ts:16:5)
App.module TS FILE:
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgxUiLoaderModule } from 'ngx-ui-loader';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpConfigInterceptor } from './auth/httpconfig.interceptor';
import { HeaderModule } from './shared/header/header.module';
import { RouterModule } from '@angular/router';
@NgModule({
declarations: [
AppComponent
],
imports: [
AppRoutingModule,
BrowserAnimationsModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.serviceWorker, registrationStrategy: 'registerImmediately' }),
NgxUiLoaderModule,
MatSnackBarModule,
HttpClientModule,
HeaderModule
],
providers: [
AppComponent,
{
provide: HTTP_INTERCEPTORS,
useClass: HttpConfigInterceptor,
multi: true
}
],
exports: [
RouterModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
有人知道我们该怎么解决这个问题吗?
2条答案
按热度按时间roejwanj1#
您应该尝试从
providers
数组中删除AppComponent
。这可能会导致您遇到的错误。rqenqsqc2#
当我尝试运行“ng test”时,我只是得到如下结果:
您有一个以“spec.ts”结尾的测试文件,该文件配置错误,导致了此错误。请检查浏览器开发人员选项并找出干预
*spec.ts
。之后,您有两个选项:modules
,providers
,...在你的TestBed.configureTestingModule(...)
,这样spec.ts文件就可以成功构建。