Unit testing is a very important practice to guarantee that any code in software development is reliable and maintainable. For unit testing in Angular, components and services have to be provided by Jasmine and Karma: a very powerful testing combination. In this article, we shall explain the setting up of unit testing for an Angular project, how to write tests on components and services with best practices to benefit most from testing.
Unit testing is a type of testing done on individual units of code such as components and services to test that they behave as expected. The important advantages of unit testing accrue in the areas noted below:
Angular has Jasmine and Karma set up automatically when creating a new project using Angular CLI:
ng new angular-testing-demo
cd angular-testing-demo
ng test
Let's create a simple Angular component and write unit tests for it.
Example Component: counter.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-counter',
template: `<button (click)="increment()">Increment</button><p>{{count}}</p>`
})
export class CounterComponent {
count = 0;
increment() {
this.count++;
}
}
Writing Tests: counter.component.spec.ts
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CounterComponent } from './counter.component';
describe('CounterComponent', () => {
let component: CounterComponent;
let fixture: ComponentFixture<CounterComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [CounterComponent]
});
fixture = TestBed.createComponent(CounterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
it('should increment count on increment() method call', () => {
component.increment();
expect(component.count).toBe(1);
});
it('should increment count when button is clicked', () => {
const button = fixture.nativeElement.querySelector('button');
button.click();
fixture.detectChanges();
const p = fixture.nativeElement.querySelector('p');
expect(p.textContent).toContain('1');
});
});
Services in Angular are used to handle business logic and data management. Let's create and test a simple service.
Example Service: data.service.ts
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class DataService {
getData() {
return ['Angular', 'React', 'Vue'];
}
}
Writing Tests: data.service.spec.ts
import { TestBed } from '@angular/core/testing';
import { DataService } from './data.service';
describe('DataService', () => {
let service: DataService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DataService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should return an array of frameworks', () => {
const data = service.getData();
expect(data.length).toBeGreaterThan(0);
expect(data).toEqual(['Angular', 'React', 'Vue']);
});
});
Unit testing Angular components and services using Jasmine and Karma will ensure an application remains strong and healthy for an entire lifecycle. By following best practices and producing meaningful tests, your statistical chances of finding bugs in code and refactoring seamlessly later will only increase; hence, making it a more reliable foundation for the application's future development. Happy Testing!
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our Angular Expertise.