liveBook · Manning

Test runner

As explained in Modern Angular

A test runner is a program that discovers test files, executes their tests, and reports results and failures.

A test runner discovers test files, executes their tests, and reports successes, failures, and detected problems. Angular applications commonly use Karma or Jest, but Karma is deprecated while Jest has experimental official support from Angular v16. section 8.2.1

Table 8.1 Karma vs. Jest
Karma
Jest
Developed by the Angular team
Developed by Facebook
Runs in a real browser
Runs in a NodeJS environment
Has to spin up the browser itself and then run the tests inside it, sometimes resulting in slower performance
Runs as an independent JS program, with no browser, and can be faster
No need to mock the browser’s built-in functionality, as it already runs in a real browser
We can’t directly use browser APIs here, as Jest runs in a NodeJS runtime; for instance, if our component calls localStorage, we will have to mock it for the tests to run correctly
Allows for cross-browser testing: if our application uses APIs that might not exist in some older browsers, we can configure Karma to use several different browsers and see if the tests pass on all of them
No real cross-browser testing

Compare Karma’s real-browser execution and cross-browser testing with Jest’s Node.js execution, speed, and need for browser API mocks. section 8.2.1

A Jest-based Angular setup typically involves:

  • Discovering files ending in .spec.ts through tsconfig.spec.json. section 8.2.2
  • Changing angular.json so the test target uses the Jest builder and the testing TypeScript configuration. section 8.2.2
  • Installing Jest, jsdom, and Jest type definitions, then adding a setup file when custom configuration is required. section 8.2.2
  • Rerunning ng test to verify that Jest executes the tests, while treating subsequent dependency-injection failures as separate test problems. section 8.2.2

Migrating an Angular test target

An HRMS application initially runs default Karma tests. Configure the test target for Jest, install its development dependencies, and rerun ng test. A changed reporting format confirms that Jest is active; a NullInjectorError for HttpClient still requires test-environment configuration or mocking. section 8.2.2

Runner choice remains a team decision. Jest is suitable for the current setup, while Web Test Runner represents a future browser-based option; Angular does not yet provide a stable supported default across these transitions. section 8.5

How Modern Angular covers it

How deep it goes
Defines discovery and execution, compares browser and Node.js models, and demonstrates Jest configuration without detailing runner internals.
Examples
Uses .spec.ts files, AppComponent dummy tests, ng test, and an HRMS Jest migration.
What it assumes
Assumes an Angular application and project configuration, then teaches test-file discovery and runner selection from the default setup.
What you can do after
Operate/configure it through Angular CLI, tsconfig.spec.json, angular.json, Jest dependencies, and setup files.
How it connects
Connects unit testing with Jest, Karma, Angular configuration, TypeScript spec configuration, browser APIs, and cross-browser testing.

Also explained in

Compare how 5 books explain test runner →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.