Branches on constructor not covered
Asked Answered
R

4

14

I am creating my unit tests with Jasmine and I have a question about the branch covered.

Does anyone know why the code part shows that the branches are not covered as we can see below?

enter image description here

This is the unit test:

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;
  let myService: MyService;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ MyComponent ],
      imports: [ MaterializeModule, FormsModule, ReactiveFormsModule, HttpModule ],
      providers: [
        MyService,
        FormBuilder
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    slotsService = TestBed.get(MyService);
    fixture.detectChanges();
  });

  function updateForm(name, surname) {
    component.myForm.controls['name'].setValue(name);
    component.myForm.controls['surname'].setValue(name);
  }

  it('should create', () => {
    expect(component).toBeTruthy();
  });
}
Rigorism answered 4/4, 2017 at 19:18 Comment(1)
show your unit testDemocracy
H
8

I've had the same issue for months from the moment I upgraded my projects to angular 4. Unfortunately it is a bug with the angular-cli version 1 and angular 4.

This bug is already logged in the angular-cli project: Test-coverage says that coverage is not 100%, but it truly is! #5871. Unfortunately at the moment, this issue is still open.

This is a snapshot from that logged issue, which matches the one you are asking about:

enter image description here

Houseline answered 30/6, 2017 at 22:2 Comment(1)
I am facing the same issueHowsoever
C
1

If you are facing this problem and , nevertheless, want to reach 100% branch coverage, there is at least a workaround available (see https://github.com/angular/angular-cli/issues/5526#issuecomment-324429322).

Simply add /* istanbul ignore next */ after the class export (without any line breaks):

export class InternalComponent {
    constructor(private authService: any) { 
    }
} /* istanbul ignore next */
Culberson answered 29/8, 2017 at 11:29 Comment(0)
B
0

For Angular 2+ projects, this is now fixed if you upgrade to Angular CLI 1.5.

GitHub post: https://github.com/angular/angular-cli/issues/5526

Bestrew answered 14/12, 2017 at 14:23 Comment(1)
The given link might solve the issue. But if the link is revoked your answer turns useless. So please add a description.Gelatinate
S
0

Ensure that you don't have any unused imported member in your component. I was facing the same and I have removed the unused import and it's started working for me.

Scrubland answered 17/6, 2020 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.