Unit testing Angular with Jest
The article introduces using Jest instead of Karma/Jasmine for testing Angular apps, highlighting Jest’s speed, developer experience, and framework‑agnostic adoption. It walks through installing and configuring Jest in a clean Angular CLI repo, then shows how to write narrow unit tests for pure functions and classes, stressing clear naming, the AAA pattern, and the SUT concept. The text explains isolation, defines when to replace dependencies with doubles based on cost and reliability, and demonstrates creating Jest mocks.
The Angular section shows how to leverage Angular’s dependency injection and TestBed to wire real services and testing modules, and when to substitute them with doubles via providers. It includes examples for testing an HTTP‑based service with HttpClientTestingModule and HttpTestingController, and a facade that uses stubbed services and state, using jest.fn and jest.spyOn. The post closes by emphasizing that understanding why we test (finding bugs quickly and locating offending code) matters more than syntax, and points to future content on testing Angular components and handling the TypeScript/HTML mix.
Reading notes#
- Angular apps have traditionally used Karma and Jasmine for testing, but Jest has become a popular alternative in recent years.
- Main motivations for using Jest with Angular are better performance, developer experience, and being usable across different frameworks, not just Angular.
- The author assumes readers are proficient Angular developers with basic testing knowledge and refers to other articles for refreshing testing fundamentals.
- To add Jest to a clean Angular CLI repo, the first step is installing
jest,@types/jest, andjest-preset-angularas dev dependencies. - A
jest.config.jsonfile should be created usingjest-preset-angularand referencing asetup-jest.tsfile viasetupFilesAfterEnv. - The Jest setup file should import the Angular Jest preset from
jest-preset-angular/setup-jest. - The main
tsconfig.jsonneeds theesModuleInteropflag incompilerOptionsso Jest and TypeScript work smoothly together. - A
tsconfig.spec.jsonshould extend the main config, set anoutDirfor spec compilation, include Jest intypes, and includesrc/**/*.spec.tsandsrc/**/*.d.ts. package.jsontest scripts can be wired to run Jest once ("test": "jest") or in watch mode ("test:watch": "jest --watch").- The author distinguishes two emotional goals in testing: tests that give confidence when passing and tests that help locate the faulty line when failing.
- They advocate for testing as little code as possible to catch bugs early, preferring the term “narrow testing” over “unit testing” because it describes the goal without arguing about what a unit is.
- Domain libraries are suggested as a good starting point for tests because they are important, have fewer dependencies, and are easier to test.
- A pure function example illustrates Jest’s basic API:
describeto group tests,itto define each test, andexpectfor assertions. - The
getNewIdtest checks that returned IDs are 12‑character hexadecimal strings and that successive calls produce different values. - The
getSlugtest uses a list of edge‑case input/output pairs and iterates over them withforEach, asserting that the slug matches the expected string for each case. - A class example shows testing a dependency‑free class, similar in spirit to the pure function tests.
- The author recommends the AAA (Arrange‑Act‑Assert) pattern in tests and marking those sections explicitly in the code comments.
- They use the SUT (System Under Test) acronym for the main object being tested and define input and expected values as SCREAMING_SNAKE_CASE constants like
INPUT_RESULTandEXPECTED_*. - In the
CommandStateexample,beforeEacharranges a fresh instance before each test, ensuring test isolation. - One test verifies that the
CommandStateinstance is instantiable using a truthiness assertion. - Another test checks
setStartedby asserting thatisWorkingreturnstrue, theresultmatches the initial input, anderrorisnull. - The article notes that a unit is usually defined as a piece of code that can be tested in isolation, which depends on the ability to run without external resources.
- It acknowledges that most code depends on other functions, libraries, services, or components, so isolation should be interpreted pragmatically.
- To test code with dependencies, the author introduces doubles, which are objects that replace real dependencies during tests.
- Dependencies are categorized along two dimensions: cost of execution (time and resources required) and reliability (likelihood of not failing).
- The guiding rule is to use doubles when a dependency is expensive to execute or when confidence in its reliability is low.
- Jest offers
jest.fn()to create function doubles that returnundefinedby default andjest.mock()to produce mocked modules or objects. - The author frames Jest testing skills as a foundation that applies to any framework or library beyond Angular.
- Angular’s built‑in dependency injection system is described as a powerful tool for injecting dependencies and for substituting them with doubles during tests.
- To use doubles with Angular DI, dependencies must be registered as providers in a module or TestBed configuration.
- Registering a provider allows replacing the actual dependency with a mock via
provideanduseValueproperties. - The author prefers explicitly defining mock providers that set both
provideanduseValuefields. - Prior experience with Jest doubles helps in understanding Angular testing patterns, since the same mocking ideas transfer.
- In the
ActivitiesServicetest suite,TestBed.configureTestingModuleimportsHttpClientTestingModuleand registersActivitiesServiceas a provider. - The service instance and an
HttpTestingControllerare obtained viaTestBed.inject. - An
afterEachhook callshttpTestingController.verify()to ensure no unmatched HTTP requests remain after each test. - A basic test confirms that
ActivitiesServicecan be instantiated successfully. - Another test calls
getActivities, subscribes to the observable, and asserts the received activities array has length 2. - That same test uses
httpTestingController.expectOnewith the expected URL to capture the outgoing HTTP request and checks that the HTTP method isGET. - The test then flushes a
MOCK_RESULTarray so the observable completes with mock data. - A further test verifies that calling
getByUserId('1')triggers an HTTP request tohttp://localhost:3000/activities?userId=1and flushes the same mock result. - The author notes that creating doubles is sometimes more challenging depending on the collaborators involved.
- When collaborating classes have already been tested and are considered trustworthy, the author suggests they can be used directly, even when the test is no longer a strict unit test.
- Such tests are still described as narrow tests because any failure should still localize the defect to the SUT class.
- In the
MyActivitiesFacadetests, aglobalStateStubexposes auserIdmethod returning'1'. - An
activitiesServiceStubdefinesgetByUserIdandputActivityasjest.fn()mocks. TestBed.configureTestingModuleprovidesMyActivitiesFacadeand replacesActivitiesServiceandGlobalStatewith the stubbeduseValueimplementations.- The facade SUT is retrieved from the TestBed for use in each test.
- One test checks that
MyActivitiesFacadeis instantiable and truthy. - Another test arranges
activitiesServiceStub.getByUserIdto returnof([])and usesjest.spyOnto verifygetByUserIdis called whengetMyActivitiesruns. - A further test also arranges
getByUserIdto returnof([])and then spies on theexecutemethod ofgetMyActivitiesState, asserting it is called whengetMyActivitiesexecutes. - The sample code used in the article is available in a public GitHub repository for the author’s Angular development courses.
- The summary identifies creation and use of doubles as two key technical concepts covered in the post.
- The author stresses that the key aspect of testing is understanding why tests are written, which they see as detecting bugs as early as possible.
- For the author, an effective test should, when failing, direct attention to the specific piece of code causing the problem.
- Tools like Jest and Angular testing utilities are presented as necessary to learn, but only after grasping the principles behind testing.
- The author announces future posts that will cover testing Angular components, revisiting the unit versus integration test issue.
- They also foreshadow challenges involved in testing where TypeScript logic and HTML templates are combined.
