Installation
Dependencies
You can install jest-preset-angular and dependencies all at once with one of the following commands.
- npm
- Yarn
- pnpm
- Bun
npm install -D jest jest-preset-angular @types/jest jest-environment-jsdom jsdom
yarn add --dev jest jest-preset-angular @types/jest jest-environment-jsdom jsdom
pnpm add -D jest jest-preset-angular @types/jest jest-environment-jsdom jsdom
bun add --dev jest jest-preset-angular @types/jest jest-environment-jsdom jsdom
Configuration
Angular doesn't support native async/await in testing with target higher than ES2016, see https://github.com/angular/components/issues/21632#issuecomment-764975917
For ESM configuration, please see more in details with ESM guide.
In your project root, create a setup file with following contents:
- Setup file CJS
- Setup file ESM
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv();
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs';
setupZoneTestEnv();
Update setupFilesAfterEnv in your Jest config as following:
import type { Config } from 'jest';
import { createCjsPreset } from 'jest-preset-angular/presets';
export default {
...createCjsPreset(),
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
} satisfies Config;
Adjust your tsconfig.spec.json to be:
- Tsconfig CJS
- Tsconfig ESM
{
//...
"extends": "./tsconfig.json",
"compilerOptions": {
//...
"module": "CommonJS",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
//...
}
{
//...
"extends": "./tsconfig.json",
"compilerOptions": {
//...
"module": "ES2022",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
//...
}
Adjust scripts part your package.json to use jest instead of ng, e.g.
- package.json for CJS
- package.json for ESM
{
//...
"scripts": {
"test": "jest"
}
//...
}
{
//...
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}
//...
}
Customizing
Global mocks
jest-preset-angular uses JSDOM which is different from normal browsers. You might need some global browser mocks to
simulate the behaviors of real browsers in JSDOM. To add global mocks, you can do the following:
- Create a file
jest-global-mocks.tsto your root project. - Import it in your global setup file:
- Setup file CJS
- Setup file ESM
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
import './jest-global-mocks';
setupZoneTestEnv();
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs';
import './jest-global-mocks';
setupZoneTestEnv();
An example for jest-global-mocks.ts
Object.defineProperty(document, 'doctype', {
value: '<!DOCTYPE html>',
});
Object.defineProperty(window, 'getComputedStyle', {
value: () => {
return {
display: 'none',
appearance: ['-webkit-appearance'],
};
},
});
/**
* ISSUE: https://github.com/angular/material2/issues/7101
* Workaround for JSDOM missing transform property
*/
Object.defineProperty(document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true,
};
},
});
Avoid karma conflicts
By Angular CLI defaults you'll have a src/test.ts file which will be picked up by jest. To circumvent this you can either rename it to src/karmaTest.ts or hide it from jest by adding <rootDir>/src/test.ts to jest testPathIgnorePatterns option.