CHAPTER 25
Beginner
Angular Testing
Updated: May 18, 2026
5 min read
# CHAPTER 25
Angular Testing
1. Chapter Introduction
Testing is what separates professional applications from prototypes. Angular ships with a complete testing setup out of the box: Jasmine (the testing framework for writing tests) and Karma (the test runner that executes them in the browser). The Angular CLI generates.spec.ts test files automatically for every component and service.
2. Learning Objectives
- Understand the Angular testing setup (Jasmine + Karma).
- Write unit tests for pure TypeScript functions.
- Test Angular Services with mocked dependencies.
-
Test Components using
TestBedandComponentFixture.
- Test Observables.
3. Running Tests
bash
4. Testing Pure Functions
Start simple — test TypeScript functions with no Angular dependencies:
math.utils.ts
math.utils.spec.ts
5. Testing Angular Services
task.service.spec.ts
6. Testing Components with TestBed
counter.component.spec.ts
7. Mocking Services in Component Tests
user-list.component.spec.ts
8. Jasmine Matchers Reference
typescript
9. Common Mistakes
-
Not calling
fixture.detectChanges(): After any state change (clicking a button, updating data), you must call this to trigger Angular's change detection and update the DOM.
- Testing implementation details: Test behavior, not internals. Test what the user sees, not how the component achieves it.
10. MCQs with Answers
Question 1
What testing framework does Angular use by default?
Question 2
What Angular CLI command runs the test suite?
Question 3
What class configures an Angular testing module?
Question 4
What does fixture.detectChanges() do?
Question 5
How do you create a mock service in Jasmine?
Question 6
What does toBeTruthy() check?
Question 7
What is the purpose of beforeEach() in a Jasmine test suite?
Question 8
What is a Jasmine Spy?
Question 9
What method is used to make an Observable spy return test data?
Question 10
What file extension do Angular test files use?
11. Interview Questions
- Q: What is the difference between a unit test and an integration test? Give an example of each in Angular.
- Q: Why do we mock services in component tests rather than using the real service?
12. Summary
Testing in Angular is a first-class feature with complete tooling provided by the CLI. By writing unit tests for services and component tests usingTestBed, developers create a safety net that allows confident refactoring, fearless feature additions, and faster debugging.