-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: refactor test environment related pages
- `test-environment.md` -> `testbed-environment.md` - `jsdom-version.md` -> `jsdom-environment` - Deprecate old doc pages
- Loading branch information
Showing
12 changed files
with
342 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
--- | ||
id: testbed-environment | ||
title: TestBed environment | ||
--- | ||
|
||
Angular provides a powerful testing utility called [TestBed](https://angular.dev/api/core/testing/TestBed), | ||
which allows to configure and initialize an environment for unit testing and provides methods for creating components and services in unit tests. | ||
|
||
`jest-preset-angular` provides utility functions to simplify setting up `TestBed` environment. These below utility functions | ||
support both **zone-based** and **zoneless** environments, catering to different testing needs. | ||
|
||
## Functions | ||
|
||
import TOCInline from '@theme/TOCInline'; | ||
|
||
<TOCInline toc={toc.slice(1)} /> | ||
|
||
--- | ||
|
||
### `setupZoneTestEnv(options)` | ||
|
||
Configures an environment that uses `zone.js`, which is the mechanism for tracking asynchronous operations. | ||
It is suitable for most Angular applications that rely on `zone.js` for change detection and other framework features. | ||
|
||
You can customize the environment by providing options as function arguments. | ||
|
||
#### Parameters | ||
|
||
- `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. | ||
|
||
#### Example: | ||
|
||
- Create a Jest setup file: | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript CJS"} | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
|
||
setupZoneTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript ESM"} | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
|
||
setupZoneTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
- Update your Jest configuration: | ||
|
||
```ts title="jest.config.ts" tab={"label": "TypeScript CJS"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createCjsPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` | ||
|
||
```ts title="jest.config.mts" tab={"label": "TypeScript ESM"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createEsmPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` | ||
|
||
### `setupZonelessTestEnv(options)` | ||
|
||
Configures an environment that **DOESN'T** use `zone.js`, as described in [Angular experimental zoneless guide](https://angular.dev/guide/experimental/zoneless). | ||
It is designed for projects that have disabled `zone.js`, which can lead to improved performance and simplified testing. | ||
|
||
You can customize the environment by providing options as function arguments. | ||
|
||
#### Parameters | ||
|
||
- `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. | ||
|
||
#### Example: | ||
|
||
- Create a Jest setup file: | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript CJS"} | ||
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless'; | ||
|
||
setupZonelessTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript ESM"} | ||
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless/index.mjs'; | ||
|
||
setupZonelessTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
- Update your Jest configuration: | ||
|
||
```ts title="jest.config.ts" tab={"label": "TypeScript CJS"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createCjsPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` | ||
|
||
```ts title="jest.config.mts" tab={"label": "TypeScript ESM"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createEsmPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
id: jsdom-environment | ||
title: JSDOM environment | ||
--- | ||
|
||
`jest-preset-angular` provides a way to configure a different version of [JSDOM](https://github.com/jsdom/jsdom) than the one ships with `Jest` | ||
via a custom `JSDOM` environment. One can follow the below steps to configure a different `JSDOM` version: | ||
|
||
- Install the desired JSDOM version | ||
|
||
```bash npm2yarn | ||
npm install -D jsdom@<desired-version> | ||
``` | ||
|
||
- In Jest config, set the `testEnvironment` like following | ||
|
||
```ts title="jest.config.ts" tab={"label": "TypeScript CJS"} | ||
import type { Config } from 'jest'; | ||
|
||
export default { | ||
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env', | ||
} satisfies Config; | ||
``` | ||
|
||
```ts title="jest.config.mts" tab={"label": "TypeScript ESM"} | ||
import type { Config } from 'jest'; | ||
|
||
export default { | ||
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env', | ||
} satisfies Config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
website/versioned_docs/version-14.5/getting-started/testbed-environment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
--- | ||
id: testbed-environment | ||
title: TestBed environment | ||
--- | ||
|
||
Angular provides a powerful testing utility called [TestBed](https://angular.dev/api/core/testing/TestBed), | ||
which allows to configure and initialize an environment for unit testing and provides methods for creating components and services in unit tests. | ||
|
||
`jest-preset-angular` provides utility functions to simplify setting up `TestBed` environment. These below utility functions | ||
support both **zone-based** and **zoneless** environments, catering to different testing needs. | ||
|
||
## Functions | ||
|
||
import TOCInline from '@theme/TOCInline'; | ||
|
||
<TOCInline toc={toc.slice(1)} /> | ||
|
||
--- | ||
|
||
### `setupZoneTestEnv(options)` | ||
|
||
Configures an environment that uses `zone.js`, which is the mechanism for tracking asynchronous operations. | ||
It is suitable for most Angular applications that rely on `zone.js` for change detection and other framework features. | ||
|
||
You can customize the environment by providing options as function arguments. | ||
|
||
#### Parameters | ||
|
||
- `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. | ||
|
||
#### Example: | ||
|
||
- Create a Jest setup file: | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript CJS"} | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
|
||
setupZoneTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript ESM"} | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
|
||
setupZoneTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
- Update your Jest configuration: | ||
|
||
```ts title="jest.config.ts" tab={"label": "TypeScript CJS"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createCjsPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` | ||
|
||
```ts title="jest.config.mts" tab={"label": "TypeScript ESM"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createEsmPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` | ||
|
||
### `setupZonelessTestEnv(options)` | ||
|
||
Configures an environment that **DOESN'T** use `zone.js`, as described in [Angular experimental zoneless guide](https://angular.dev/guide/experimental/zoneless). | ||
It is designed for projects that have disabled `zone.js`, which can lead to improved performance and simplified testing. | ||
|
||
You can customize the environment by providing options as function arguments. | ||
|
||
#### Parameters | ||
|
||
- `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. | ||
|
||
#### Example: | ||
|
||
- Create a Jest setup file: | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript CJS"} | ||
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless'; | ||
|
||
setupZonelessTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
```ts title="setup-jest.ts" tab={"label": "TypeScript ESM"} | ||
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless/index.mjs'; | ||
|
||
setupZonelessTestEnv({ | ||
//...options | ||
}); | ||
``` | ||
|
||
- Update your Jest configuration: | ||
|
||
```ts title="jest.config.ts" tab={"label": "TypeScript CJS"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createCjsPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` | ||
|
||
```ts title="jest.config.mts" tab={"label": "TypeScript ESM"} | ||
import type { Config } from 'jest'; | ||
import presets from 'jest-preset-angular/presets'; | ||
|
||
export default { | ||
...presets.createEsmPreset(), | ||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], | ||
} satisfies Config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
website/versioned_docs/version-14.5/guides/jsdom-environment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
id: jsdom-environment | ||
title: JSDOM environment | ||
--- | ||
|
||
`jest-preset-angular` provides a way to configure a different version of [JSDOM](https://github.com/jsdom/jsdom) than the one ships with `Jest` | ||
via a custom `JSDOM` environment. One can follow the below steps to configure a different `JSDOM` version: | ||
|
||
- Install the desired JSDOM version | ||
|
||
```bash npm2yarn | ||
npm install -D jsdom@<desired-version> | ||
``` | ||
|
||
- In Jest config, set the `testEnvironment` like following | ||
|
||
```ts title="jest.config.ts" tab={"label": "TypeScript CJS"} | ||
import type { Config } from 'jest'; | ||
|
||
export default { | ||
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env', | ||
} satisfies Config; | ||
``` | ||
|
||
```ts title="jest.config.mts" tab={"label": "TypeScript ESM"} | ||
import type { Config } from 'jest'; | ||
|
||
export default { | ||
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env', | ||
} satisfies Config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.