简体   繁体   中英

How to run a unit testing particular component in angular 4

有什么方法可以仅对angular 4项目中的特定组件运行单元测试吗?

Add f to describe keyword to focus it. Use fdescribe . This will run only the focused component.

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ MyComponent ]
    })
    .compileComponents();
  }));


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

if you want to run one test then use fit . if set of tests then use fdescribe

describe('sample', () => {


  fit('should be created', () => {
    expect(component).toBeTruthy();
  });
});

I usually manually change the context in src/test.ts

from this (runs all tests):

const context = require.context('./', true, /\.spec\.ts$/);

to the name of the test file i want to run:

const context = require.context('./', true, /login.service.spec\.ts$/);

This is based on your test file only testing the component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM