docs(browser): add render libraries examples#9325
docs(browser): add render libraries examples#9325sheremet-va merged 20 commits intovitest-dev:mainfrom
Conversation
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
…der-functions-examples
…der-functions-examples
…der-functions-examples
macarie
left a comment
There was a problem hiding this comment.
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.
mcous
left a comment
There was a problem hiding this comment.
Looks great! Got a few minor corrections for the Svelte usage since its options aren't exactly symmetrical with React testing library
docs/api/browser/angular.md
Outdated
| `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. |
There was a problem hiding this comment.
I'm not sure this is accurate, the package.json currently doesn't export the /pure
There was a problem hiding this comment.
This is a convention in other packages, do you think it makes sense to expose in angular?
There was a problem hiding this comment.
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:
-
whether users are using Angular CLI or Analog, cleanup will be registered between tests (either through afterEach or beforeEach depending on an Angular's
TestBedflag) -
not cleaning up between tests will often break because the the
TestBedhas 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.
There was a problem hiding this comment.
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
docs/api/browser/angular.md
Outdated
|
|
||
| ## Setup | ||
|
|
||
| Before using `vitest-browser-angular`, you need to set up the test environment using `@analogjs/vitest-angular`: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
Changing a doc if something changes doesn't seem bad in my opinion
There was a problem hiding this comment.
The think about Angular is that we currently have two options:
- Angular CLI's
unit-testbuilder (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 🤷
}
}
}
}
}
}There was a problem hiding this comment.
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
There was a problem hiding this comment.
I agree if the approach is to copy their docs every time they change, we need to show both, not just one way
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
docs/api/browser/angular.md
Outdated
| selector: 'app-hello-world', | ||
| template: '<h1>Hello, {{ name() }}!</h1>', | ||
| }) | ||
| export class HelloWorldComponent { |
There was a problem hiding this comment.
nit: I would remove the Component suffix. The convention was removed from the Angular style guide + it aligns more with other frameworks.
docs/api/browser/angular.md
Outdated
| `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. |
There was a problem hiding this comment.
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:
-
whether users are using Angular CLI or Analog, cleanup will be registered between tests (either through afterEach or beforeEach depending on an Angular's
TestBedflag) -
not cleaning up between tests will often break because the the
TestBedhas 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.
docs/api/browser/angular.md
Outdated
|
|
||
| ## Setup | ||
|
|
||
| Before using `vitest-browser-angular`, you need to set up the test environment using `@analogjs/vitest-angular`: |
There was a problem hiding this comment.
The think about Angular is that we currently have two options:
- Angular CLI's
unit-testbuilder (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 🤷
}
}
}
}
}
}
docs/api/browser/angular.md
Outdated
|
|
||
| Before using `vitest-browser-angular`, you need to set up the test environment using `@analogjs/vitest-angular`: | ||
|
|
||
| ::: code-group |
There was a problem hiding this comment.
With the Angular CLI approach, we don't need the configuration below.
There was a problem hiding this comment.
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
docs/api/browser/angular.md
Outdated
|
|
||
| ::: code-group | ||
| ```ts [zoneless] | ||
| import '@angular/compiler' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This is an example from analog linked from vitest-browser-angular README
docs/api/browser/angular.md
Outdated
|
|
||
| ### componentClassInstance | ||
|
|
||
| The actual Angular component class instance. Use this to access component methods and properties directly. |
There was a problem hiding this comment.
I would add a warning here to discourage this usage.
The component instance should be considered an implementation detail.
|
@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:
What do you think Shai?
Whatever works for you Vlad. |
Let's add the angular one in a separate PR to unblock this one. |
Description
Resolves #9019
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.