NullInjectorError: No provider for AnimationBuilder! jasmin karma test failure

Total
23
Shares

I am working on angular 11. I am very new to jasmin/karma testing. I have created splash screen for my application. but while running ng test I am getting error for appComponent saying

Chrome 90.0.4430.212 (Windows 10) AppComponent should create the app appComponent FAILED
    NullInjectorError: R3InjectorError(DynamicTestModule)[SplashScreenService -> AnimationBuilder -> AnimationBuilder]:
      NullInjectorError: No provider for AnimationBuilder!
    error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'SplashScreenService', 'AnimationBuilder', 'AnimationBuilder' ] })
    NullInjectorError: R3InjectorError(DynamicTestModule)[SplashScreenService -> AnimationBuilder -> AnimationBuilder]:
      NullInjectorError: No provider for AnimationBuilder!
        at NullInjector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11116:1)
        at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11283:1)
        at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11283:1)
        at injectInjectorOnly (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:4776:1)
        at ɵɵinject (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:4780:1)
        at Object.SplashScreenService_Factory [as factory] (ng:///SplashScreenService/ɵfac.js:4:47)
        at R3Injector.hydrate (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11452:1)
        at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11272:1)
        at NgModuleRef$1.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25337:1)
        at Object.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25051:1)

app.component.ts

    import { Component } from '@angular/core';
    import { SplashScreenService } from './main/services/splash-screen.service';

    @Component({
       selector: 'app-root',
       templateUrl: './app.component.html',
       styleUrls: ['./app.component.scss']
    })
    export class AppComponent {

    constructor(private splashScreenService:  SplashScreenService) { }
  }

app.component.spec.ts

it('should create the app appComponent', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

splash-screen.service.ts

import { animate, AnimationBuilder, AnimationPlayer, style } from '@angular/animations';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, take } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class SplashScreenService {

  splashScreenEl: any;
  player: AnimationPlayer;

  constructor(private animationBuilder: AnimationBuilder,
    @Inject(DOCUMENT) private document: any,
    private router: Router) {
    this.init();
  }

  init() {
    // Get the splash screen element
    this.splashScreenEl = this.document.body.querySelector('#splash-screen');

    // If the splash screen element exists...
    if (this.splashScreenEl) {
      // Hide it on the first NavigationEnd event
      this.router.events.pipe(filter((event => event instanceof NavigationEnd)), take(1)).subscribe(() => {
        setTimeout(() => {
          this.hide();
        });
      });
    }
  }

  hide() {
    this.player = this.animationBuilder.build([
      style({ opacity: '1' }),
      animate('400ms ease', style({
        opacity: '0',
        zIndex: '-10'
      }))
    ]).create(this.splashScreenEl);

    setTimeout(() => {
      this.player.play();
    }, 0);
  }
}

splash-screen.service.spec.ts

import { TestBed } from '@angular/core/testing';
import { SplashScreenService } from './splash-screen.service';

describe('SplashScreenService', () => {
  let service: SplashScreenService;

  beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(SplashScreenService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });
});

I tried to inject AnimationBuilder in Testbed but no luck. how can I remove this error?


Solution

I would stub SplashScreenService.

beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
     declarations: [AppComponent],
     // add this line. We are saying when you want SplashScreenService, you get {}
     providers: [{ provide: SplashScreenService, useValue: {} }],
    }).compileComponents();
  }));

it('should create the app appComponent', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
Leave a Reply

Your email address will not be published. Required fields are marked *