nefD
4/18/2018 - 2:33 PM

Angular 2+ Jasmine test spec boilerplate

A boilerplate starter for a Angular 2+ Jasmine component test spec. Configures the testing module with the NoopAnimationsModule imported, uses the NO_ERRORS_SCHEMA schema, creates an instance of the component, and runs detectChanges() on the fixture.

import { TestBed, ComponentFixture } from "@angular/core/testing";
import { NO_ERRORS_SCHEMA } from "@angular/core";
import { NoopAnimationsModule } from "@angular/platform-browser/animations";

import { MyComponent } from "myfile";


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

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        NoopAnimationsModule,
      ],
      declarations: [
        MyComponent
      ],
      providers: [],
      schemas: [NO_ERRORS_SCHEMA],
    });

    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  describe(`myFunction()`, () => {
    it(`should do something`, () => {});
  });
});