Skip to content

Commit d51c17c

Browse files
committed
02/03: improve readme
1 parent 2bfe0ce commit d51c17c

File tree

1 file changed

+57
-1
lines changed
  • exercises/02.vitest-browser-mode/03.solution.playwright

1 file changed

+57
-1
lines changed

exercises/02.vitest-browser-mode/03.solution.playwright/README.mdx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,60 @@ Problem: Using the default `preview` provider in Vitest is great to try things o
99

1010
---
1111

12-
// TODO: Mention `"types": ["@vitest/browser/providers/playwright"]` in `tsconfig.node.json` to have `.toBeVisible()` and other nice browser assertions!
12+
Let's start from installing `playwright`:
13+
14+
```sh
15+
npm i -D playwright
16+
```
17+
18+
Playwright needs browser executables to be present on your machine to run. In this workshop, I am using Chromium as my browser of choice, so I can install just that particular browser by running the following command:
19+
20+
```sh
21+
npx playwright install --with-dewps chromium
22+
```
23+
24+
> 🦉 You can provide the Playwright CLI with the explicit browsers you want to be installed. This reduces its footprint in the system by fetching only the browsers you need.
25+
26+
The next step is to tell Vitest to use Playwright as the browser provider for component tests. In `vite.config.ts`, set the `test.browser.provider` option to `'playwright'`:
27+
28+
```ts filename=vite.config.ts add=11
29+
/// <reference types="vitest" />
30+
import { defineConfig } from 'vite'
31+
import react from '@vitejs/plugin-react'
32+
33+
export default defineConfig({
34+
plugins: [react()],
35+
test: {
36+
globals: true,
37+
browser: {
38+
enabled: true,
39+
provider: 'playwright',
40+
instances: [
41+
{
42+
browser: 'chromium',
43+
},
44+
],
45+
},
46+
},
47+
})
48+
```
49+
50+
And, finally, let's update the type definitions for the assertion matchers to be those from Playwright in `tsconfig.test.json`:
51+
52+
```json filename=tsconfig.test.json remove=9 add=10
53+
{
54+
"extends": "./tsconfig.base.json",
55+
"include": ["vite.config.ts", "**/*.test.ts*"],
56+
"compilerOptions": {
57+
"target": "esnext",
58+
"module": "preserve",
59+
"types": [
60+
"vitest/globals",
61+
"@vitest/browser/matchers",
62+
"@vitest/browser/providers/playwright"
63+
]
64+
}
65+
}
66+
```
67+
68+
This will make sure that the locators, user events, and matchers are correctly annotated.

0 commit comments

Comments
 (0)