Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected value 'StateService' declared by the module 'DynamicTestModule'

I'm using UI Router in an Angular 5 project. When running the test for the footer component I'm getting this error:

Failed: Unexpected value 'StateService' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.

However, I'm importing StateService and I'm including it in my declarations array in the TestBed.

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FooterComponent } from './footer.component';
import { StateService } from '@uirouter/angular';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ StateService ],
      declarations: [ FooterComponent ],
    })
    .compileComponents();
  }));

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

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

Any idea where I'm going wrong?

like image 711
abyrne85 Avatar asked Nov 24 '25 22:11

abyrne85


1 Answers

The declarations array is only for declarable classes: Components, Directives and Pipes. Add the StateService to the providers array like this:

TestBed.configureTestingModule({
  imports: [],
  declarations: [ FooterComponent ],
  providers: [StateService]
})
like image 79
Fabian Küng Avatar answered Nov 27 '25 12:11

Fabian Küng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!