|
1 | 1 | # Browser Mode |
2 | 2 |
|
3 | | -Tabs have the ability to leverage `playwright` and `vitest`'s browser mode to ensure that the interactive features of the tab actually |
4 | | -behave like they should. |
| 3 | +Vitest provides a special "browser" mode that runs tests directly in a simulated browser environment and provides utilities |
| 4 | +to test interactive features. |
5 | 5 |
|
6 | | -The default testing configuration for tabs has browser mode disabled. This is so that if your tab doesn't need the features that Playwright provides, |
| 6 | +This is particularly useful for tabs, but bundles can also leverage browser mode to test browser-only functionalities. Refer to the `pix_n_flix` |
| 7 | +bundle for such an example. |
| 8 | + |
| 9 | +The default testing configuration has browser mode disabled. This is so that if your dom't need the features that Playwright provides, |
7 | 10 | `vitest` won't need to spin up the Playwright instance. |
8 | 11 |
|
9 | 12 | ## Setting up Browser Mode |
10 | 13 |
|
11 | | -Should you wish to enable browser mode, create a custom `vitest` configuration file for your tab with the `browser.enabled` option set to `true`: |
| 14 | +Should you wish to enable browser mode, [create](./1-general#custom-test-configuration) a custom `vitest` configuration file for your tab with the `browser.enabled` option set to `true`: |
12 | 15 |
|
13 | | -```js {9} |
14 | | -// @ts-check |
| 16 | +```js {8} |
15 | 17 | import { defineProject } from 'vitest/config'; |
16 | 18 |
|
17 | 19 | export default defineProject({ |
@@ -49,8 +51,8 @@ Now, the tests for your tab can be run in browser mode. |
49 | 51 | Writing interactive tests is not very different from writing regular tests. Most of the time, the usual test and assertion functions will suffice. |
50 | 52 |
|
51 | 53 | > [!INFO] |
52 | | -> While writing your tests, you should use watch mode. This will allow `vitest` to open a browser and actually display what your tab looks like whilst |
53 | | -> being rendered during the test. |
| 54 | +> While writing your tests, you should use watch mode. This will allow `vitest` to open a browser and actually display what your rendered |
| 55 | +> component looks like during the test. |
54 | 56 |
|
55 | 57 | Use the `render` function provided `vitest-browser-react` to render your tab/component to a screen. Note that it should be awaited. |
56 | 58 | The returned value can then be interacted with: |
@@ -85,7 +87,7 @@ test('Testing my component', async () => { |
85 | 87 |
|
86 | 88 | ### `expect.poll` and `expect.element` |
87 | 89 |
|
88 | | -Sometimes, visual elements take a while to finish or an element might take a while to load. If you just directly used an assertion, the assertion |
| 90 | +Sometimes, visual elements take a while to finish or an element might take a while to load. If you just used an assertion normally, the assertion |
89 | 91 | might fail because the element hasn't displayed yet: |
90 | 92 |
|
91 | 93 | ```tsx |
@@ -169,7 +171,7 @@ assertions or clicking). |
169 | 171 |
|
170 | 172 | `vitest` provides several ways to "locate" an element helpfully called [locators](https://vitest.dev/guide/browser/locators.html). |
171 | 173 |
|
172 | | -While writing tabs, if we believe that a component will need to be interacted with during unit testing, we can use attributes like `title` to make it |
| 174 | +While writing components, if we believe they will need to be interacted with during unit testing, we can use attributes like `title` to make it |
173 | 175 | easier to refer to these elements: |
174 | 176 |
|
175 | 177 | ```tsx |
|
0 commit comments