You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: website/docs/getting-started/test-environment.md
+113-25
Original file line number
Diff line number
Diff line change
@@ -3,32 +3,120 @@ id: test-environment
3
3
title: Test environment
4
4
---
5
5
6
-
If you look at [`setup-jest.js`](https://github.com/thymikee/jest-preset-angular/blob/main/setup-jest.js),
7
-
what we're doing here is we're adding globals required by Angular. With the included [Angular zone patch](https://github.com/angular/angular/tree/main/packages/zone.js)
8
-
we also make sure Jest test methods run in Zone context. Then we initialize the Angular testing environment like normal.
9
-
10
-
While `setup-jest.js` above is for running Jest with **CommonJS** mode, we also provide [`setup-jest.mjs`](https://github.com/thymikee/jest-preset-angular/blob/main/setup-jest.mjs)
11
-
to run with **ESM** mode.
12
-
13
-
### Configure test environment
14
-
15
-
When creating Angular test environment with `TestBed`, it is possible to specify the `testEnvironmentOptions` via `globalThis` in the Jest setup file.
16
-
For example:
17
-
18
-
```ts
19
-
// setup-test.ts
20
-
globalThis.ngJest= {
21
-
testEnvironmentOptions: {
22
-
teardown: {
23
-
destroyAfterEach: false,
24
-
rethrowErrors: true,
25
-
},
26
-
errorOnUnknownElements: true,
27
-
errorOnUnknownProperties: true,
28
-
},
6
+
In Jest, a test environment defines the sandbox context in which your tests run.
7
+
For Angular projects, setting up the correct test environment is essential to ensure compatibility with the
8
+
framework-specific features, such as dependency injection and change detection.
9
+
10
+
`jest-preset-angular` provides utility functions to simplify setting up a Jest test environment tailored for Angular projects.
11
+
These functions support both **zone-based** and **zoneless** environments, catering to different testing needs.
12
+
13
+
## Methods
14
+
15
+
import TOCInline from '@theme/TOCInline';
16
+
17
+
<TOCInlinetoc={toc.slice(1)} />
18
+
19
+
---
20
+
21
+
### `setupZoneTestEnv(options)`
22
+
23
+
Configures a test environment that uses `zone.js`, which is the mechanism for tracking asynchronous operations.
24
+
It is suitable for most Angular applications that rely on `zone.js` for change detection and other framework features.
25
+
26
+
You can customize the environment by providing options as function arguments.
27
+
28
+
#### Parameters
29
+
30
+
-`options`**(optional)**: An object follows [TestEnvironmentOptions interface](https://github.com/angular/angular/blob/a55341b1ab8d2bc4285a4cce59df7fc0b23c0125/packages/core/testing/src/test_bed_common.ts#L95), which allows fine-tuning the environment.
`jest-preset-angular` will look at `globalThis.ngJest` and pass the correct [`TestEnvironmentOptions`](https://angular.io/api/core/testing/TestEnvironmentOptions) object to `TestBed`.
68
+
```ts tab={"label": "TypeScript ESM"}
69
+
// jest.config.mts
70
+
importtype { Config } from'jest';
71
+
72
+
const jestConfig:Config= {
73
+
preset: 'jest-preset-angular',
74
+
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
75
+
};
76
+
77
+
exportdefaultjestConfig;
78
+
```
79
+
80
+
### `setupZonelessTestEnv(options)`
81
+
82
+
Configures a test environment that **DOESN'T** use `zone.js`, as described in [Angular experimental zoneless guide](https://angular.dev/guide/experimental/zoneless).
83
+
It is designed for projects that have disabled `zone.js`, which can lead to improved performance and simplified testing.
84
+
85
+
:::important
86
+
87
+
This function is only supported in Jest `ESM` mode in [Jest 29](https://github.com/jestjs/jest/issues/10962). Jest 30+ will support to use for `CommonJS` mode.
88
+
89
+
:::
90
+
91
+
You can customize the environment by providing options as function arguments.
92
+
93
+
#### Parameters
94
+
95
+
-`options`**(optional)**: An object follows [TestEnvironmentOptions interface](https://github.com/angular/angular/blob/a55341b1ab8d2bc4285a4cce59df7fc0b23c0125/packages/core/testing/src/test_bed_common.ts#L95), which allows fine-tuning the environment.
Copy file name to clipboardexpand all lines: website/docs/guides/troubleshooting.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ With Angular 8 and higher, a [change to the way the Angular CLI works](https://g
18
18
"emitDecoratorMetadata": true
19
19
```
20
20
21
-
In general, this is related to Angular's reflection and also depends on a reflection library, as e. g. included in `core-js`. We use our own minimal reflection that satisfy Angular's current requirements, but in case these change, you can install `core-js` and import the reflection library in your `setup-jest.ts`:
21
+
In general, this is related to `Angular`'s reflection and also depends on a reflection library, as e. g. included in `core-js`. We use our own minimal reflection that satisfy `Angular`'s current requirements, but in case these change, you can install `core-js` and import the reflection library in your `setup-jest.ts`:
0 commit comments