Skip to content

Commit f4faebf

Browse files
committed
docs: overhaul unit testing overview guide
This commit introduces a series of improvements to the unit testing overview guide to enhance clarity, organization, and accuracy. The guide has been updated with a more streamlined flow and clearer explanations. Key changes include: - A reorganized configuration section with more logical subsections for `angular.json` options and global setup. - A completely restructured "Running tests in a browser" section, which now groups each browser provider with its installation commands, includes links to official documentation, and provides clearer instructions for controlling headless mode. - A note clarifying that the `@vitest/browser-preview` provider is not suitable for CI environments. - An update to the continuous integration guide to use the more modern `--watch=false` flag.
1 parent 789f91b commit f4faebf

File tree

1 file changed

+82
-34
lines changed

1 file changed

+82
-34
lines changed

adev/src/content/guide/testing/overview.md

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
Testing your Angular application helps you check that it is working as you expect. Unit tests are crucial for catching bugs early, ensuring code quality, and facilitating safe refactoring.
44

5-
NOTE: This guide focuses on the default testing setup for new Angular CLI projects. If you are migrating an existing project from Karma to Vitest, see the [Migrating from Karma to Vitest guide](guide/testing/migrating-to-vitest). While Vitest is the default test runner, Karma is still fully supported. For information on testing with Karma, see the [Karma testing guide](guide/testing/karma).
5+
NOTE: This guide covers the default testing setup for new Angular CLI projects, which uses Vitest. If you are migrating an existing project from Karma, see the [Migrating from Karma to Vitest guide](guide/testing/migrating-to-vitest). Karma is still supported; for more information, see the [Karma testing guide](guide/testing/karma).
66

77
## Set up for testing
88

9-
The Angular CLI downloads and installs everything you need to test an Angular application with the [Vitest testing framework](https://vitest.dev). By default, new projects include `vitest` and `jsdom`.
9+
The Angular CLI downloads and installs everything you need to test an Angular application with the [Vitest testing framework](https://vitest.dev). New projects include `vitest` and `jsdom` by default.
1010

11-
Vitest runs your unit tests in a Node.js environment, using `jsdom` to emulate the DOM. This allows for faster test execution by avoiding the overhead of launching a browser. You can also use `happy-dom` as an alternative by installing it and removing `jsdom`. The CLI will automatically detect and use `happy-dom` if it is present.
11+
Vitest runs your unit tests in a Node.js environment. To simulate the browser's DOM, Vitest uses a library called `jsdom`. This allows for faster test execution by avoiding the overhead of launching a browser. You can swap `jsdom` for an alternative like `happy-dom` by installing it and uninstalling `jsdom`.
1212

13-
The project you create with the CLI is immediately ready to test. Just run the [`ng test`](cli/test) CLI command:
13+
The project you create with the CLI is immediately ready to test. Run the [`ng test`](cli/test) command:
1414

1515
```shell
1616
ng test
@@ -31,26 +31,29 @@ The console output looks like this:
3131
Duration 2.46s (transform 615ms, setup 2ms, collect 2.21s, tests 5ms)
3232
```
3333

34-
The `ng test` command also watches for changes. To see this in action, make a small change to `app.ts` and save it. The tests run again, and the new results appear in the console.
34+
The `ng test` command also watches your files for changes. If you modify a file and save it, the tests will run again.
3535

3636
## Configuration
3737

38-
The Angular CLI handles most of the Vitest configuration for you. For many common use cases, you can adjust the test behavior by modifying options directly in your `angular.json` file.
38+
The Angular CLI handles most of the Vitest configuration for you. You can customize the test behavior by modifying the `test` target options in your `angular.json` file.
3939

40-
### Built-in configuration options
41-
42-
You can change the following options in the `test` target of your `angular.json` file:
40+
### Angular.json options
4341

4442
- `include`: Glob patterns for files to include for testing. Defaults to `['**/*.spec.ts', '**/*.test.ts']`.
4543
- `exclude`: Glob patterns for files to exclude from testing.
4644
- `setupFiles`: A list of paths to global setup files (e.g., polyfills or global mocks) that are executed before your tests.
4745
- `providersFile`: The path to a file that exports a default array of Angular providers for the test environment. This is useful for setting up global test providers which are injected into your tests.
4846
- `coverage`: A boolean to enable or disable code coverage reporting. Defaults to `false`.
49-
- `browsers`: An array of browser names to run tests in (e.g., `["chromium"]`). Requires a browser provider to be installed.
47+
- `browsers`: An array of browser names to run tests in a real browser (e.g., `["chromium"]`). Requires a browser provider to be installed. See the [Running tests in a browser](#running-tests-in-a-browser) section for more details.
48+
49+
### Global test setup and providers
50+
51+
The `setupFiles` and `providersFile` options are particularly useful for managing global test configuration.
5052

5153
For example, you could create a `src/test-providers.ts` file to provide `provideHttpClientTesting` to all your tests:
5254

5355
```typescript
56+
// src/test-providers.ts
5457
import { Provider } from '@angular/core';
5558
import { provideHttpClient } from '@angular/common/http';
5659
import { provideHttpClientTesting } from '@angular/common/http/testing';
@@ -73,11 +76,7 @@ You would then reference this file in your `angular.json`:
7376
"test": {
7477
"builder": "@angular/build:unit-test",
7578
"options": {
76-
"include": ["src/**/*.spec.ts"],
77-
"setupFiles": ["src/test-setup.ts"],
78-
"providersFile": "src/test-providers.ts",
79-
"coverage": true,
80-
"browsers": ["chromium"]
79+
"providersFile": "src/test-providers.ts"
8180
}
8281
}
8382
}
@@ -86,13 +85,15 @@ You would then reference this file in your `angular.json`:
8685
}
8786
```
8887

89-
### Advanced: Custom Vitest configuration
88+
HELPFUL: When creating new TypeScript files for test setup or providers, like `src/test-providers.ts`, ensure they are included in your project's test TypeScript configuration file (typically `tsconfig.spec.json`). This allows the TypeScript compiler to properly process these files during testing.
9089

91-
For advanced use cases, you can provide a custom Vitest configuration file.
90+
### Advanced Vitest configuration
9291

93-
IMPORTANT: While using a custom configuration enables advanced options, the Angular team does not provide direct support for the specific contents of the configuration file or for any third-party plugins used within it. The CLI will also override certain properties (`test.projects`, `test.include`) to ensure proper operation.
92+
For advanced use cases, you can provide a custom Vitest configuration file using the `configFile` option in `angular.json`.
9493

95-
You can create a Vitest configuration file (e.g., `vitest-base.config.ts`) and reference it in your `angular.json` using the `runnerConfig` option.
94+
IMPORTANT: While using a custom configuration enables advanced options, the Angular team does not provide support for the contents of the configuration file or for any third-party plugins. The CLI will also override certain properties (`test.projects`, `test.include`) to ensure proper integration.
95+
96+
You can create a Vitest configuration file (e.g., `vitest-base.config.ts`) and reference it in your `angular.json`:
9697

9798
```json
9899
{
@@ -119,24 +120,27 @@ ng generate config vitest
119120

120121
This creates a `vitest-base.config.ts` file that you can customize.
121122

122-
HELPFUL: Read more about Vitest configuration in the [Vitest configuration guide](https://vitest.dev/config/).
123+
HELPFUL: Read more about Vitest configuration in the [official Vitest documentation](https://vitest.dev/config/).
123124

124125
## Code coverage
125126

126-
You can generate code coverage reports by adding the `--coverage` flag to the `ng test` command. The report is generated in the `coverage/` directory.
127+
You can generate a code coverage report by adding the `--coverage` flag to the `ng test` command. The report is generated in the `coverage/` directory.
127128

128-
For more detailed information on prerequisites, enforcing coverage thresholds, and advanced configuration, see the [Code coverage guide](guide/testing/code-coverage).
129+
For more detailed information, see the [Code coverage guide](guide/testing/code-coverage).
129130

130131
## Running tests in a browser
131132

132133
While the default Node.js environment is faster for most unit tests, you can also run your tests in a real browser. This is useful for tests that rely on browser-specific APIs (like rendering) or for debugging.
133134

134-
To run tests in a browser, you must first install a browser provider.
135+
To run tests in a browser, you must first install a browser provider. Beyond installing the provider and specifying the `browsers` option in `angular.json` or as a CLI flag, no further configuration is required.
136+
137+
Read more about Vitest's browser mode in the [official documentation](https://vitest.dev/guide/browser).
138+
135139
Choose one of the following browser providers based on your needs:
136140

137-
- **Playwright**: `@vitest/browser-playwright` for Chromium, Firefox, and WebKit.
138-
- **WebdriverIO**: `@vitest/browser-webdriverio` for Chrome, Firefox, Safari, and Edge.
139-
- **Preview**: `@vitest/browser-preview` for Webcontainer environments (like StackBlitz).
141+
### Playwright
142+
143+
[Playwright](https://playwright.dev/) is a browser automation library that supports Chromium, Firefox, and WebKit.
140144

141145
<docs-code-multifile>
142146
<docs-code header="npm" language="shell">
@@ -153,33 +157,77 @@ Choose one of the following browser providers based on your needs:
153157
</docs-code>
154158
</docs-code-multifile>
155159

156-
Once the provider is installed, you can run your tests in the browser using the `--browsers` flag:
160+
### WebdriverIO
161+
162+
[WebdriverIO](https://webdriver.io/) is a browser and mobile automation test framework that supports Chrome, Firefox, Safari, and Edge.
163+
164+
<docs-code-multifile>
165+
<docs-code header="npm" language="shell">
166+
npm install --save-dev @vitest/browser-webdriverio webdriverio
167+
</docs-code>
168+
<docs-code header="yarn" language="shell">
169+
yarn add --dev @vitest/browser-webdriverio webdriverio
170+
</docs-code>
171+
<docs-code header="pnpm" language="shell">
172+
pnpm add -D @vitest/browser-webdriverio webdriverio
173+
</docs-code>
174+
<docs-code header="bun" language="shell">
175+
bun add --dev @vitest/browser-webdriverio webdriverio
176+
</docs-code>
177+
</docs-code-multifile>
178+
179+
### Preview
180+
181+
The `@vitest/browser-preview` provider is designed for Webcontainer environments like StackBlitz and is not intended for use in CI/CD.
182+
183+
<docs-code-multifile>
184+
<docs-code header="npm" language="shell">
185+
npm install --save-dev @vitest/browser-preview
186+
</docs-code>
187+
<docs-code header="yarn" language="shell">
188+
yarn add --dev @vitest/browser-preview
189+
</docs-code>
190+
<docs-code header="pnpm" language="shell">
191+
pnpm add -D @vitest/browser-preview
192+
</docs-code>
193+
<docs-code header="bun" language="shell">
194+
bun add --dev @vitest/browser-preview
195+
</docs-code>
196+
</docs-code-multifile>
197+
198+
Once the provider is installed, you can run your tests in the browser using the `--browsers` flag. Tests run in a headed browser by default. If the `CI` environment variable is set, headless mode is used instead. To explicitly control headless mode, you can suffix the browser name with `Headless` (e.g., `chromiumHeadless`).
157199

158200
```bash
159-
# Example for Playwright
201+
# Example for Playwright (headed)
160202
ng test --browsers=chromium
161203

162-
# Example for WebdriverIO
204+
# Example for Playwright (headless)
205+
ng test --browsers=chromiumHeadless
206+
207+
# Example for WebdriverIO (headed)
163208
ng test --browsers=chrome
209+
210+
# Example for WebdriverIO (headless)
211+
ng test --browsers=chromeHeadless
164212
```
165213

166-
Headless mode is enabled automatically if the `CI` environment variable is set. Otherwise, tests will run in a headed browser.
214+
For more advanced browser-specific configuration, see the [Advanced Vitest configuration](#advanced-vitest-configuration) section.
167215

168216
## Other test frameworks
169217

170-
You can also unit test an Angular application with other testing libraries and test runners. Each library and runner has its own distinctive installation procedures, configuration, and syntax.
218+
You can also unit test an Angular application with other testing libraries and test runners. Each library and runner has its own installation procedures, configuration, and syntax.
171219

172220
## Testing in continuous integration
173221

174-
A robust test suite is a key part of a continuous integration (CI) pipeline. CI servers let you set up your project repository so that your tests run on every commit and pull request.
222+
A robust test suite is a key part of a continuous integration (CI) pipeline. CI servers let you automate your tests to run on every commit and pull request.
175223

176-
To test your Angular application in a continuous integration (CI) server, you can typically run the standard test command:
224+
To test your Angular application in a CI server, run the standard test command:
177225

178226
```shell
179227
ng test
180228
```
181229

182-
Most CI servers set a `CI=true` environment variable, which `ng test` detects. This automatically runs your tests in the appropriate non-interactive, single-run mode.
230+
Most CI servers set a `CI=true` environment variable, which `ng test` detects. This automatically configures your tests to run in a non-interactive, single-run mode.
183231

184232
If your CI server does not set this variable, or if you need to force single-run mode manually, you can use the `--no-watch` and `--no-progress` flags:
185233

0 commit comments

Comments
 (0)