Skip to content

Commit f7e199e

Browse files
authored
chore(release): 11.0.0 (#1186)
1 parent 40b769b commit f7e199e

21 files changed

+789
-5
lines changed

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
# [11.0.0](https://github.com/thymikee/jest-preset-angular/compare/v11.0.0-rc.4...v11.0.0) (2021-11-18)
2+
3+
4+
### Bug Fixes
5+
6+
* **presets:** use `ts-jest` types for preset types ([#1172](https://github.com/thymikee/jest-preset-angular/issues/1172)) ([81854f6](https://github.com/thymikee/jest-preset-angular/commit/81854f680088f23f4f2f5801ef660711812b90e3))
7+
* **presets:** add `mjs` to the list of `moduleFileExtensions` ([#1155](https://github.com/thymikee/jest-preset-angular/issues/1155)) ([3c992a1](https://github.com/thymikee/jest-preset-angular/commit/3c992a18b209f9d6500e34255b8f74c7fc5f3560)), closes [#1147](https://github.com/thymikee/jest-preset-angular/issues/1147)
8+
9+
10+
### Features
11+
12+
* support Angular 13 in CJS mode ([#1122](https://github.com/thymikee/jest-preset-angular/issues/1122)) ([12d3c6d](https://github.com/thymikee/jest-preset-angular/commit/12d3c6d27fadc3c423ab42d10615526e26826ed6))
13+
14+
15+
## Code Refactoring
16+
17+
* **presets**: improve `transformIgnorePatterns` value ([#1162](https://github.com/thymikee/jest-preset-angular/issues/1162)) ([99a4b1b1](https://github.com/thymikee/jest-preset-angular/commit/99a4b1b1549a13b27bd8d03df181ea6fa69aa073))
18+
19+
20+
### Performance Improvements
21+
22+
* process `js` files in `node_modules` with `esbuild` ([#1169](https://github.com/thymikee/jest-preset-angular/issues/1169)) ([1de3bf0](https://github.com/thymikee/jest-preset-angular/commit/1de3bf04886fbc91ab821e965ec94a2d8dc741be))
23+
* use `esbuild` to process `.mjs` files ([#1142](https://github.com/thymikee/jest-preset-angular/issues/1142)) ([5d3fe10](https://github.com/thymikee/jest-preset-angular/commit/5d3fe1010592b7a3973cdbc22c238e0e46d47b45)), closes [#1141](https://github.com/thymikee/jest-preset-angular/issues/1141)
24+
25+
26+
## BREAKING CHANGES
27+
28+
* `js` files from `node_modules` are now compiled with `esbuild` to improve performance.
29+
* **NodeJs** range version support now is `^12.20.0 || ^14.15.0 || >=16.10.0`
30+
* Due to the introduction of **ESM package format** for Angular packages, several things are added to the **default preset**
31+
to handle `.mjs` files from **Angular ESM packages**. Please check our migration documentation at https://thymikee.github.io/jest-preset-angular/docs/next/guides/angular-13+
32+
33+
34+
### Special Thanks
35+
36+
Alan Agius, Pete Bacon Darwin from Angular team
37+
38+
39+
140
# [11.0.0-rc.4](https://github.com/thymikee/jest-preset-angular/compare/v11.0.0-rc.3...v11.0.0-rc.4) (2021-11-15)
241

342

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-preset-angular",
3-
"version": "11.0.0-rc.4",
3+
"version": "11.0.0",
44
"description": "Jest preset configuration for Angular projects",
55
"license": "MIT",
66
"engines": {

website/docs/getting-started/options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Options
66
`jest-preset-angular` uses `ts-jest` options under the hood, which are located under the `globals` of Jest config object
77
in the `package.json` file of your project, or through a `jest.config.js`, or `jest.config.ts` file.
88

9-
More information about `ts-jest` options, see https://kulshekhar.github.io/ts-jest/docs/next/getting-started/options
9+
More information about `ts-jest` options, see https://kulshekhar.github.io/ts-jest/docs/getting-started/options
1010

1111
:::important
1212

website/docs/guides/angular-13+.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Angular >=13
44
---
55

66
**Angular 13** introduces ESM package format for Angular packages. `jest-preset-angular`
7-
currently supports testing with Jest in `CommonJS` mode with **Angular 13** using [default preset](https://thymikee.github.io/jest-preset-angular/docs/next/getting-started/presets).
7+
currently supports testing with Jest in `CommonJS` mode with **Angular 13** using [default preset](../getting-started/presets.md).
88
Jest ESM support with **Angular 13** is being worked on and will come in the future releases.
99

1010
Starting from **11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**:

website/docs/guides/esm-support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: ESM Support
55

66
To use `jest-preset-angular` with ESM support, you'll first need to check [ESM Jest documentation](https://jestjs.io/docs/en/ecmascript-modules).
77

8-
`jest-preset-angular` supports ESM via a `ts-jest` config option [useESM](https://kulshekhar.github.io/ts-jest/docs/next/getting-started/options/useESM) in combination with jest config option [extensionsToTreatAsEsm](https://jestjs.io/docs/en/next/configuration#extensionstotreatasesm-arraystring).
8+
`jest-preset-angular` supports ESM via a `ts-jest` config option [useESM](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/useESM) in combination with jest config option [extensionsToTreatAsEsm](https://jestjs.io/docs/en/configuration#extensionstotreatasesm-arraystring).
99

1010
There is also a [preset](../getting-started/presets.md) to work with ESM.
1111

website/src/pages/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function Home() {
7979
Angular Ivy? Check out our <Link to="/docs/guides/angular-ivy">Angular Ivy guidance</Link>.
8080
</div>
8181
<div className={styles.announcementInner}>
82-
Migrate to Angular >=13? Check out our <Link to="/docs/next/guides/angular-13+">Angular >=13 guidance</Link>
82+
Migrate to Angular >=13? Check out our <Link to="/docs/guides/angular-13+">Angular >=13 guidance</Link>
8383
.
8484
</div>
8585
</div>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
id: presets
3+
title: Presets
4+
---
5+
6+
### The presets
7+
8+
`jest-preset-angular` comes with 2 presets, covering most of the project's base configuration:
9+
10+
| Preset name | Description |
11+
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
12+
| `jest-preset-angular/presets/default`<br/>or `jest-preset-angular` | TypeScript, JavaScript and HTML files (`js`, `.ts`, `.html`) will be transformed by `jest-preset-angular` to **CommonJS** syntax. |
13+
| `jest-preset-angular/presets/defaults-esm`<br/> | TypeScript, JavaScript and HTML files (`js`, `.ts`, `.html`) will be transformed by `jest-preset-angular` to **ESM** syntax. |
14+
15+
### Basic usage
16+
17+
In most cases, simply setting the `preset` key to the desired preset name in your Jest config should be enough to start
18+
using TypeScript with Jest (assuming you added `jest-preset-angular` to your `devDependencies` of course):
19+
20+
```js
21+
// jest.config.js
22+
module.exports = {
23+
// [...]
24+
// Replace `jest-preset-angular` with the preset you want to use
25+
// from the above list
26+
preset: 'jest-preset-angular',
27+
};
28+
```
29+
30+
```json
31+
// OR package.json
32+
{
33+
// [...]
34+
"jest": {
35+
// Replace `jest-preset-angular` with the preset you want to use
36+
// from the above list
37+
"preset": "jest-preset-angular"
38+
}
39+
}
40+
```
41+
42+
### Advanced
43+
44+
All presets come with default `ts-jest` config options.
45+
If you want to override any of the options, you'll need to use the JavaScript version of Jest config,
46+
copy the original options and override the options you need:
47+
48+
```js
49+
// jest.config.js
50+
const { defaults: jestNgPreset } = require('jest-preset-angular/presets');
51+
// const { defaultsESM: jestNgPreset } = require('jest-preset-angular/presets')
52+
53+
module.exports = {
54+
// [...]
55+
globals: {
56+
'ts-jest': {
57+
...jestNgPreset.globals['ts-jest'],
58+
// [...your overriden options]
59+
},
60+
},
61+
};
62+
```
63+
64+
Or through TypeScript (if `ts-node` is installed):
65+
66+
```ts
67+
// jest.config.ts
68+
import type { InitialOptionsTsJest } from 'ts-jest/dist/types';
69+
import { defaults as jestNgPreset } from 'jest-preset-angular/presets';
70+
// import { defaultsESM as jestNgPreset } from 'jest-preset-angular/presets'
71+
72+
const config: InitialOptionsTsJest = {
73+
// [...]
74+
globals: {
75+
'ts-jest': {
76+
...jestNgPreset.globals['ts-jest'],
77+
// [...your overriden options]
78+
},
79+
},
80+
};
81+
82+
export default config;
83+
```
84+
85+
:::important
86+
87+
If you choose to override `globals` in order to point at a specific tsconfig, you will need to make sure that original `ts-jest`
88+
options provided through the default preset are defined to the `globals.ts-jest` section too, otherwise you will get
89+
errors.
90+
91+
:::
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
id: test-environment
3+
title: Test environment
4+
---
5+
6+
If you look at [`setup-jest.ts`](https://github.com/thymikee/jest-preset-angular/blob/main/src/config/setup-jest.ts),
7+
what we're doing here is we're adding globals required by Angular. With the included [jest-zone-patch](https://github.com/thymikee/jest-preset-angular/tree/main/src/zone-patch)
8+
we also make sure Jest test methods run in Zone context. Then we initialize the Angular testing environment like normal.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
id: absolute-imports
3+
title: Absolute Imports
4+
---
5+
6+
If you wish to use TypeScript path mappings which are defined in `paths` of your tsconfig, make sure that you create the
7+
similar mapping for `moduleNameMapper` in Jest config.
8+
9+
More information see `ts-jest` [paths mapping](https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping) configuration documentation

0 commit comments

Comments
 (0)