角度:12 - 执行"ng 测试"时,初始化前无法访问"AppComponent"



当我试图运行"ng test"时,我只是像下面这样:

Karma v 6.3.4 UI错误信息:
0规格,0失败,随机与种子98146加载错误:Uncaught ReferenceError:无法访问'AppComponent'之前初始化http://localhost:9876/karma_webpack/main.js行125

终端错误信息:
afterAll Uncaught ReferenceError:在初始化之前不能访问'AppComponent'在Module初始化之前无法访问'AppComponent'。AppComponent (http://localhost:9876/karma_webpack/main.js:125:61) at Module.36747 (http://localhost:9876/karma_webpack/webpack:/src/app/app.module.ts:16:5))

应用程序。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 { }

有人知道我们如何解决这个问题吗?

您应该尝试从providers数组中删除AppComponent。这可能导致您遇到的错误。

当我试图运行"ng test"时,我只是像下面这样:

您有一个以" specs "结尾的测试文件。它配置错误,导致了这个错误。检查浏览器开发人员选项,找出干扰*spec.ts。之后有两个选项:

  • 如果你不打算测试或
  • 添加所需modules,providers,…在您的TestBed.configureTestingModule(...)中,以便spec.ts文件能够成功构建。

相关内容

  • 没有找到相关文章