-
Notifications
You must be signed in to change notification settings - Fork 4
docs: embed setup instructions for Angular CLI & Analog #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yjaaidi
wants to merge
3
commits into
main
Choose a base branch
from
docs/update-setup-instructions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,21 +1,127 @@ | ||
| # vitest-browser-angular | ||
|
|
||
| Render Angular components in VItest Browser Mode. | ||
| This community package renders Angular components in [Vitest Browser Mode](https://vitest.dev/guide/browser). | ||
|
|
||
| ## Installation | ||
| ```ts | ||
| import { Component, input } from '@angular/core' | ||
| import { expect, test } from 'vitest' | ||
| import { render } from 'vitest-browser-angular' | ||
|
|
||
| @Component({ | ||
| selector: 'app-hello-world', | ||
| template: '<h1>Hello, {{ name() }}!</h1>', | ||
| }) | ||
| export class HelloWorld { | ||
| name = input.required<string>() | ||
| } | ||
|
|
||
| test('renders name', async () => { | ||
| const { locator } = await render(HelloWorld, { | ||
| inputs: { | ||
| name: 'World', | ||
| }, | ||
| }) | ||
|
|
||
| await expect.element(locator).toHaveTextContent('Hello, World!') | ||
| }) | ||
| ``` | ||
|
|
||
| ## Setup | ||
|
|
||
| There are currently two ways to set up Vitest for Angular: | ||
| - Angular CLI's [`unit-test` builder](https://angular.dev/guide/testing#configuration) *(official)*. | ||
| - Analog's [`vitest-angular` plugin](https://analogjs.org/docs/features/testing/vitest) *(community)*. | ||
|
|
||
|
|
||
| While Angular CLI's `unit-test` builder is the official way to set up Vitest for Angular, it has some [limitations](https://analogjs.org/docs/features/testing/overview#angular-support-for-vitest). Analog's `vitest-angular` plugin provides more Vitest features and greater flexibility. | ||
|
|
||
| ### Setup with Analog Plugin | ||
|
|
||
| 1. Set up Vitest | ||
|
|
||
| ```sh | ||
| pnpm add -D vitest-browser-angular | ||
| npm add -D @analogjs/platform @nx/devkit vitest-browser-angular | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🫤 we need to add the |
||
|
|
||
| ng g @analogjs/platform:setup-vitest | ||
| ``` | ||
|
|
||
| ## Setup Test Environment | ||
| 2. Activate browser mode in the generated Vitest configuration by following the [browser mode configuration instructions](https://vitest.dev/guide/browser/#configuration). | ||
|
|
||
| ### Setup with Angular CLI | ||
|
|
||
| 1. Configure your Angular project to use the `@angular/build:unit-test` builder, and add the browsers of your choice. | ||
|
|
||
| ```json | ||
| { | ||
| ..., | ||
| "projects": { | ||
| "my-app": { | ||
| ..., | ||
| "architect": { | ||
| "test": { | ||
| "builder": "@angular/build:unit-test", | ||
| "options": { | ||
| "browsers": ["Chromium", "Firefox", "Webkit"] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| *Since Angular v21, Vitest is the default runner so you don't need to set the `runner` option.* | ||
|
|
||
| To set up your test environment (with Zone.js or Zoneless), use `@analogjs/vitest-angular`'s `setupTestBed()` function. | ||
| 2. Install the browser provider of your choice using `ng add` | ||
|
|
||
| **Important:** Make sure to use `{ browserMode: true }` when calling `setupTestBed()` to enable Vitest browser mode's visual test preview functionality. | ||
| ```sh | ||
| # With Playwright | ||
| ng add @vitest/browser-playwright | ||
|
|
||
| # or with WebdriverIO | ||
| ng add @vitest/browser-webdriverio | ||
| ``` | ||
|
|
||
| 3. Add the `vitest-browser-angular` package to your project. | ||
|
|
||
| ```sh | ||
| npm add -D vitest-browser-angular | ||
| ``` | ||
|
|
||
|
|
||
| ## Zone.js and Zoneless Setup | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Maybe |
||
|
|
||
| Angular CLI will automatically set up the test environment for you depending on the presence of `zone.js` in your project's polyfills. | ||
|
|
||
| When using the Analog plugin, you can control the behavior using the `zoneless` option of `setupTestBed()` in `test-setup.ts`: | ||
|
|
||
| ```ts | ||
| import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed'; | ||
|
|
||
| setupTestBed({ | ||
| zoneless: true, | ||
| }); | ||
| ``` | ||
|
|
||
| For detailed setup instructions for both Zone.js and Zoneless configurations, please refer to the [Analog Vitest documentation](https://analogjs.org/docs/features/testing/vitest). | ||
|
|
||
|
|
||
| ## Component Preview | ||
|
|
||
| To preview, debug and interact with a component in the browser after the test, you can prevent Angular from destroying it. | ||
|
|
||
| In Angular CLI, enable this using the `--debug` option. | ||
|
|
||
| With the Analog plugin, enable this using the `browserMode` option of `setupTestBed()` in `test-setup.ts`: | ||
|
|
||
| ```ts | ||
| import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed'; | ||
|
|
||
| setupTestBed({ | ||
| browserMode: true, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Example | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! Just noticed that the previous example was using
pnpm.Let's document
npminstead ofpnpmbecausepnpmusers know how to translate that. It doesn't work the other way.Documenting all options is an alternative but while it works with tabs, it pollutes in a flat README like this.