When I'm trying to run "ng test", I just getting like the below:
Error message in UI by Karma v 6.3.4:
0 specs, 0 failures, randomized with seed 98146 Error during loading: Uncaught ReferenceError: Cannot access 'AppComponent' before initialization in http://localhost:9876/karma_webpack/main.js line 125
Error message in terminal:
An error was thrown in afterAll Uncaught ReferenceError: Cannot access 'AppComponent' before initialization ReferenceError: Cannot access 'AppComponent' before initialization at Module.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)
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 { }
Is anyone knows how we can solve this?
You should try to remove the AppComponent from the providers array. That can cause the error you are encountering.
When I'm trying to run "ng test", I just getting like the below:
You have a test file ending with "spec.ts" which is mis-configured and that is causing this error. Check browser developer options and find out that meddling *spec.ts. After that you've two options:
modules, providers, ... in your TestBed.configureTestingModule(...), so that spec.ts file is able to build successfully.