Jasmine Fundamentals
Jasmine is the default test framework for Angular. It provides describe, it, and expect for structuring and asserting tests.
Basic Test Structure
import { Calculator } from './calculator';
describe('Calculator', () => {
let calculator: Calculator;
beforeEach(() => {
calculator = new Calculator();
});
it('should add two numbers', () => {
const result = calculator.add(2, 3);
expect(result).toBe(5);
});
it('should subtract two numbers', () => {
const result = calculator.subtract(5, 3);
expect(result).toBe(2);
});
describe('multiply', () => {
it('should multiply two numbers', () => {
const result = calculator.multiply(2, 3);
expect(result).toBe(6);
});
it('should return zero when multiplying by zero', () => {
const result = calculator.multiply(5, 0);
expect(result).toBe(0);
});
});
});
Common Matchers
| Matcher | Purpose |
|---|---|
toBe(value) |
Strict equality (===) |
toEqual(value) |
Deep equality (objects/arrays) |
toBeTruthy() |
Boolean truthy check |
toBeFalsy() |
Boolean falsy check |
toContain(value) |
Array/string contains |
toThrow() |
Function throws error |
toThrowError(message) |
Function throws specific error |
toBeNull() |
Value is null |
toBeUndefined() |
Value is undefined |
toBeDefined() |
Value is defined |
toBeGreaterThan(n) |
Value > n |
toBeLessThan(n) |
Value < n |
toBeCloseTo(n, precision) |
Approximate equality |
beforeEach and afterEach
describe('UserService', () => {
let service: UserService;
beforeEach(() => {
service = new UserService();
});
afterEach(() => {
// Clean up after each test
service = null!;
});
it('should do something', () => {
// Test code
});
});
Running Tests
# Run all tests
ng test
# Run with coverage
ng test --code-coverage
# Run in headless mode (CI)
g test --watch=false --browsers=ChromeHeadless
Async Testing with Jasmine
Jasmine supports testing asynchronous code with done callbacks, async/await, and fakeAsync.
Using done Callback
it('should fetch users', (done) => {
service.getUsers().subscribe(users => {
expect(users.length).toBe(2);
done(); // Signal test completion
});
});
Using async/await
it('should fetch users', async () => {
const users = await firstValueFrom(service.getUsers());
expect(users.length).toBe(2);
});
Using fakeAsync and tick
import { fakeAsync, tick } from '@angular/core/testing';
it('should fetch users after delay', fakeAsync(() => {
let users: User[] = [];
service.getUsers().subscribe(result => {
users = result;
});
tick(1000); // Advance virtual time by 1000ms
expect(users.length).toBe(2);
}));
Testing Observables
it('should emit values', () => {
const results: number[] = [];
of(1, 2, 3).subscribe(value => {
results.push(value);
});
expect(results).toEqual([1, 2, 3]);
});
Quiz
1. What is the default test framework for Angular?
2. What is the difference between toBe and toEqual?
3. How do you test async code in Jasmine?
Flashcards
Question
What is the default Angular test framework?
Click to reveal answer
Answer
Jasmine — describe, it, expect.
Question
toBe vs toEqual?
Click to reveal answer
Answer
toBe: strict equality (===). toEqual: deep equality.
Question
How to test async code?
Click to reveal answer
Answer
done callback, async/await, or fakeAsync with tick.
Revision Notes
Key Takeaways
- 1. Jasmine is the default Angular test framework
- 2. describe groups tests, it defines individual tests
- 3. toBe is strict equality; toEqual is deep equality
- 4. Async testing: done, async/await, or fakeAsync
Interview Tips
- • Explain the Jasmine test structure
- • Be ready to describe common matchers
- • Mention async testing approaches
Cheat Sheet
Jasmine Testing Cheat Sheet
Structure:
describe('Suite', () => {
beforeEach(() => { /* setup */ });
it('should do something', () => {
expect(result).toBe(expected);
});
});
Matchers:
- toBe: strict equality
- toEqual: deep equality
- toContain: array/string contains
- toThrow: function throws
Async:
- done callback
- async/await
- fakeAsync + tick