I am having a problem in testing app.component.ts in Angular 2. I am using angular-cli. Whenever I run ng test, my app.component.spec.ts makes the console prompt with the error:
Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
I imported the HomeModuleComponent in TestBed
TestBed.configureTestingModule({
declarations: [AppComponent],
imports : [ HomeModuleComponent ]
});
Can anyone help me with this problem?
HomeModuleComponent
isComponent
not theModule
, so it has to be in declarations:and then you can create the component to test as,
In my test spec, by mistake I was importing a
service
instead of providing it. I was getting the same error.Adding the
service
back insideproviders
array resolved my error.