Skip to content

Commit 0ff70c6

Browse files
authored
chore(release): v11.0.0-rc.0 (#1131)
1 parent 12d3c6d commit 0ff70c6

File tree

20 files changed

+784
-2
lines changed

20 files changed

+784
-2
lines changed

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
# [11.0.0-rc.0](https://github.com/thymikee/jest-preset-angular/compare/v10.0.1...v11.0.0-rc.0) (2021-11-04)
2+
3+
4+
### Features
5+
6+
* 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))
7+
8+
9+
## BREAKING CHANGES
10+
11+
* **NodeJs** range version support now is `^12.20.0 || ^14.15.0 || >=16.10.0`
12+
* Due to the introduction of **ESM package format** for Angular packages, several things are added to the **default preset** to handle `.mjs` files from **Angular ESM packages**:
13+
+ `ng-jest-resolver` is introduced as a custom Jest resolver to resolve `.mjs` files.
14+
+ `transformIgnorePatterns` is added to inform Jest to transform `.mjs` files.
15+
+ `transform` is updated to include `.mjs` extension to transform to `CommonJS` codes.
16+
17+
If one has custom Jest config, please make sure to adjust Jest config for CJS mode as following:
18+
```
19+
// jest.config.js
20+
module.exports = {
21+
// other config
22+
resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
23+
transformIgnorePatterns: ['node_modules/(?!@angular)'],
24+
transform: {
25+
'^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
26+
},
27+
}
28+
```
29+
30+
31+
### Special Thanks
32+
33+
Alan Agius, Pete Bacon Darwin from Angular team
34+
35+
36+
137
## [10.1.0](https://github.com/thymikee/jest-preset-angular/compare/v10.0.1...v10.1.0) (2021-10-26)
238

339

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ Adjust your `tsconfig.spec.json` to be:
7171
}
7272
```
7373

74+
## Notes on usage with Angular >=13
75+
76+
Check out our [Angular >=13 usage notes](https://thymikee.github.io/jest-preset-angular/docs/guides/angular-13)
77+
7478
## Angular Ivy
7579

7680
Check out our [Angular Ivy guidance](https://thymikee.github.io/jest-preset-angular/docs/guides/angular-ivy)
7781

78-
## Example apps
82+
## Example projects with base Jest configuration
7983

8084
We 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.
85+
The `examples` folder consist of several example Angular applications from **v10** to **13**, as well as example projects
86+
with `yarn workspace` or monorepo structure.
8187

8288
## Built With
8389

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": "10.1.0",
3+
"version": "11.0.0-rc.0",
44
"description": "Jest preset configuration for Angular projects",
55
"license": "MIT",
66
"engines": {

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: angular-13+
3+
title: Angular >=13
4+
---
5+
6+
**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).
8+
Jest ESM support with **Angular 13** is being worked on and will come in the future releases.
9+
10+
Starting from **11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**:
11+
12+
- `ng-jest-resolver` is introduced as a custom Jest resolver to resolve `.mjs` files.
13+
14+
- `transformIgnorePatterns` is added to inform Jest to transform `.mjs` files.
15+
16+
- `transform` is updated to include `.mjs` extension to transform to `CommonJS` codes.
17+
18+
## Migration steps from Angular < 13
19+
20+
- Upgrade the project to **Angular 13** following https://update.angular.io/
21+
22+
- If one is using the default preset as following:
23+
24+
```js
25+
// jest.config.js
26+
module.exports = {
27+
preset: 'jest-preset-angular',
28+
};
29+
```
30+
31+
there are no migration steps required
32+
33+
- If one is using custom Jest config options for `transformIgnorePatterns`, `transform` and not using `preset: 'jest-preset-angular'`,
34+
there are a few changes needed to be added to the Jest config:
35+
36+
```js
37+
// jest.config.js
38+
module.exports = {
39+
resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
40+
transformIgnorePatterns: ['node_modules/(?!@angular)'],
41+
transform: {
42+
'^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
43+
},
44+
};
45+
```

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
collapsed: false,
2323
items: [
2424
'guides/angular-ivy',
25+
'guides/angular-13+',
2526
'guides/esm-support',
2627
'guides/jsdom-version',
2728
'guides/using-with-babel',

website/src/pages/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ function Home() {
7878
<div className={styles.announcementInner}>
7979
Angular Ivy? Check out our <Link to="/docs/guides/angular-ivy">Angular Ivy guidance</Link>.
8080
</div>
81+
<div className={styles.announcementInner}>
82+
Migrate to Angular >=13? Check out our <Link to="/docs/next/guides/angular-13+">Angular >=13 guidance</Link>
83+
.
84+
</div>
8185
</div>
8286
</header>
8387
<main>
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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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/next/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+
testEnvironment: 'jsdom',
40+
transform: {
41+
'^.+\\.(ts|js|html|svg)$': 'jest-preset-angular',
42+
},
43+
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
44+
snapshotSerializers,
45+
};
46+
```
47+
48+
:::important
49+
50+
Jest runs with `jest-preset-angular` neither in browser nor through dev server. It uses `JSDOM` to abstract browser environment hence we depend on
51+
`JSDOM` implementation for real browser features.
52+
53+
:::
54+
55+
### Brief explanation of config
56+
57+
- 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`.
58+
- `"transform"` – run every TS, JS, or HTML file through so called _Jest transformer_; this lets Jest understand non-JS syntax.
59+
- `"testEnvironment"` – the test environment to run on.
60+
- `"moduleFileExtensions"` – our modules are TypeScript and JavaScript files.
61+
- `"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex.
62+
- `"snapshotSerializers"` - array of serializers which will be applied to snapshot the code. Note: by default angular adds
63+
some angular-specific attributes to the code (like `ng-reflect-*`, `ng-version="*"`, `_ngcontent-c*` etc).
64+
This package provides serializer to remove such attributes. This makes snapshots cleaner and more human-readable.
65+
To remove such specific attributes use `no-ng-attributes` serializer. You need to add `no-ng-attributes` serializer manually.

0 commit comments

Comments
 (0)