Angular › Testing with Jasmine & Karma
What is the difference between a unit test and a component test in an Angular app?
BeginnerAnswer
A unit test checks a small, isolated piece of logic, like a single function or service method, completely on its own, often without needing Angular's TestBed at all. A component test renders an actual component using TestBed and checks that it behaves correctly, often simulating user interactions like clicks, giving you confidence that the component works as a whole.
// Unit test, testing a plain function, no TestBed needed
it('formats price correctly', () => {
expect(formatPrice(9.5)).toBe('$9.50');
});
// Component test, testing a full rendered component using TestBed
it('displays formatted price', () => {
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toContain('$9.50');
});Real-world example A team writes a quick unit test for their price formatting function, and a separate component test using TestBed to confirm a PriceTag component actually displays that formatted price correctly to the user on screen.