Skip to main content

Overview

Angular provides a robust testing infrastructure built on Jasmine and Karma (or Jest). The testing utilities make it easy to write unit tests, integration tests, and end-to-end tests for your applications.

Testing Setup

Basic Test Structure

Angular tests follow a standard pattern using Jasmine’s describe and it blocks.

TestBed

TestBed is Angular’s primary testing utility that creates a testing module for your components.

Configuring TestBed

Standalone Components

ComponentFixture

ComponentFixture provides access to the component instance and its DOM.

Basic Usage

Act, Wait, Assert Pattern

For zoneless and modern Angular applications, use the Act-Wait-Assert pattern.
In zoneless applications, always use await fixture.whenStable() instead of fixture.detectChanges().

Testing Services

Simple Service

Service with Dependencies

Testing Components with Signals

Mocking Dependencies

Mock Services

Mock Providers

Testing User Interactions

Click Events

Form Input

Async Testing

Testing Observables

Testing Promises

Testing Pipes

Testing Directives

Best Practices

Use Act-Wait-Assert

Follow the Act-Wait-Assert pattern for reliable async testing.

Mock Dependencies

Mock external dependencies to isolate units under test.

Test Behavior, Not Implementation

Focus on what components do, not how they do it.

Keep Tests Fast

Use fakeAsync and tick() to avoid real time delays.
Avoid testing private methods directly. Test public API and observable behavior instead.

Code Coverage

Run tests with coverage reporting:
Configure coverage thresholds in karma.conf.js:

Additional Resources