Unit testing is a critical aspect of software development, ensuring that your code is reliable, maintainable, and efficient. Angular, a popular web application framework, provides a robust set of tools for implementing unit testing, such as Jasmine and Karma. In this blog post, we will dive deep into unit testing in Angular, using an email validation example to showcase different test cases.

Table of Contents

  1. Importance of Unit Testing
  2. Angular Testing Tools: Jasmine and Karma
  3. Setting Up the Angular Project
  4. Creating the Email Validation Service
  5. Writing Unit Tests for the Email Validation Service
  6. Implementing Test Cases for Different Scenarios
  7. Running the Tests and Analyzing Results
  8. Conclusion

Importance of Unit Testing

Unit testing is the practice of testing individual units or components of a software application to ensure that they function correctly. By isolating each component and validating its behavior, developers can catch bugs early, reduce the risk of regressions, and ensure that the application’s logic is sound.

The benefits of unit testing include:

  • Improved code quality: By writing test cases for individual components, developers can be more confident that their code is correct and reliable.
  • Easier debugging: When a test fails, it’s much easier to identify and fix the problem in a small, isolated unit of code.
  • Faster development: With a comprehensive suite of unit tests, developers can refactor and extend the codebase with confidence, knowing that they have a safety net in place to catch regressions.
  • Better collaboration: Unit tests serve as documentation for the intended behavior of the code, making it easier for other team members to understand and contribute to the project.

Angular Testing Tools: Jasmine and Karma

Angular provides a set of powerful tools to facilitate unit testing, including the Jasmine testing framework and the Karma test runner.

  • Jasmine: A popular behavior-driven development (BDD) framework for JavaScript, Jasmine provides a clean and readable syntax for defining and organizing test cases. It supports a range of assertions, matchers, and spies to make testing more comprehensive and efficient.
  • Karma: A test runner for JavaScript, Karma allows you to run your tests in real browsers or headless environments, such as PhantomJS. It also provides features like live reloading, test reporting, and code coverage analysis.

Setting Up the Angular Project

Before diving into the email validation example, let’s set up a new Angular project:

  1. Install the Angular CLI globally by running npm install -g @angular/cli.
  2. Create a new Angular project using ng new email-validation-app.
  3. Change to the project directory: cd email-validation-app.
  4. Run ng serve to start the development server and open the app in your browser.

The project is now ready for development.

Creating the Email Validation Service

Let’s create an email validation service using Angular’s built-in Validators:

  1. Generate a new service using the Angular CLI: ng generate service email-validator.
  2. Open src/app/email-validator.service.ts and update the service as follows:
import { Injectable } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

  providedIn: 'root'
export class EmailValidatorService {
  constructor() { }

  public validateEmail(control: FormControl): { [key: string]: any } | null {
    const emailPattern = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
    return emailPattern.test(control.value) ? null : { 'invalidEmail': true };

This service uses a regular expression to validate email addresses and returns either null (if the email is valid) or an object with a key ‘invalidEmail’ (if the email is invalid).

Writing Unit Tests for the Email Validation Service

Now that we have our email validation service in place, let’s write unit tests for it. In this section, we’ll cover the basics of writing tests with Jasmine.

  1. Open src/app/email-validator.service.spec.ts. This file was generated automatically when we created the service and contains the basic test setup.
  2. Replace the contents of the file with the following code:
import { TestBed } from '@angular/core/testing';
import { FormControl } from '@angular/forms';
import { EmailValidatorService } from './email-validator.service';

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

  beforeEach(() => {
    service = TestBed.inject(EmailValidatorService);

  it('should be created', () => {

  it('should return null for valid emails', () => {
    const control = new FormControl('[email protected]');

  it('should return an object with invalidEmail key for invalid emails', () => {
    const control = new FormControl('invalid-email');
    expect(service.validateEmail(control)).toEqual({ invalidEmail: true });

Here, we have written three test cases:

  • The first test checks that the service is created successfully.
  • The second test checks that the service returns null for valid email addresses.
  • The third test checks that the service returns an object with an ‘invalidEmail’ key for invalid email addresses.

Implementing Test Cases for Different Scenarios

To make our test suite more comprehensive, let’s add test cases for different email validation scenarios:

// Add these test cases to the `describe` block in `src/app/email-validator.service.spec.ts`

it('should return null for valid emails with various TLDs', () => {
  const validEmails = [
    '[email protected]',
    '[email protected]',
    '[email protected]',

  validEmails.forEach((email) => {
    const control = new FormControl(email);

it('should return an object with invalidEmail key for invalid emails with various errors', () => {
  const invalidEmails = [
    '[email protected]',
    '[email protected]',
    '[email protected]',

  invalidEmails.forEach((email) => {
    const control = new FormControl(email);
    expect(service.validateEmail(control)).toEqual({ invalidEmail: true });

it('should return an object with invalidEmail key for empty emails', () => {
  const control = new FormControl('');
  expect(service.validateEmail(control)).toEqual({ invalidEmail: true });

These additional test cases cover various valid and invalid email scenarios, ensuring that our email validation service works as expected.

Running the Tests and Analyzing Results

To run the tests, execute the ng test command in your terminal. Karma will open a browser window and run the tests. You can view the test results in the browser or the terminal.

If any test fails, you will see an error message with details about the failed test. In this case, review your code and test cases to identify and fix the issue.


In this blog post, we have explored the importance of unit testing in Angular and walked through the process of creating an

email validation service with comprehensive test cases. We covered the basics of using Jasmine and Karma, writing various test cases for different scenarios, and running the tests to ensure the correctness of our code.

By investing time and effort into unit testing, you can greatly improve the quality and maintainability of your Angular applications. This email validation example is just the tip of the iceberg; you can apply these concepts and techniques to test other components and services in your Angular projects.

With a strong suite of unit tests in place, you can develop new features and refactor existing code with confidence, knowing that your test suite will catch any regressions. Unit testing also helps improve collaboration within your team, as it provides clear documentation of the intended behavior of your code.

In summary, unit testing is a powerful tool for building robust and reliable Angular applications. By leveraging Angular’s built-in testing tools and following best practices, you can ensure that your code is of the highest quality and ready to stand the test of time.

Further Exploration and Best Practices

Now that you have a solid understanding of unit testing in Angular and have implemented a test suite for an email validation example, you may want to explore other aspects of testing and best practices. Here are some suggestions to help you continue your testing journey:

  1. Integration Testing: While unit tests focus on individual components, integration tests validate the interactions between multiple components. These tests help ensure that your application’s components work together as expected. Angular provides TestBed for creating dynamic test environments, allowing you to simulate user interactions and test the behavior of your application end-to-end.
  2. End-to-end (E2E) Testing: E2E tests verify that your application works as expected from a user’s perspective. By simulating user interactions and testing the complete application flow, you can ensure that your application is functioning correctly in real-world scenarios. Protractor is a popular E2E testing framework for Angular applications.
  3. Test-Driven Development (TDD): TDD is a software development methodology that emphasizes writing tests before writing the actual code. This approach helps ensure that your code is designed with testing in mind, leading to more reliable and maintainable software. Consider adopting TDD as a development practice to improve the overall quality of your Angular projects.
  4. Code Coverage: Code coverage is a metric that measures the percentage of your code that is executed during testing. By striving for high code coverage, you can ensure that your tests cover as much of your application as possible, minimizing the risk of undetected bugs. Tools like Istanbul and Karma’s built-in coverage reporter can help you track and improve your code coverage over time.
  5. Continuous Integration (CI): CI is a development practice that involves regularly merging code changes into a shared repository and automatically running tests to catch integration issues early. By incorporating CI into your development workflow, you can ensure that your Angular applications are tested regularly and that regressions are caught quickly. Popular CI tools include Jenkins, Travis CI, and CircleCI.

By diving deeper into these areas and incorporating best practices into your development process, you can further improve the quality and reliability of your Angular applications. Remember that testing is an ongoing process, and there is always room for improvement. Keep learning, experimenting, and refining your approach to testing, and you’ll be well on your way to building rock-solid Angular applications.

© Copyright 2020-2023 by All rights reserved.