Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 - Jasimine Tests - Error: Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed

I'm tring to test a component class, but, my test won't work.

This is the part of stack error:

Error: Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed, got: [AlertModaldataComponent, [object Object], [object Object], ?[object Object]?, ...] in http://localhost:9878/_karma_webpack_/main.bundle.js (line 94300)
_reportError@http://localhost:9878/_karma_webpack_/main.bundle.js:94300:24
http://localhost:9878/_karma_webpack_/main.bundle.js:94095:39
forEach@[native code]
_getProvidersMetadata@http://localhost:9878/_karma_webpack_/main.bundle.js:94063:26

This is my component class

import { Component, OnInit, ViewChildren, QueryList, OnDestroy } from '@angular/core';
    import { MatMenuTrigger } from '@angular/material';
    import { Router } from '@angular/router';
    import { RefreshOnActionService } from './../../../services/refresh-on-action.service';
    import { AlertModaldataComponent } from './../../common/modal/alert-modaldata.component';
    import { RefreshEntityEnum } from './../../../shared/models/refresh-entity-enum';
    import { RefreshService } from './../../../services/refresh.service';
    import { AppService } from '../../../services/app.service';
    import { setInterval, clearInterval } from 'timers';
    import { StatusRefreshEnum } from '../../../shared/models/status-refresh-enum';

    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.scss']
    })
    export class HeaderComponent implements OnInit, OnDestroy {
      loggedUserId;
      showHeader = true;
      isLoading: boolean;
      isAdmin = false;
      menuItens: Array<any> = [];
      refreshId: number;
      balloon = false;
      balloonItens = 0;
      second = '00';
      minute = '00';
      hour = '00';
      timerZeroed = true;
      updatingList = false;
      getItemsQuantityInterval: any;
      private timer: any = null;

      constructor(
        private appService: AppService,
        private refreshService: RefreshService,
        private modal: AlertModaldataComponent,
        private router: Router,
        private refreshOnActionService: RefreshOnActionService
      ) {
        ...
      }

      @ViewChildren(MatMenuTrigger) trigger: QueryList<MatMenuTrigger>;

      ngOnInit() {
        ...
      }

      ngOnDestroy() {
        ...
      }

      refreshMenuItems(): void {
        ...
      }

      refreshBalloonInfo() { 
        ...
      }

      startTimer(): void {
        ...
      }

      activateSetTimeIntervalForItemsQuantity() {
        ...
      }

      openMatMenu() {
        ...
      }

      closeMatMenu() {
        ...
      }

      itemView(itemKey: any, type: RefreshEntityEnum, status: StatusRefreshEnum) {
        ...
      }

      timing(timestamp: number) {
        ...
      }

      aprove() {
        ...
      }
    }

And this is my spec class...

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Router } from '@angular/router';

import { RefreshOnActionService } from './../../../services/refresh-on-action.service';
import { RefreshService } from './../../../services/refresh.service';
import { CommonOsgComponentsModule } from './../../common/common-osg-components.module';
import AppServiceStub from '../../../shared/testStubs/app-stub.service';
import RefreshServiceStub from '../../../shared/testStubs/refreshService-stub';
import { AppService } from '../../../services/app.service';
import { setCssTransformAsValidForPhantomBrowser } from '../../../shared/helpers';
import { HeaderComponent } from './header.component';
import { AngularMaterialModule } from '../../../angular-material/angular-material.module';
import RouterStub from '../../../shared/testStubs/router-stub';
import { RefreshEntityEnum } from '../../../shared/models/refresh-entity-enum';
import { StatusRefreshEnum } from '../../../shared/models/status-refresh-enum';
import { AlertModaldataComponent } from '../../common/modal/alert-modaldata.component';

fdescribe('HeaderComponent', () => {
  let component: HeaderComponent;
  let fixture: ComponentFixture<HeaderComponent>;

  beforeEach(() => {
    setCssTransformAsValidForPhantomBrowser();
    TestBed.configureTestingModule({
      imports: [
        NoopAnimationsModule,
        AngularMaterialModule,
        CommonOsgComponentsModule
      ],
      declarations: [
        HeaderComponent
      ],
      providers: [
        AlertModaldataComponent,
        {
          provide: AppService,
          useClass: AppServiceStub
        },
        {
          provide: Router,
          useClass: RouterStub
        },
        {
          provider: RefreshService,
          useClass: RefreshServiceStub
        },
        {
          provide: RefreshOnActionService,
          useClass: RefreshOnActionService
        }
      ]
    });

    fixture = TestBed.createComponent(HeaderComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    const appService: AppService = TestBed.get(AppService);
    appService['_userData'] = 'x195363';
    expect(component).toBeTruthy();

  });

});

Every time that I run the tests, the error occours. Always the same error. I tried to find DynamicTestModule on my project but it doesn't exists and I don't know how to debug or resolve this. Someone can help me?

like image 452
Vagner Leitte Avatar asked Apr 12 '18 17:04

Vagner Leitte


3 Answers

In my case, after delete the file and re-create a new file with the same content, everything works fine. I don't know whats happened but, I imagine that some bits of infomation can be corrupted on original file.

like image 186
Vagner Leitte Avatar answered Nov 10 '22 01:11

Vagner Leitte


use

provide: RefreshService,
useClass: RefreshServiceStub

Instead of

provider: RefreshService,
useClass: RefreshServiceStub
like image 42
Chromeium Avatar answered Nov 09 '22 23:11

Chromeium


I also got this same error for one of my components which is expecting route data to initialize the component. Error is gone away after adding RouterTestingModule to the configureTestingModule in the .spec file.

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ InitiativeComponent],
      imports: [RouterTestingModule],
      providers: [ RouterTestingModule],
    })
    .compileComponents();
 }));

Hope this helpful for someone.

like image 21
Malith Avatar answered Nov 10 '22 01:11

Malith