|
| 1 | +--- |
| 2 | +id: installation |
| 3 | +title: Installation |
| 4 | +--- |
| 5 | + |
| 6 | +### Dependencies |
| 7 | + |
| 8 | +You can install `jest-preset-angular` and dependencies all at once with one of the following commands. |
| 9 | + |
| 10 | +#### NPM |
| 11 | + |
| 12 | +```sh |
| 13 | +npm install -D jest jest-preset-angular @types/jest |
| 14 | +``` |
| 15 | + |
| 16 | +#### Yarn |
| 17 | + |
| 18 | +```sh |
| 19 | +yarn add -D jest jest-preset-angular @types/jest |
| 20 | +``` |
| 21 | + |
| 22 | +### Configuration |
| 23 | + |
| 24 | +In your project root, create `setup-jest.ts` file with following contents: |
| 25 | + |
| 26 | +```ts |
| 27 | +import 'jest-preset-angular/setup-jest'; |
| 28 | +``` |
| 29 | + |
| 30 | +Add the following section: |
| 31 | + |
| 32 | +- to your root `jest.config.js` |
| 33 | + |
| 34 | +```js |
| 35 | +// jest.config.js |
| 36 | +module.exports = { |
| 37 | + preset: 'jest-preset-angular', |
| 38 | + setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +- or to your root `package.json` |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "jest": { |
| 47 | + "preset": "jest-preset-angular", |
| 48 | + "setupFilesAfterEnv": ["<rootDir>/setup-jest.ts"] |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Adjust your `tsconfig.spec.json` to be: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "extends": "./tsconfig.json", |
| 58 | + "compilerOptions": { |
| 59 | + "outDir": "./out-tsc/spec", |
| 60 | + "types": ["jest"] |
| 61 | + }, |
| 62 | + "include": ["src/**/*.spec.ts", "src/**/*.d.ts"] |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Customizing |
| 67 | + |
| 68 | +#### Global mocks |
| 69 | + |
| 70 | +`jest-preset-angular` uses `JSDOM` which is different from normal browsers. You might need some global browser mocks to |
| 71 | +stimulate the behaviors of real browsers in `JSDOM`. To add global mocks, you can do the following: |
| 72 | + |
| 73 | +- Create a file `jest-global-mocks.ts` to your root project. |
| 74 | +- Import it in your global setup file: |
| 75 | + |
| 76 | +```ts |
| 77 | +// Assuming that your global setup file is setup-jest.ts |
| 78 | +import 'jest-preset-angular/setup-jest'; |
| 79 | +import './jest-global-mocks'; |
| 80 | +``` |
| 81 | + |
| 82 | +:::tip |
| 83 | + |
| 84 | +An example for `jest-global-mocks.ts` |
| 85 | + |
| 86 | +``` |
| 87 | +Object.defineProperty(window, 'CSS', { value: null }); |
| 88 | +Object.defineProperty(document, 'doctype', { |
| 89 | + value: '<!DOCTYPE html>', |
| 90 | +}); |
| 91 | +Object.defineProperty(window, 'getComputedStyle', { |
| 92 | + value: () => { |
| 93 | + return { |
| 94 | + display: 'none', |
| 95 | + appearance: ['-webkit-appearance'], |
| 96 | + }; |
| 97 | + }, |
| 98 | +}); |
| 99 | +/** |
| 100 | + * ISSUE: https://github.com/angular/material2/issues/7101 |
| 101 | + * Workaround for JSDOM missing transform property |
| 102 | + */ |
| 103 | +Object.defineProperty(document.body.style, 'transform', { |
| 104 | + value: () => { |
| 105 | + return { |
| 106 | + enumerable: true, |
| 107 | + configurable: true, |
| 108 | + }; |
| 109 | + }, |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +::: |
| 114 | + |
| 115 | +#### Avoid karma conflicts |
| 116 | + |
| 117 | +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. |
0 commit comments