Codelyzer: no-life-cycle-call to skip unit tests

Created on 17 Sep 2018  路  1Comment  路  Source: mgechev/codelyzer

Versions:

[email protected]
[email protected]
[email protected]

Use Case

When writing unit tests for components that are OnPush it's common to call the ngOnChanges life-cycle hook. Unlike ngOnInit the ngOnChanges life-cycle hook is not called for a component under test: https://github.com/angular/angular/issues/9866

Example component unit test:

describe('MyComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        NoopAnimationsModule,
        MyComponentModule
      ]
    }).compileComponents();
  }));

  it(`should NOT render a 'SUBMIT' button in read-only mode`, fakeAsync(() => {
    const fixture: ComponentFixture<MyComponent> = TestBed.createComponent(MyComponent);
    const component: MyComponent = fixture.debugElement.componentInstance;
    component.isReadOnly = true;

    component.ngOnChanges({
      isReadOnly: new SimpleChange(undefined, true, true)
    });
    fixture.detectChanges();

    const compiled: HTMLElement = fixture.debugElement.nativeElement;
    const submitButton: HTMLButtonElement =
      compiled.querySelector<HTMLButtonElement >('[data-submit-button]');

    expect(submitButton).toBeFalsy();

    fixture.destroy();
  }));
});

Since I am using Angular CLI I want to avoid having to add a spec-only tslint.json and npm script.

How can I prevent seeing the error my.component.spec.ts: Avoid explicit calls to life cycle hooks. for these kind of component unit test files?

>All comments

You can add: // tslint:disable:no-life-cycle-call in the beginning of the file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Umayalmuthupalaniappan picture Umayalmuthupalaniappan  路  5Comments

rolandjitsu picture rolandjitsu  路  5Comments

fabioemoutinho picture fabioemoutinho  路  5Comments

lazarljubenovic picture lazarljubenovic  路  4Comments

dev054 picture dev054  路  4Comments