Skip to content

Commit eed3789

Browse files
committed
address exercise feedback
1 parent c57ad1f commit eed3789

File tree

17 files changed

+74
-15
lines changed

17 files changed

+74
-15
lines changed

exercises/01.sunsetting-jsdom/01.solution.break-jsdom/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 { test, expect } from 'vitest'
21
import { render, screen } from '@testing-library/react'
32
import { FilePreview } from './file-preview.tsx'
43

exercises/01.sunsetting-jsdom/01.solution.break-jsdom/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"compilerOptions": {
55
"target": "esnext",
66
"module": "preserve",
7-
"types": ["@vitest/browser/providers/playwright"]
7+
"types": ["vitest/globals"]
88
}
99
}

exercises/01.sunsetting-jsdom/01.solution.break-jsdom/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import react from '@vitejs/plugin-react'
55
export default defineConfig({
66
plugins: [react()],
77
test: {
8+
globals: true,
89
environment: 'jsdom',
910
},
1011
})

exercises/02.vitest-browser-mode/01.problem.installation-and-setup/README.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The installation step is done, and now it's time to configure Vitest is it can r
2929

3030
To enable the Browser Mode in Vitest, I need to set `test.browser.enabled` to `true` in my `vite.config.ts`/`vitest.config.ts`:
3131

32-
```ts filename=vite.config.ts add=9-11
32+
```ts filename=vite.config.ts remove=9 add=10-12
3333
/// <reference types="vitest" />
3434
import { defineConfig } from 'vite'
3535
import react from '@vitejs/plugin-react'
@@ -38,6 +38,7 @@ export default defineConfig({
3838
plugins: [react()],
3939
test: {
4040
globals: true,
41+
environment: 'jsdom',
4142
browser: {
4243
enabled: true,
4344
},

exercises/02.vitest-browser-mode/01.problem.installation-and-setup/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"compilerOptions": {
55
"target": "esnext",
66
"module": "preserve",
7-
"types": ["@vitest/browser/providers/playwright"]
7+
"types": ["vitest/globals", "@vitest/browser/matchers"]
88
}
99
}

exercises/02.vitest-browser-mode/01.solution.installation-and-setup/README.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Installation and setup
22

3-
That's it! :tada: You have everything you 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.
3+
That's it! :tada: You have everything you need to finally start testing your browser code in the browser.
4+
5+
If you try running tests right now, you will get an error:
6+
7+
```txt nonumber nocopy
8+
TestingLibraryElementError: Unable to find an element with the text: hello world. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
9+
```
10+
11+
This is because you have to refactor your test suite to correctly render your component in the browser. This is exactly what you are going to do in the next exercise.
412

513
## Related materials
614

exercises/02.vitest-browser-mode/01.solution.installation-and-setup/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"compilerOptions": {
55
"target": "esnext",
66
"module": "preserve",
7-
"types": ["vitest/globals"]
7+
"types": ["vitest/globals", "@vitest/browser/matchers"]
88
}
99
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 💣 Remove the import from `@testing-library/react`. You won't need it anymore.
22
import { render, screen } from '@testing-library/react'
3-
// 🐨 Import `page` from '@vitest/browser'
3+
// 🐨 Import `page` from '@vitest/browser/context'
44
// 💰 import { foo } from 'bar'
55
//
66
// 🐨 Import `render` from 'vitest-browser-react'.
@@ -10,7 +10,7 @@ import { FilePreview } from './file-preview.tsx'
1010
test('displays the preview card', async () => {
1111
render(<FilePreview file={new File(['hello world'], 'file.txt')} />)
1212

13-
// 🐨 Replace `expect()` with `expect.element()`.
13+
// 🐨 Replace `expect()` with `await expect.element()`.
1414
expect(
1515
// 🐨 Replace the `screen.getByText` function with
1616
// `page.getByText`.

exercises/02.vitest-browser-mode/02.problem.migrate-the-test/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"compilerOptions": {
55
"target": "esnext",
66
"module": "preserve",
7-
"types": ["vitest/globals"]
7+
"types": ["vitest/globals", "@vitest/browser/matchers"]
88
}
99
}

exercises/02.vitest-browser-mode/02.solution.migrate-the-test/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"compilerOptions": {
55
"target": "esnext",
66
"module": "preserve",
7-
"types": ["vitest/globals"]
7+
"types": ["vitest/globals", "@vitest/browser/matchers"]
88
}
99
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In this workshop, we will be using Playwright as the browser provider for Vitest
2020
👨‍💼 Your task is to introduce Playwright to the existing Vitest setup. It will consist of multiple steps:
2121

2222
1. Install `playwright` as a dependency in the project;
23-
1. Update `test.browser.provider` to use Playwright as the browser provider;
24-
1. Update `tsconfig.test.json` to use the type definitions from Playwright for `expect()` matchers.
23+
1. In <InlineFile file="vite.config.ts">`vite.config.ts`</InlineFile>, update `test.browser.provider` to use `'playwright'` as the browser provider;
24+
1. In <InlineFile file="tsconfig.test.json">`tsconfig.test.json`</InlineFile>, update the `compilerOptions.types` to include the Playwright type definitions for `expect()` matchers.
2525

2626
Once you are done with it, make sure that the tests are still passing by running `npm test`. Good luck!

exercises/02.vitest-browser-mode/03.problem.playwright/tsconfig.test.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"compilerOptions": {
55
"target": "esnext",
66
"module": "preserve",
7-
"types": ["vitest/globals"]
7+
"types": [
8+
"vitest/globals",
9+
// 💣 Remove the default browser matcher types.
10+
"@vitest/browser/matchers"
11+
12+
// 🐨 Instead, include the matcher types from Playwright.
13+
// 💰 "@vitest/browser/providers/playwright"
14+
]
815
}
916
}

exercises/02.vitest-browser-mode/03.problem.playwright/vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default defineConfig({
88
globals: true,
99
browser: {
1010
enabled: true,
11+
// 🐨 Set a custom browser provider via the `provider` option.
12+
// 💰 provider: 'playwright',
1113
instances: [
1214
{
1315
browser: 'chromium',

exercises/02.vitest-browser-mode/04.problem.shared-assets/README.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@ I think this is a great problem for you to fix!
1616

1717
👨‍💼 In this exercise, you will create a custom browser setup file that will run as a global setup for all component tests.
1818

19-
First, create a <InlineFile file="vitest.browser.setup.ts">`vitest.browser.setup.ts`</InlineFile> file. In that file, import any assets you wish to apply to _all tests_ (e.g. CSS).
19+
🐨 First, create a <InlineFile file="vitest.browser.setup.ts">`vitest.browser.setup.ts`</InlineFile> file. In that file, import any assets you wish to apply to _all tests_ (e.g. `./src/index.css`).
2020

21-
> 🦉 To make Vite process the browser setup file like it does the rest of the app, include the following type reference at the top of the setup file: `/// <reference path="./src/vite-env.d.ts" />`.
21+
🐨 Enable TypeScript for the new file by adding it to the `include` array in <InlineFile file="tsconfig.node.json">`tsconfig.node.json`</InlineFile>.
22+
23+
Once you do all this, you will spot that the `*.css` imports fail to resolve in TypeScript.
24+
25+
```txt nonumber nocopy
26+
Cannot find module './src/index.css' or its corresponding type declarations.ts(2307)
27+
```
28+
29+
🐨 To fix this, we need to pull some of the Vite built-in type definitions to help us. At the top of the `vitest.browser.setup.ts` file, add the following type reference comment:
30+
31+
```ts nonumber
32+
/// <reference path="./src/vite-env.d.ts" />
33+
```
2234

2335
Lastly, provide the path to the setup file in <InlineFile file="vite.config.ts">`vite.config.ts`</InlineFile> as the `setupFiles` value of a conrete browser instance (follow the instructions in the configuration file for more details).
2436

exercises/02.vitest-browser-mode/04.problem.shared-assets/vite.config.ts#3:5

Whitespace-only changes.

exercises/02.vitest-browser-mode/04.problem.shared-assets/vite.config.ts@3:5

Whitespace-only changes.

exercises/02.vitest-browser-mode/05.problem.multiple-workspaces/vite.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@ import react from '@vitejs/plugin-react'
44

55
export default defineConfig({
66
plugins: [react()],
7+
// 🐨 Add a new property called `workspace`.
8+
// As the value, provide an array with two entries.
9+
// 💰 workspace: [{}, {}]
10+
//
11+
// 🐨 In the first entry of the `workspace` array,
12+
// define a `test` property and give it a `test.name` equal to "unit".
13+
// 💰 { test: { name: 'unit' } }
14+
//
15+
// 🐨 In the unit test workspace, set `globals` to true
16+
// and `environment` to "node".
17+
//
18+
// 🐨 Finally, declare `include` and `exclude` properties
19+
// that decide which test suites to run.
20+
// Use the "**/*.test.ts" pattern for `include`
21+
// and "**/*.browser.test.ts(x)?" pattern for `exclude.
22+
//
23+
// 🐨 Now, switch to the second entry in the `workspace`
24+
// array. There, give it the following properties:
25+
// {
26+
// name: "browser",
27+
// globals: true,
28+
// include: ["**/*.browser.test.ts(x)?"]
29+
// }
30+
//
31+
// 🐨 Finally, copy the existing `browser` configuration
32+
// under the `test` property of the second workspace.
33+
// 💰 { test: { name: 'browser', browser: {...} }}
34+
//
35+
// 💣 Delete this root-level `test` property altogether.
736
test: {
837
globals: true,
938
browser: {

0 commit comments

Comments
 (0)