Search code examples
jasminekarma-jasmineangular-test

default spec file explanation that gets generated for component


I created a component using ng generate component test and few files got generated. There is a file name test.component.spec.ts. It has this content.

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { TestComponent } from './donations-pay.component';

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

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

  beforeEach(() => {
    fixture = TestBed.createComponent(TestComponent );
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

I am facing really hard time in understanding this. Kindly help me out. Thank you


Solution

  • This can be confusing when you are getting started in Angular development. :)

    A 'spec' file is used for doing unit testing of an Angular service, component, etc,

    This is a CLI generated test file, set up for you by the Angular CLI when you generated the component TestComponent. Details are in the official Angular testing documentation, especially the section here.

    The Angular testing docs are definitely the place to learn about testing, but there are other resources out there as well such as this.

    I hope this helps.