Skip to content

Comments

docs(browser): add render libraries examples#9325

Merged
sheremet-va merged 20 commits intovitest-dev:mainfrom
sheremet-va:docs/add-render-functions-examples
Feb 5, 2026
Merged

docs(browser): add render libraries examples#9325
sheremet-va merged 20 commits intovitest-dev:mainfrom
sheremet-va:docs/add-render-functions-examples

Conversation

@sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Dec 22, 2025

Description

Resolves #9019

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link

netlify bot commented Dec 22, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit b7f7d1e
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/698481fd769e0e00080ec65d
😎 Deploy Preview https://deploy-preview-9325--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Member

@macarie macarie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like having these in the main docs, they're easier to discover 😄

Just a couple of small suggestions regarding wording. I believe some of them (the container-related ones) also apply to the Vue page.

Copy link
Contributor

@mcous mcous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Got a few minor corrections for the Svelte usage since its options aren't exactly symmetrical with React testing library

`vitest-browser-angular` returns APIs that interact well with built-in [locators](/api/browser/locators), [user events](/api/browser/interactivity) and [assertions](/api/browser/assertions): for example, Vitest will automatically retry the element until the assertion is successful, even if it was rerendered between the assertions.
:::

The package exposes two entry points: `vitest-browser-angular` and `vitest-browser-angular/pure`. They expose identical API, but the `pure` entry point doesn't add a handler to remove the component before the next test has started.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is accurate, the package.json currently doesn't export the /pure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a convention in other packages, do you think it makes sense to expose in angular?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I didn't know pure was exposed in other packages.
What are the use cases for the pure entry point? Why would users opt-out of cleanup?

Also, there are two Angular specificities:

  1. whether users are using Angular CLI or Analog, cleanup will be registered between tests (either through afterEach or beforeEach depending on an Angular's TestBed flag)

  2. not cleaning up between tests will often break because the the TestBed has two phases, a configuration phase, and an instantiation phase. Once a component is instantiated, calling a configuration API would fail unless there was a cleanup.

Therefore, I still can't see the value of exposing pure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other frameworks people can disable automatic cleanup to do it manually. For example, to move it into a different hook, or add an extra action before/after cleanup. If this is not advisable with how angular testing works, we don't have to add this entry point


## Setup

Before using `vitest-browser-angular`, you need to set up the test environment using `@analogjs/vitest-angular`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to take the approach that @yjaaidi and I took which is to point to the analog vitest docs (because the setup might change from time to time so you don't want to keep maintaining this).

So we could point to their docs (like with do on the README) and also give a link to our vitest setup file (for the repo's own tests) to show a practical example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, as someone who is not familiar with angular, I was very confused by the setup instructions in vitest-browser-angular and had to spend more time than I'd like in unrelated (to my eye) documentation (analog)

Copy link
Member Author

@sheremet-va sheremet-va Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to follow this principle in our docs:

Be Self-Contained - While there are a number of existing tools Vitest pulls in or works like (Chai, Jest, etc…), whenever something is important enough to include in documentation, descriptions and usage examples should be included in the docs instead of linked to an outside source. This will help keep users focused on their task at hand without jumping between multiple tabs to understand a single concept.

#279

Changing a doc if something changes doesn't seem bad in my opinion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The think about Angular is that we currently have two options:

  • Angular CLI's unit-test builder (the official approach which has limitations as it wraps Vitest instead of providing a plugin)
  • Analog approach for those who want more features, more control, and more alignment with Vitest.

We could either document the Angular CLI approach and link to the Analog documentation for those who want to use it.
Or document both in different sections or tabs.

For Angular, enabling browser mode resides in adding browsers option in angular.json's test config.
https://angular.dev/guide/testing#running-tests-in-a-browser:~:text=Once%20the%20provider,angular.json

{
  "projects": {
    "your-project-name": {
      "architect": {
        "test": {
          "builder": "@angular/build:unit-test",
          "options": {
            "browsers": ["Chromium", "Firefox", "Safari"] // Add "Headless" suffix to enable headless 🤷
          }
        }
      }
    }
  }
}

Copy link
Member Author

@sheremet-va sheremet-va Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document all possible approaches. Setting this up is frankly annoying without a proper guide. We don't need to document all config options (like the headless note), just how to get started with testing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree if the approach is to copy their docs every time they change, we need to show both, not just one way

Copy link
Member Author

@sheremet-va sheremet-va Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let’s show both. We don’t need to copy the whole doc, just the setup with a link to the original doc that expands what we showed. At the moment readme from your package lead me to analog doc that I just copied here. Turns out, it’s not actually correct. Which means the current approach to just simply link is flawed

Copy link
Contributor

@shairez shairez Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well each one has its trade-offs IMO, I don't think one is flawed and the other is not, it's just a matter of how many places you want to keep submitting the fix PR to... 😅

(and I'm not sure the analog docs has a mistake, what younes said is that it's just not relevant if one goes with the native angular cli way, but AFAIK you do need the compiler if you go with analog )

I originally had all the setup steps as part of the README of the package, but after talking with @yjaaidi , we agreed that it's best to link to analog's setup as it keeps being updated, than to play catch with it every time it changes.

Angular, unfortunately, has its own way of doing things ( I wish it was just a vite plugin and we could consolidate everything into one thing), and that forces us to instead of showing one way we need to show 2 ways... which is not fun.

But we can show 2 minimal setups, and we'll just need to keep in mind we'll need to maintain them as things change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey guys, I've just created a PR for vitest-browser-angular README.
I tried to document both approaches (Angular CLI & Analog) while embedding instructions that should work out of the box.

Once @shairez reviews it, we could use it as a source of inspiration for the docs.

selector: 'app-hello-world',
template: '<h1>Hello, {{ name() }}!</h1>',
})
export class HelloWorldComponent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would remove the Component suffix. The convention was removed from the Angular style guide + it aligns more with other frameworks.

`vitest-browser-angular` returns APIs that interact well with built-in [locators](/api/browser/locators), [user events](/api/browser/interactivity) and [assertions](/api/browser/assertions): for example, Vitest will automatically retry the element until the assertion is successful, even if it was rerendered between the assertions.
:::

The package exposes two entry points: `vitest-browser-angular` and `vitest-browser-angular/pure`. They expose identical API, but the `pure` entry point doesn't add a handler to remove the component before the next test has started.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I didn't know pure was exposed in other packages.
What are the use cases for the pure entry point? Why would users opt-out of cleanup?

Also, there are two Angular specificities:

  1. whether users are using Angular CLI or Analog, cleanup will be registered between tests (either through afterEach or beforeEach depending on an Angular's TestBed flag)

  2. not cleaning up between tests will often break because the the TestBed has two phases, a configuration phase, and an instantiation phase. Once a component is instantiated, calling a configuration API would fail unless there was a cleanup.

Therefore, I still can't see the value of exposing pure.


## Setup

Before using `vitest-browser-angular`, you need to set up the test environment using `@analogjs/vitest-angular`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The think about Angular is that we currently have two options:

  • Angular CLI's unit-test builder (the official approach which has limitations as it wraps Vitest instead of providing a plugin)
  • Analog approach for those who want more features, more control, and more alignment with Vitest.

We could either document the Angular CLI approach and link to the Analog documentation for those who want to use it.
Or document both in different sections or tabs.

For Angular, enabling browser mode resides in adding browsers option in angular.json's test config.
https://angular.dev/guide/testing#running-tests-in-a-browser:~:text=Once%20the%20provider,angular.json

{
  "projects": {
    "your-project-name": {
      "architect": {
        "test": {
          "builder": "@angular/build:unit-test",
          "options": {
            "browsers": ["Chromium", "Firefox", "Safari"] // Add "Headless" suffix to enable headless 🤷
          }
        }
      }
    }
  }
}


Before using `vitest-browser-angular`, you need to set up the test environment using `@analogjs/vitest-angular`:

::: code-group
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the Angular CLI approach, we don't need the configuration below.

Copy link
Member Author

@sheremet-va sheremet-va Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very confusing how to configure vitest-browser-angular to work. The readme just redirects to analog, so these examples are from there. If it is not valid, then let's update README and this doc to include all possible setups. I have no idea how to set it up


::: code-group
```ts [zoneless]
import '@angular/compiler'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have to discuss this with @shairez. @angular/compiler should not be necessary, especially if we enable AOT which is the default behavior with the Angular CLI unit-test builder.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example from analog linked from vitest-browser-angular README


### componentClassInstance

The actual Angular component class instance. Use this to access component methods and properties directly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a warning here to discourage this usage.
The component instance should be considered an implementation detail.

@yjaaidi
Copy link
Contributor

yjaaidi commented Feb 4, 2026

@shairez suggested that we contribute directly to this PR. Would that work for you @sheremet-va?

I just took time to read everything thoroughly and here is a quick summary of the changes that I would make:

  1. I would update the first example to be interactive (e.g. a counter just like other frameworks.
  2. Update setup instructions as in docs: embed setup instructions for Angular CLI & Analog vitest-community/vitest-browser-angular#21
  3. Remove the example from componentProviders option as it can be considered an advanced use case and users should pick the providers option unless they know what they are doing
  4. Do not document the imports option. Users should use providers combined with importProvidersFrom
  5. Do not document the withRouting, componentClassInstance, router, and routerHarness. This is something that we still have to discuss internally but I'd rather not expose these in the docs until we decide whether we keep them or not.
  6. Update fixture example by removing the fixture.detectChanges call — it's better to discourage users from using it.
  7. We could add a link to https://angular.dev/api/core/testing/ComponentFixture# in fixture
  8. Probably remove the examples at the bottom. Having an interactive first example such as a counter is probably enough.

What do you think Shai?
Depending on Shai's feedback we can then:

  • suggest changes on this PR
  • make a PR against this PR
  • remove the angular.md file from this PR so that we do not block other frameworks then submit a dedicated Angular PR

Whatever works for you Vlad.

@sheremet-va
Copy link
Member Author

remove the angular.md file from this PR so that we do not block other frameworks then submit a dedicated Angular PR

Let's add the angular one in a separate PR to unblock this one.

@sheremet-va sheremet-va merged commit 70d4c2e into vitest-dev:main Feb 5, 2026
10 checks passed
@sheremet-va sheremet-va deleted the docs/add-render-functions-examples branch February 5, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation: add vitest-browser-* packages to the API section

5 participants