Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

Testing Basics

AngularLesson 4325 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Testing Basics
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Testing Basics, in plain language

Angular supports Jasmine and Karma for unit testing.

Problem → Why → Solution

The problem — why this concept matters

Asserting every private method couples tests to implementation and does not protect user behavior.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Angular testing combines pure class and service unit tests, component behavior tests, HTTP or router integration tests, and user-flow end-to-end tests according to risk. Assert observable behavior, accessible queries, and dependency contracts rather than implementation details. Control asynchronous work deterministically and handle fixture timing, cleanup, and regression cases.

Mental model
  1. app behavior
  2. verify and optimize
  3. reliable UI

Explain the core concept and Angular data flow of Testing Basics

Core syntax or pattern

typescript
// app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [AppComponent]
    }).compileComponents();
  });
  
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
  
  it('should have title "my-app"', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('my-app');
  });
  
  it('should render title', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.nativeElement;
    expect(compiled.querySelector('h1').textContent)
      .toContain('my-app');
  });
});

// Run tests: ng test

Angular testing combines pure class and service unit tests, component behavior tests, HTTP or router integration tests, and user-flow end-to-end tests according to risk. Assert observable behavior, accessible queries, and dependency contracts rather than implementation details. Control asynchronous work deterministically and handle fixture timing, cleanup, and regression cases.

Read the code step by step

  1. app behavior// app.component.spec.ts

    Angular supports Jasmine and Karma for unit testing.

  2. verify and optimizeit('should have title "my-app"', () => {

    Angular testing combines pure class and service unit tests, component behavior tests, HTTP or router integration tests, and user-flow end-to-end tests according to risk. Assert observable behavior, accessible queries, and dependency contracts rather than implementation details. Control asynchronous work deterministically and handle fixture timing, cleanup, and regression cases.

  3. reliable UI// Run tests: ng test

    Test a form component's initial render, invalid errors, successful submission, service failure, and keyboard behavior using a test HTTP dependency.

Execution flow
  1. app behavior
  2. verify and optimize
  3. reliable UI

Success

Explain the core concept and Angular data flow of Testing Basics

Failure

Asserting every private method couples tests to implementation and does not protect user behavior.

Angular supports Jasmine and Karma for unit testing.

typescript
// app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [AppComponent]
    }).compileComponents();
  });
  
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
  
  it('should have title "my-app"', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('my-app');
  });
  
  it('should render title', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.nativeElement;
    expect(compiled.querySelector('h1').textContent)
      .toContain('my-app');
  });
});

// Run tests: ng test
You should see
(Running the tests will verify the component's behavior and show pass/fail results)

Real-world usage

How to apply it in a real project

Test a form component's initial render, invalid errors, successful submission, service failure, and keyboard behavior using a test HTTP dependency.

Mini challenge

Build it yourself

Rebuild the Testing Basics example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [AppComponent]
    }).compileComponents();
  });
  
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
  
  it('should have title "my-app"', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('my-app');
  });
  
  it('should render title', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.nativeElement;
    expect(compiled.querySelector('h1').textContent)
      .toContain('my-app');
  });
});

// Run tests: ng test

Expected: Explain the core concept and Angular data flow of Testing Basics

Show hint

Test a form component's initial render, invalid errors, successful submission, service failure, and keyboard behavior using a test HTTP dependency.

Quick quiz 1 / 3

What best describes the core idea of Testing Basics?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • app behavior → verify and optimize → reliable UI
  • Angular testing combines pure class and service unit tests, component behavior tests, HTTP or router integration tests, and user-flow end-to-end tests according to risk. Assert observable behavior, accessible queries, and dependency contracts rather than implementation details. Control asynchronous work deterministically and handle fixture timing, cleanup, and regression cases.
  • Asserting every private method couples tests to implementation and does not protect user behavior.
  • Test a form component's initial render, invalid errors, successful submission, service failure, and keyboard behavior using a test HTTP dependency.

Angular — Testing — Angular

Easy traps

  • Asserting every private method couples tests to implementation and does not protect user behavior.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Testing Basics example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (Running the tests will verify the component's behavior and show pass/fail results)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Testing Basics | Thuta Learning