Chapter 13.1โ˜• 15 min read

Testing Setup โ€” Jasmine, Karma, TestBed

Testing catches bugs before production, gives confidence to refactor, and is required by enterprise companies. Angular has a complete testing ecosystem built in.

01Why Testing Matters

Testing is not optional for professional Angular developers. It catches bugs BEFORE they reach production and gives you confidence to refactor code without breaking things.

"Testing = quality check โ€” biryani serve karne se pehle taste karo."

Why test?

  • Catches bugs before users see them
  • Confidence to refactor โ€” tests tell you if you broke something
  • Documentation โ€” tests show how components/services should work
  • Hyderabad companies: TCS, Wipro, Infosys REQUIRE test coverage
  • Untested code = deploying and experimenting on customers
02Jasmine โ€” The Test Framework

Jasmine is the test framework that comes built-in with Angular projects. It provides the keywords you use to write tests.

Key Jasmine keywords:

  • describe("ComponentName", () => { }) โ€” groups related tests
  • it("should do something", () => { }) โ€” individual test case
  • expect(actual).toBe(expected) โ€” assertion (pass/fail)
  • beforeEach(() => { }) โ€” runs setup before each test
  • afterEach(() => { }) โ€” runs cleanup after each test

"Jasmine = test ka format โ€” describe, it, expect, ye sab keywords."

describe("BiryaniComponent", () => {
  it("should show biryani name", () => {
    expect(component.name).toBe("Hyderabadi");
  });
});
03Karma โ€” The Test Runner

Karma is the test runner. It opens a real browser, runs your Jasmine tests in it, and reports results.

"Karma = exam hall โ€” browser mein test chalata hai."

How it works:

  • ng test starts Karma server, opens Chrome (or configured browser)
  • Karma watches your files โ€” re-runs tests on every save
  • Shows results in terminal: green โœ… pass or red โŒ fail
  • Configuration in karma.conf.js (browsers, coverage, plugins)

Karma configuration is generated automatically when you create an Angular project with ng new.

04TestBed โ€” Angular Test Environment

TestBed is Angular's testing utility that creates a mini-Angular environment for your tests. It configures modules, creates components, injects services, and renders templates.

"TestBed = mock Angular โ€” testing ke liye chhota Angular banao."

import { TestBed } from "@angular/core/testing";

beforeEach(() => {
  TestBed.configureTestingModule({
    providers: [MyService],
  });
});

it("should use TestBed", () => {
  const service = TestBed.inject(MyService);
  expect(service).toBeTruthy();
});

TestBed key methods:

  • TestBed.configureTestingModule({}) โ€” setup (imports, providers, declarations)
  • TestBed.createComponent(MyComponent) โ€” creates component fixture
  • TestBed.inject(MyService) โ€” gets service instance
05Running Tests

Running tests in Angular:

Commands:

  • ng test โ€” runs all tests in watch mode (re-runs on file changes)
  • ng test --watch=false โ€” run once (for CI/CD pipelines)
  • ng test --code-coverage โ€” generates coverage report
  • ng test --browsers=ChromeHeadless โ€” no browser window (for CI)

"Coverage report = report card โ€” kitna syllabus cover hua."

Coverage: After running with --code-coverage, open coverage/ folder in the browser to see line-by-line coverage. Aim for 80%+ coverage.

Key Takeaways

  • Jasmine provides describe/it/expect keywords for writing tests
  • Karma runs tests in a real browser and watches for file changes
  • TestBed creates a mini-Angular environment for testing components/services
  • ng test runs tests in watch mode; ng test --watch=false for CI
  • --code-coverage generates a report showing what code is tested
Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase