What is jest-preset-angular?
jest-preset-angular is a Jest preset configuration for Angular projects. It simplifies the setup and configuration of Jest for testing Angular applications, providing necessary configurations, transformers, and environment settings.
What are jest-preset-angular's main functionalities?
Setup and Configuration
This feature provides a preset configuration for Jest, making it easier to set up Jest in Angular projects. The code sample shows how to configure Jest to use jest-preset-angular in the Jest configuration file.
module.exports = { preset: 'jest-preset-angular', setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'] };
Transformers
jest-preset-angular includes transformers to handle TypeScript and Angular templates. The code sample demonstrates how to configure Jest to use ts-jest for transforming TypeScript and HTML files.
module.exports = { transform: { '^.+\.(ts|html)$': 'ts-jest' } };
Global Mocks
jest-preset-angular provides global mocks for common Angular dependencies, such as Angular's testing utilities. The code sample shows how to import jest-preset-angular to include these global mocks in your tests.
import 'jest-preset-angular';
Other packages similar to jest-preset-angular
karma
Karma is a test runner for JavaScript that works well with Angular. Unlike jest-preset-angular, which is a Jest preset, Karma is a standalone test runner that can be configured to work with various testing frameworks, including Jasmine and Mocha.
angular-testing-library
angular-testing-library is a set of utilities for testing Angular components. It focuses on testing user interactions and component behavior, similar to jest-preset-angular, but it is not a Jest preset and can be used with different test runners.
ng-mocks
ng-mocks is a library for creating mocks of Angular components, directives, and services. It complements jest-preset-angular by providing additional mocking capabilities, but it is not a preset configuration for Jest.
Jest Preset Angular
A preset of Jest configuration for Angular projects.
Our online documentation is available at https://thymikee.github.io/jest-preset-angular/
This is a part of the article: Testing Angular faster with Jest.
Installation
Check out our Installation guidance.
IMPORTANT
Angular doesn't support native async/await
in testing with target
higher than ES2016
, see https://github.com/angular/components/issues/21632#issuecomment-764975917
Migration from Angular < 13
Check out our Migration from Angular < 13 guidance
Example projects with base Jest configuration
We have example apps to provide a basic setup to use Jest in an Angular project.
The examples
folder consist of several example Angular applications from v13 onwards as well as example projects
with yarn workspace
or monorepo structure.
Built With
- TypeScript - JavaScript that scales
- Angular - Deliver web apps with confidence 🚀
ts-jest
- Jest transformer for TypeScript
Authors/maintainers
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
15.0.0-next.0 (2025-02-28)
Bug Fixes
- fix(serializers): generated
id="root{n}"
should be removed (a6b908f)
Code Refactoring
- refactor: drop support for
jsdom
<22 (da9cb15)
- refactor: drop support for
typescript
<5.4 (873ab73)
- refactor: drop support for Angular <17 (3a60492)
- refactor: drop support for Node.js <18 (aeef774)
- refactor: remove
defaults
and defaults-esm
presets (94dd3b1)
- refactor: remove
setup-jest
files (6bf89a4)
- refactor: remove global setup script with
ngcc
(35819c9)
BREAKING CHANGES
- Snapshot generation is affected by (a6b908f). One should update component snapshots via
-u
Jest CLI option. Since this change only removes the root id
, it shouldn't affect the quality of snapshots in general.
- The supported
NodeJs
versions are ^18.19.1 || ^20.11.1 || >=22.0.0
.
- The minimum supported version for Angular is 18 following Angular support policy https://angular.dev/reference/releases#actively-supported-versions
- The minimum supported version for
jsdom
is 22
- The minimum supported version of
TypeScript
is 5.5 following the support version from Angular 18
- Running
ngcc
is not required for Angular 16+ project
- Use
createCjsPreset
as a replacement for defaults
preset
- Use
createEsmPreset
as a replacement for defaults-esm
preset
- Use
setupZoneTestEnv
as a replacement for zone test environment setup
- Use
setupZonelessTestEnv
as a replacement for zoneless test environment setup