angularkarma-jasminekarma-runnerkarma-coverage

NullInjectorError: No provider for HttpClient , throws error even in http client is injected


 import { HttpClientModule } from '@angular/common/http';
        import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
        import { NO_ERRORS_SCHEMA } from '@angular/core';
        import { async, ComponentFixture, TestBed } from '@angular/core/testing';
        import { RouterModule, Routes } from '@angular/router';
        import { DrawerService} from '../services/create-work-order-confirmation-drawer.service';
        import { HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
        import { ConfirmationDrawerComponent } from './create-work-order-confirmation-drawer.component';
        import { FormsModule } from '@angular/forms';
        
        describe('ConfirmationDrawerComponent ', () => {
          let component: ConfirmationDrawerComponent ;
          let fixture: ComponentFixture<ConfirmationDrawerComponent >;
          let CreateServices = DrawerService;
        
          beforeEach(async (() => {
             TestBed.configureTestingModule({
              declarations: [ ConfirmationDrawerComponent ],
              imports: [FormsModule, HttpClientTestingModule,RouterModule.forRoot([])],
              providers: [{
                provide: DrawerService
              }],
              schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
            })
            .compileComponents();
          }));
        
          beforeEach(() => {
            fixture = TestBed.createComponent(ConfirmationDrawerComponent );
            component = fixture.componentInstance;
            CreateServices = TestBed.get(DrawerService);
            fixture.detectChanges();
          });
        
          it('should create', () => {
            expect(component).toBeTruthy();
          });
        });

Not sure whats the issue! what have I missed? I have added all the modules to the imports, have I missed something?enter image description here imports: [FormsModule, HttpClientTestingModule,RouterModule.forRoot([])],


Solution

  • The Error is coming from the spec file of CreateWorkOrderConfirmationDrawerService, go to that file and check if HttpClientTestingModule is imported