UNPKG

3.87 kBMarkdownView Raw
1<h1 align="center">Jest Preset Angular</h1>
2
3<p align="center">A preset of Jest configuration for Angular projects.</p>
4
5<p align="center">
6 <a href="https://actions-badge.atrox.dev/thymikee/jest-preset-angular/goto?ref=main"><img alt="Build Status" src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fthymikee%2Fjest-preset-angular%2Fbadge%3Fref%3Dmain&style=flat-square" /></a>
7 <a href="https://www.npmjs.com/package/jest-preset-angular"><img src="https://img.shields.io/npm/v/jest-preset-angular/latest.svg?style=flat-square" alt="NPM Version" /></a>
8 <a href="https://github.com/thymikee/jest-preset-angular/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/jest-preset-angular.svg?style=flat-square" alt="GitHub license" /></a>
9</p>
10
11> Our online documentation is available at https://thymikee.github.io/jest-preset-angular/
12
13> This is a part of the article: [Testing Angular faster with Jest](https://www.xfive.co/blog/testing-angular-faster-jest/).
14
15## Getting Started
16
17These instructions will get you setup to use `jest-preset-angular` in your project. For more detailed documentation,
18please check [online documentation](https://thymikee.github.io/jest-preset-angular).
19
20Install using [`yarn`](https://yarnpkg.com/en/package/jest-preset-angular):
21
22```bash
23yarn add -D jest jest-preset-angular @types/jest
24```
25
26Or [`npm`](https://www.npmjs.com/package/jest-preset-angular):
27
28```bash
29npm install -D jest jest-preset-angular @types/jest
30```
31
32## Configuration
33
34In your project root, create `setup-jest.ts` file with following contents:
35
36```ts
37import 'jest-preset-angular/setup-jest';
38```
39
40Add the following section:
41
42- to your root `jest.config.js`
43
44```js
45// jest.config.js
46module.exports = {
47 preset: 'jest-preset-angular',
48 setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
49 globalSetup: 'jest-preset-angular/global-setup',
50};
51```
52
53- or to your root `package.json`
54
55```json
56{
57 "jest": {
58 "preset": "jest-preset-angular",
59 "setupFilesAfterEnv": ["<rootDir>/setup-jest.ts"],
60 "globalSetup": "jest-preset-angular/global-setup"
61 }
62}
63```
64
65Adjust your `tsconfig.spec.json` to be:
66
67```json
68{
69 "extends": "./tsconfig.json",
70 "compilerOptions": {
71 "outDir": "./out-tsc/spec",
72 "module": "CommonJs",
73 "types": ["jest"]
74 },
75 "include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
76}
77```
78
79**IMPORTANT**
80
81Angular doesn't support native `async/await` in testing with `target` higher than `ES2016`, see https://github.com/angular/components/issues/21632#issuecomment-764975917
82
83## Migration from Angular < 13
84
85Check out our [Migration from Angular < 13 guidance](https://thymikee.github.io/jest-preset-angular/docs/guides/angular-13+)
86
87## Angular Ivy
88
89Check out our [Angular Ivy guidance](https://thymikee.github.io/jest-preset-angular/docs/guides/angular-ivy)
90
91## Example projects with base Jest configuration
92
93We have [example apps](https://github.com/thymikee/jest-preset-angular/tree/main/examples) to provide a basic setup to use Jest in an Angular project.
94The `examples` folder consist of several example Angular applications from **v13** onwards as well as example projects
95with `yarn workspace` or monorepo structure.
96
97## Built With
98
99- [TypeScript](https://www.typescriptlang.org/) - JavaScript that scales
100- [Angular](https://angular.io/) - The modern web developer's platform
101- [`ts-jest`](https://kulshekhar.github.io/ts-jest) - Jest transformer for TypeScript
102
103## Authors/maintainers
104
105- **Michał Pierzchała** - [thymikee](https://github.com/thymikee)
106- **Ahn** - [ahnpnl](https://github.com/ahnpnl)
107- **Thomas** - [wtho](https://github.com/wtho)
108
109See also the list of [contributors](https://github.com/thymikee/jest-preset-angular/contributors) who participated in this project.
110
111## License
112
113This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details