|
| 1 | +--- |
| 2 | +id: options |
| 3 | +title: Options |
| 4 | +--- |
| 5 | + |
| 6 | +`jest-preset-angular` uses `ts-jest` options under the hood, which are located under the `globals` of Jest config object |
| 7 | +in the `package.json` file of your project, or through a `jest.config.js`, or `jest.config.ts` file. |
| 8 | + |
| 9 | +More information about `ts-jest` options, see https://kulshekhar.github.io/ts-jest/docs/getting-started/options |
| 10 | + |
| 11 | +:::important |
| 12 | + |
| 13 | +Since **9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse |
| 14 | +the old `moduleNameMapper` configuration, you can put this into your Jest config |
| 15 | + |
| 16 | +``` |
| 17 | +moduleNameMapper: { |
| 18 | + '^src/(.*)$': '<rootDir>/src/$1', |
| 19 | + '^app/(.*)$': '<rootDir>/src/app/$1', |
| 20 | + '^assets/(.*)$': '<rootDir>/src/assets/$1', |
| 21 | + '^environments/(.*)$': '<rootDir>/src/environments/$1', |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +::: |
| 26 | + |
| 27 | +### Exposed configuration |
| 28 | + |
| 29 | +```js |
| 30 | +const snapshotSerializers = require('../build/serializers'); |
| 31 | + |
| 32 | +module.exports = { |
| 33 | + globals: { |
| 34 | + 'ts-jest': { |
| 35 | + tsconfig: '<rootDir>/tsconfig.spec.json', |
| 36 | + stringifyContentPathRegex: '\\.(html|svg)$', |
| 37 | + }, |
| 38 | + }, |
| 39 | + moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'], |
| 40 | + resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js', |
| 41 | + snapshotSerializers, |
| 42 | + testEnvironment: 'jsdom', |
| 43 | + transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], |
| 44 | + transform: { |
| 45 | + '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular', |
| 46 | + }, |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | +:::important |
| 51 | + |
| 52 | +Jest runs with `jest-preset-angular` neither in browser nor through dev server. It uses `JSDOM` to abstract browser environment hence we depend on |
| 53 | +`JSDOM` implementation for real browser features. |
| 54 | + |
| 55 | +::: |
| 56 | + |
| 57 | +### Brief explanation of config |
| 58 | + |
| 59 | +- we're using some `"globals"` to pass information about where our tsconfig.json file is that we'd like to be able to transform HTML files through `ts-jest`. |
| 60 | +- `"moduleFileExtensions"` – our modules are TypeScript (`ts`), HTML (`html`), JavaScript (`js`), JSON (`json`) and ESM JavaScript (`mjs`) files. |
| 61 | +- `"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses `RegExp`. |
| 62 | +- `"resolver"` - instruct Jest how to resolve entry file based on `package.json` definitions. |
| 63 | +- `"snapshotSerializers"` - array of serializers which will be applied to snapshot the code. Note: by default angular adds |
| 64 | + some angular-specific attributes to the code (like `ng-reflect-*`, `ng-version="*"`, `_ngcontent-c*` etc). |
| 65 | + This package provides serializer to remove such attributes. This makes snapshots cleaner and more human-readable. |
| 66 | + To remove such specific attributes use `no-ng-attributes` serializer. You need to add `no-ng-attributes` serializer manually. |
| 67 | +- `"testEnvironment"` – the test environment to run on. |
| 68 | +- `"transformIgnorePatterns"`: instruct Jest to transform any `.mjs` files which come from `node_modules`. |
| 69 | +- `"transform"` – run every `TS`, `JS`, `MJS`, or `HTML` file through so called _Jest transformer_; this lets Jest understand non-JS syntax. |
0 commit comments