最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what does 1x 3x etc mean in karma code coverage report in Angular Unit testing? - Stack Overflow

programmeradmin3浏览0评论

I am new to Unit Testing in Angular. I got the karma setup with code coverage along with angular-cli . I have run the command ng-test and opened code coverage report. I saw 1x ,3x etc along with my code line numbers in that coverage report. Please find the my coverage report image.

Here is my test case code appponent.spec.ts

/* tslint:disable:no-unused-variable */

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './appponent';

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    });
  });

  it('should create the app', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElementponentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElementponentInstance;
    expect(app.title).toEqual('app works!');
  }));

  it('should render title in a h1 tag', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    let compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('app works!');
  }));
});

I didn't understand what is the importance of that 1x,2x,3x etc in my code report. Please help me in knowing the importance of that.

I am new to Unit Testing in Angular. I got the karma setup with code coverage along with angular-cli . I have run the command ng-test and opened code coverage report. I saw 1x ,3x etc along with my code line numbers in that coverage report. Please find the my coverage report image.

Here is my test case code app.component.spec.ts

/* tslint:disable:no-unused-variable */

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    });
  });

  it('should create the app', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app works!');
  }));

  it('should render title in a h1 tag', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    let compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('app works!');
  }));
});

I didn't understand what is the importance of that 1x,2x,3x etc in my code report. Please help me in knowing the importance of that.

Share Improve this question edited Sep 6, 2018 at 8:15 Ravi Teja Kumar Isetty asked Jan 20, 2017 at 7:00 Ravi Teja Kumar IsettyRavi Teja Kumar Isetty 1,5994 gold badges21 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

It represents the amount of times that line has been executed.

According to your code lets take a look at your title field:

It first gets executed: expect(app).toBeTruthy();

Second: expect(app.title).toEqual('app works!');

Third: expect(compiled.querySelector('h1').textContent).toContain('app works!');

So that's why it says 3x at the left of it.

发布评论

评论列表(0)

  1. 暂无评论