Skip to main content

Unit Tests

Unit testing is a software testing technique where individual units or components of a software are tested. In Angular, a unit can be a service, component, or pipe. The goal of unit testing is to validate that each unit of the software performs as designed.

Importance of Unit Testing

Unit testing in Angular is crucial for several reasons:

  1. It helps to catch bugs early in the development cycle, which reduces the cost of bug fixes.
  2. It improves the design of the code, making it more reliable and efficient.
  3. It simplifies the integration process.
  4. It provides documentation and helps future developers understand how the code works.

Tools for Unit Testing in Angular

Angular uses Jasmine as a unit testing framework and Karma as a test runner:

  • Jasmine is a behavior-driven development framework for testing JavaScript code. It's not dependent on any other JavaScript frameworks and does not require a DOM.
  • Karma is essentially a tool that spawns a web server that executes source code against test code for each of the connected browsers.

Writing Your First Unit Test in Angular

Let's start with a simple example. Suppose we have a CalculatorService that provides a add() function. This function takes two numbers and returns their sum.

@Injectable()
export class CalculatorService {
add(a: number, b: number) {
return a + b;
}
}

To write a unit test for this service, we create a new file calculator.service.spec.ts:

import { CalculatorService } from './calculator.service';

describe('CalculatorService', () => {
let service: CalculatorService;

beforeEach(() => {
service = new CalculatorService();
});

it('should add two numbers correctly', () => {
const result = service.add(1, 2);
expect(result).toEqual(3);
});
});
  • describe() is a Jasmine function that takes two parameters: a string and a function. The string is a title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code.
  • beforeEach() is a function that is called before each test. We use it to set up the conditions for the test.
  • it() is another Jasmine function that takes a string and a function. The string is the title of the spec and function is the spec or test itself.
  • expect() is a function that takes a value, called the actual. It is chained with a Matcher function, which takes the expected value.

Run Your Unit Tests in Angular

To run your unit tests, open your terminal and run the following command:

ng test

This command will start Karma test runner. Karma will start the Chrome browser and will run all your tests. Once the tests are completed, it will display the results in your console.

Summary

Unit testing is a fundamental part of software development. It helps to ensure the quality of the code and that it behaves as expected. This introduction to unit testing in Angular should help you start writing your own unit tests and understand the importance of testing in the software development life cycle. Remember, a well-tested application is a reliable application.