Skip to content

Commit

Permalink
docs(browser): add vanilla example (#7049)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Dec 8, 2024
1 parent 3c18ecf commit 545cfa2
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion docs/guide/browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,31 @@ Headless mode is not available by default. You need to use either [`playwright`]

## Examples

Vitest provides packages to render components for several popular frameworks out of the box:
By default, you don't need any external packages to work with the Browser Mode:

```js [example.test.js]
import { expect, test } from 'vitest'
import { page } from '@vitest/browser/context'
import { render } from './my-render-function.js'

test('properly handles form inputs', async () => {
render() // mount DOM elements

// Asserts initial state.
await expect.element(page.getByText('Hi, my name is Alice')).toBeInTheDocument()

// Get the input DOM node by querying the associated label.
const usernameInput = page.getByLabelText(/username/i)

// Type the name into the input. This already validates that the input
// is filled correctly, no need to check the value manually.
await usernameInput.fill('Bob')

await expect.element(page.getByText('Hi, my name is Bob')).toBeInTheDocument()
})
```

However, Vitest also provides packages to render components for several popular frameworks out of the box:

- [`vitest-browser-vue`](https://github.com/vitest-dev/vitest-browser-vue) to render [vue](https://vuejs.org) components
- [`vitest-browser-svelte`](https://github.com/vitest-dev/vitest-browser-svelte) to render [svelte](https://svelte.dev) components
Expand Down Expand Up @@ -491,6 +515,8 @@ For unsupported frameworks, we recommend using `testing-library` packages:
- [`@solidjs/testing-library`](https://testing-library.com/docs/solid-testing-library/intro) to render [solid](https://www.solidjs.com) components
- [`@marko/testing-library`](https://testing-library.com/docs/marko-testing-library/intro) to render [marko](https://markojs.com) components

You can also see more examples in [`browser-examples`](https://github.com/vitest-tests/browser-examples) repository.

::: warning
`testing-library` provides a package `@testing-library/user-event`. We do not recommend using it directly because it simulates events instead of actually triggering them - instead, use [`userEvent`](/guide/browser/interactivity-api) imported from `@vitest/browser/context` that uses Chrome DevTools Protocol or Webdriver (depending on the provider) under the hood.
:::
Expand Down

0 comments on commit 545cfa2

Please sign in to comment.