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.
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
In your project root, create setup-jest.ts
file with following contents:
// setup-jest.ts
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv();
Add the following section:
- to your root
jest.config.ts
// jest.config.ts
import type { Config } from 'jest';
const jestConfig: Config = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
};
export default jestConfig;
- 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": {
"module": "CommonJS",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
IMPORTANT
Angular doesn't support native async/await
in testing with target
higher than ES2016
, see angular/components#21632 (comment)
Check out our Migration from Angular < 13 guidance
Check out our Angular Ivy guidance
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.
- TypeScript - JavaScript that scales
- Angular - The modern web developer's platform
ts-jest
- Jest transformer for TypeScript
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details