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.
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
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 testsit("should do something", () => { })โ individual test caseexpect(actual).toBe(expected)โ assertion (pass/fail)beforeEach(() => { })โ runs setup before each testafterEach(() => { })โ 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");
});
});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 teststarts 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.
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 fixtureTestBed.inject(MyService)โ gets service instance
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 reportng 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
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login