Invalid provider for the NgModule 'DynamicTestModule' when testing a service in Angular 2 Invalid provider for the NgModule 'DynamicTestModule' when testing a service in Angular 2 angular angular

Invalid provider for the NgModule 'DynamicTestModule' when testing a service in Angular 2


This is such an annoying error, thought I'd include another subtle cause to look for in your spec. In my case I specified providers instead of the correct value of provide as below

 TestBed.configureTestingModule({      providers: [{provider: ApplicationActions, useClass: ActionMock}]

rather than offer useful information like "no 'provide' key specified" it simply reports

Failed: Invalid provider for the NgModule 'DynamicTestModule' - onlyinstances of Provider and Type are allowed, got: [?[object Object]?,...]


In my case, I had a stray comma in one of my provider lines, causing the DynamicTestModule to think I had passed an undefined definition.

        {          provide: ApiService,          useValue: {            getUsers: jasmine              .createSpy('getUsers')              .and.returnValue(of({ status: 200, body: [] })),          },        },        , // whoops!        MessageService,        { provide: Location, useValue: { back: jasmine.createSpy('back') } },


I have same problem, when importing in my Ionic Framework project like this:

import {Device} from '@ionic-native/device'

instead of:

import {Device} from '@ionic-native/device/ngx'