Skip to content

Commit 1d367b4

Browse files
committed
02/02: add solution readme
1 parent f3c70b5 commit 1d367b4

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed
Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
# Installation and setup
22

3-
0. Uninstall JSDOM.
4-
1. Install Vitest and the Vitest browser package.
5-
1. Create Vitest configuration (or extend it).
3+
## Install dependencies
4+
5+
As the first order of business, I'm going to uninstall the packages I won't be needing anymore:
6+
7+
```sh
8+
npm uninstall jsdom @testing-library/react
9+
```
10+
11+
> 🦉 I am uninstalling React Testing Library because I won't be using it directly for my in-browser tests. **That doesn't mean its API and practices are gone!** Instead, I will continue using them through the Vitest and Playwright APIs that are inspired by RTL and promote its best practices.
12+
13+
Now I can install the dependencies required for the Browser Mode in Vitest:
14+
15+
```sh
16+
npm i -D @vitest/browser vitest-browser-react playwright
17+
```
18+
19+
Let's break down what these packages do:
20+
21+
- [`@vitest/browser`](https://www.npmjs.com/package/@vitest/browser) enables the browser mode and provides the bindings to interact with the underlying browser;
22+
- [`vitest-browser-react`](https://www.npmjs.com/package/vitest-browser-react) provisions rendering of React components (similar to `@testing-library/react`);
23+
- [`playwright`](https://www.npmjs.com/package/playwright) will serve as the actual browser automation (a browser provider) that Vitest will use to run tests.
24+
25+
The installation step is done, and now it's time to configure Vitest is it can run my component tests in the actual browser.
26+
27+
## Configure Vitest
28+
29+
To enable the Browser Mode in Vitest, I need to set `test.browser.enabled` to `true` in my `vite.config.ts`/`vitest.config.ts`:
30+
31+
```ts add=9-11
32+
/// <reference types="vitest" />
33+
import { defineConfig } from 'vite'
34+
import react from '@vitejs/plugin-react'
35+
36+
export default defineConfig({
37+
plugins: [react()],
38+
test: {
39+
globals: true,
40+
browser: {
41+
enabled: true,
42+
},
43+
},
44+
})
45+
```
46+
47+
As the next step, I need to tell Vitest what browsers I want for testing my components. Let's use Chromium by setting the `test.browser.instances` correctly:
48+
49+
```ts add=10-14
50+
/// <reference types="vitest" />
51+
import { defineConfig } from 'vite'
52+
import react from '@vitejs/plugin-react'
53+
54+
export default defineConfig({
55+
plugins: [react()],
56+
test: {
57+
browser: {
58+
enabled: true,
59+
instances: [
60+
{
61+
browser: 'chromium',
62+
},
63+
],
64+
},
65+
},
66+
})
67+
```
68+
69+
> 🦉 You can configure _multiple browser instances_ to execute your component tests. This is handy for solid cross-browser code coverage.
70+
71+
That's it! :tada: I have everything I need to finally start testing my browser code in the browser. Now, it's time to refactor the existing test to leverage the beauty of Vitest Browser Mode.

exercises/02.vitest-browser-mode/02.solution.migrate-the-test/src/file-preview.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { it, expect } from 'vitest'
21
import { page } from '@vitest/browser/context'
32
import { render } from 'vitest-browser-react'
43
import { FilePreview } from './file-preview.tsx'

0 commit comments

Comments
 (0)