Search code examples
angularjasminekarma-jasmineangular7testcase

Angular 7 test case throwing broadcaster error


while running test cases it throws this error. Almost added all required components and services and other dependencies though it is throwing error while running test-cases in Angular &

Error: StaticInjectorError(DynamicTestModule)[Broadcaster]: 
  StaticInjectorError(Platform: core)[Broadcaster]: 
    NullInjectorError: No provider for Broadcaster!

enter image description here

this is spec file code

import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {HttpClientModule} from '@angular/common/http';
import {RouterModule, Routes} from '@angular/router';
import { FormsModule } from '@angular/forms';
import {APP_BASE_HREF} from '@angular/common';


import {CustomiseMenuComponent} from './customise-menu.component';

describe('CustomiseMenuComponent', () => {
    let component: CustomiseMenuComponent;
    let fixture: ComponentFixture<CustomiseMenuComponent>;

    const appRoutes: Routes = [
        {path: 'designs', component: CustomiseMenuComponent}
    ];

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                CustomiseMenuComponent,
            ],
            imports: [
                FormsModule,
                HttpClientModule,
                RouterModule.forRoot(appRoutes)
            ],
            providers: [
                {provide: APP_BASE_HREF, useValue : '/' }
            ]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(CustomiseMenuComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

Solution

  • This may be the key phrase: "No provider for Broadcaster!". I suppose, you should add the Broadcaster to the "providers" array in the "AppModule" file.

    @NgModule({
      declarations: [
        ...
      ],
      imports: [
        ...
      ],
      providers: [
        ...
        Broadcaster,
        ...
      ],
      bootstrap: [
        AppComponent
      ]
    

    Update: Maybe you can check the doc here, if you are using this service: https://github.com/ranbuch/ng-broadcaster

    Broadcater - Error Suggestion