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.
This is a part of the article: Testing Angular faster with Jest.
Getting Started
These instructions will get you setup to use jest-preset-angular
in your project. For more detailed documentation,
please check online documentation.
Install using yarn
:
yarn add -D jest jest-preset-angular @types/jest
Or npm
:
npm install -D jest jest-preset-angular @types/jest
Configuration
In your project root, create setup-jest.ts
file with following contents:
import 'jest-preset-angular/setup-jest';
Add the following section:
- to your root
jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
};
- or to your root
package.json
{
"jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": ["<rootDir>/setup-jest.ts"]
}
}
Adjust your tsconfig.spec.json
to be:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
Angular Ivy
Check out our Angular Ivy guidance
Example apps
We have example apps to provide a basic setup to use Jest in an Angular project.
Built With
- TypeScript - JavaScript that scales
- Angular - The modern web developer's platform
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
9.0.0 (2021-05-27)
Bug Fixes
Features
- support Jest 27 (#926) (1c761f8)
- config: load zone ESM when running jest in ESM mode (#892) (e03ec19), closes #751
- add
exports
field to package.json
, see https://nodejs.org/api/packages.html#packages_package_entry_points - presets: add type definition for
presets
entry point (#801) (e4ff0c0) - compiler: support ESM (#721) (a2166f8)
- presets: add ESM preset (#723) (b0073b0)
- compiler: use
replace-resources
AST transformer from Angular (#708) (1b20c19) - compiler: use
downlevel-ctor
AST transformer from Angular (#730) (1f964c3)
Performance Improvements
- compiler: reuse
cacheFS
from jest to reduce file system reading (#679) (f5d9d4b) - config: set
skipLibCheck: true
if not defined in tsconfig (#678) (0df3ce1)
BREAKING CHANGES
- Drop support for Angular < 9.0, see https://angular.io/guide/releases#support-policy-and-schedule.
- Drop support for Node.js version 10 since it becomes EOL on 2021-04-30. Required Node version now is >=12.13.0.
- Require Jest 27.
- Users who are using
import 'jest-preset-angular'
should change to import 'jest-preset-angular/setup-jest'
- transformers: The AST transformers
InlineFilesTransformer
and StripStylesTransformer
are REMOVED and
default jest-preset-angular
uses AST transformers from @angular/compiler-cli
and @ngtools/webpack
.
One should remove the old transformers from the jest config. - compiler:
jest-preset-angular
now switches to default to use its own transformer which wraps around ts-jest
to transform codes.
Users who are currently doing in jest config
// jest.config.js
module.exports = {
// [...]
transform: {
'^.+\\.(ts|js|html)$': 'ts-jest',
},
}
should change to
// jest.config.js
module.exports = {
// [...]
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
},
}
- serializers:: snapshot serializer paths have been changed:
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js'
is changed to 'jest-preset-angular/build/serializers/no-ng-attributes
.'jest-preset-angular/build/AngularSnapshotSerializer.js'
is changed to 'jest-preset-angular/build/serializers/ng-snapshot
.'jest-preset-angular/build/HTMLCommentSerializer.js'
is changed to 'jest-preset-angular/build/serializers/html-comment
.
- When generating a new project from Angular CLI, by default the
tsconfig.json
doesn't contain any path mappings
hence removing moduleNameMapper
from preset will make sure that the preset works in pair with tsconfig.json
.
Ones who are relying on the value of moduleNameMapper
from the preset should create their own moduleNameMapper
config manually or via ts-jest
util https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping.
If you wish to reuse the old configuration of moduleNameMapper
, you can put this into your Jest config
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
'^app/(.*)$': '<rootDir>/src/app/$1',
'^assets/(.*)$': '<rootDir>/src/assets/$1',
'^environments/(.*)$': '<rootDir>/src/environments/$1',
}
- By default, if
skipLibCheck
is not defined in tsconfig, jest-preset-angular
will set it to true
. If one wants to have it as false
, one can set explicitly in tsconfig. - compiler:
jest-preset-angular
now switches to default to use its own transformer which wraps around ts-jest
to transform codes.
Users who are currently doing in jest config
// jest.config.js
module.exports = {
// [...]
transform: {
'^.+\\.(ts|js|html)$': 'ts-jest',
},
}
should change to
// jest.config.js
module.exports = {
// [...]
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
},
}