Skip to content

Commit 1a56cff

Browse files
authored
docs(devs-infra): documentation for v9.0.0 (#931)
1 parent 98f3058 commit 1a56cff

File tree

20 files changed

+10582
-15332
lines changed

20 files changed

+10582
-15332
lines changed

website/docs/getting-started/presets.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ copy the original options and override the options you need:
4747

4848
```js
4949
// jest.config.js
50-
const { defaults: tsjPreset } = require('jest-preset-angular/presets');
51-
// const { defaultsESM: tsjPreset } = require('jest-preset-angular/presets')
50+
const { defaults: jestNgPreset } = require('jest-preset-angular/presets');
51+
// const { defaultsESM: jestNgPreset } = require('jest-preset-angular/presets')
5252

5353
module.exports = {
5454
// [...]
5555
globals: {
5656
'ts-jest': {
57-
...tsjPreset.globals['ts-jest'],
57+
...jestNgPreset.globals['ts-jest'],
5858
// [...your overriden options]
5959
},
6060
},
@@ -66,14 +66,14 @@ Or through TypeScript (if `ts-node` is installed):
6666
```ts
6767
// jest.config.ts
6868
import type { InitialOptionsTsJest } from 'ts-jest/dist/types';
69-
import { defaults as tsjPreset } from 'jest-preset-angular/presets';
70-
// import { defaultsESM as tsjPreset } from 'jest-preset-angular/presets'
69+
import { defaults as jestNgPreset } from 'jest-preset-angular/presets';
70+
// import { defaultsESM as jestNgPreset } from 'jest-preset-angular/presets'
7171

7272
const config: InitialOptionsTsJest = {
7373
// [...]
7474
globals: {
7575
'ts-jest': {
76-
...tsjPreset.globals['ts-jest'],
76+
...jestNgPreset.globals['ts-jest'],
7777
// [...your overriden options]
7878
},
7979
},

website/package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
"clear": "docusaurus clear"
1313
},
1414
"dependencies": {
15-
"@docusaurus/core": "2.0.0-alpha.70",
16-
"@docusaurus/plugin-ideal-image": "2.0.0-alpha.70",
17-
"@docusaurus/plugin-pwa": "2.0.0-alpha.70",
18-
"@docusaurus/preset-classic": "2.0.0-alpha.70",
15+
"@docusaurus/core": "^2.0.0-beta.0",
16+
"@docusaurus/plugin-ideal-image": "^2.0.0-beta.0",
17+
"@docusaurus/plugin-pwa": "^2.0.0-beta.0",
18+
"@docusaurus/preset-classic": "^2.0.0-beta.0",
1919
"@mdx-js/react": "^1.6.21",
2020
"clsx": "^1.1.1",
2121
"latest-version": "^5.1.0",
22-
"react": "^16.8.4",
23-
"react-dom": "^16.8.4"
22+
"react": "^16.14.0",
23+
"react-dom": "^16.14.0"
24+
},
25+
"devDependencies": {
26+
"@babel/core": "^7.0.0",
27+
"@types/react": "^17.0.3"
2428
},
2529
"browserslist": {
2630
"production": [

website/static/manifest.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@
88
"start_url": "./index.html",
99
"icons": [
1010
{
11-
"src": "img/icons/icon-72x72.png",
11+
"src": "img/logo.png",
1212
"sizes": "72x72",
1313
"type": "image/png"
1414
},
1515
{
16-
"src": "img/icons/icon-96x96.png",
16+
"src": "img/logo.png",
1717
"sizes": "96x96",
1818
"type": "image/png"
1919
},
2020
{
21-
"src": "img/icons/icon-128x128.png",
21+
"src": "img/logo.png",
2222
"sizes": "128x128",
2323
"type": "image/png"
2424
},
2525
{
26-
"src": "img/icons/icon-144x144.png",
26+
"src": "img/logo.png",
2727
"sizes": "144x144",
2828
"type": "image/png"
2929
},
3030
{
31-
"src": "img/icons/icon-152x152.png",
31+
"src": "img/logo.png",
3232
"sizes": "152x152",
3333
"type": "image/png"
3434
},
3535
{
36-
"src": "img/icons/icon-192x192.png",
36+
"src": "img/logo.png",
3737
"sizes": "192x192",
3838
"type": "image/png"
3939
},
4040
{
41-
"src": "img/icons/icon-384x384.png",
41+
"src": "img/logo.png",
4242
"sizes": "384x384",
4343
"type": "image/png"
4444
},
4545
{
46-
"src": "img/icons/icon-512x512.png",
46+
"src": "img/logo.png",
4747
"sizes": "512x512",
4848
"type": "image/png"
4949
}

website/versioned_docs/version-8.x/getting-started/presets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ copy the original options and override the options you need:
4242

4343
```js
4444
// jest.config.js
45-
const { defaults: tsjPreset } = require('jest-preset-angular');
45+
const { defaults: jestNgPreset } = require('jest-preset-angular');
4646

4747
module.exports = {
4848
// [...]
4949
globals: {
5050
'ts-jest': {
51-
...tsjPreset.globals['ts-jest'],
51+
...jestNgPreset.globals['ts-jest'],
5252
// [...your overriden options]
5353
},
5454
},
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
Feel free to copy the [`jest-global-mocks.ts`](https://github.com/thymikee/jest-preset-angular/blob/master/e2e/test-app-v9/jest-global-mocks.ts) file from the test app directory and save it next to the `setup-jest.ts` file.
85+
86+
:::
87+
88+
#### Avoid karma conflicts
89+
90+
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
### Exposed configuration
12+
13+
```js
14+
const snapshotSerializers = require('../build/serializers');
15+
16+
module.exports = {
17+
globals: {
18+
'ts-jest': {
19+
tsconfig: '<rootDir>/tsconfig.spec.json',
20+
stringifyContentPathRegex: '\\.html$',
21+
},
22+
},
23+
testEnvironment: 'jsdom',
24+
transform: {
25+
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
26+
},
27+
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
28+
snapshotSerializers,
29+
};
30+
```
31+
32+
:::important
33+
34+
Jest runs with `jest-preset-angular` neither in browser nor through dev server. It uses `JSDOM` to abstract browser environment hence we depend on
35+
`JSDOM` implementation for real browser features.
36+
37+
:::
38+
39+
### Brief explanation of config
40+
41+
- 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`.
42+
- `"transform"` – run every TS, JS, or HTML file through so called _Jest transformer_; this lets Jest understand non-JS syntax.
43+
- `"testEnvironment"` – the test environment to run on.
44+
- `"moduleFileExtensions"` – our modules are TypeScript and JavaScript files.
45+
- `"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex.
46+
- `"snapshotSerializers"` - array of serializers which will be applied to snapshot the code. Note: by default angular adds
47+
some angular-specific attributes to the code (like `ng-reflect-*`, `ng-version="*"`, `_ngcontent-c*` etc).
48+
This package provides serializer to remove such attributes. This makes snapshots cleaner and more human-readable.
49+
To remove such specific attributes use `no-ng-attributes` serializer. You need to add `no-ng-attributes` serializer manually.
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/default-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/master/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/master/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)