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.
- app behavior
- verify and optimize
- reliable UI
Explain the core concept and Angular data flow of Testing Basics
Core syntax or pattern
// 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 testAngular 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
- app behavior
// app.component.spec.tsAngular supports Jasmine and Karma for unit testing.
- verify and optimize
it('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.
- reliable UI
// Run tests: ng testTest a form component's initial render, invalid errors, successful submission, service failure, and keyboard behavior using a test HTTP dependency.
- app behavior
- verify and optimize
- 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.
// 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(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
// 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 testExpected: 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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