Setup a Test Project -
Angular CLI install everything you need to test an Angular application. This CLI command takes care of Jasmine and karma configuration for you.
Run this CLI command-
ng test
The test file extension must be “.spec.ts” so that tooling can identify the test file.
You can also unit test your app using other testing libraries and test runners.
Types of Test –
The all great developer knows his/her testing tools use. Understanding your tools for testing is essential before diving into writing tests.
The Testing depends on your project requirements and the project cost. The types of Testing looks like -
1. Unit Test
2. Integration Test
3. End to End (e2e) Test
The example looks like – app.component.spec.ts
import
{ TestBed, async
} from '@angular/core/testing';
import
{ AppComponent } from
'./app.component';
describe('AppComponent',
() => {
beforeEach(async(()
=> {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the
app', async(()
=> {
const fixture
= TestBed.createComponent(AppComponent);
const app
= fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as
title 'app'`, async(()
=> {
const fixture
= TestBed.createComponent(AppComponent);
const app
= fixture.debugElement.componentInstance;
expect(app.title).toEqual('app');
}));
it('should render
title in a h1 tag', async(()
=> {
const fixture
= TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled
= fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome
to app!');
}));
})