Skip to content

Commit 7919c51

Browse files
committed
show RTL matchers migration, ditch .toBeTruthy()
1 parent 6a90d3c commit 7919c51

File tree

15 files changed

+141
-28
lines changed

15 files changed

+141
-28
lines changed

exercises/01.sunsetting-jsdom/01.problem.break-jsdom/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@tailwindcss/vite": "^4.0.7",
1515
"@testing-library/dom": "^10.4.0",
16+
"@testing-library/jest-dom": "^6.6.3",
1617
"@testing-library/react": "^16.1.0",
1718
"@testing-library/user-event": "^14.5.2",
1819
"@types/react": "^19.0.6",
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '@testing-library/jest-dom/vitest'
12
import { render, screen } from '@testing-library/react'
23

34
test('displays the preview card', () => {
@@ -6,10 +7,10 @@ test('displays the preview card', () => {
67
// Provide a new `File` instance as the value of the `file` prop.
78
// 💰 new File(['hello world'], 'file.txt')
89
//
9-
// 🐨 Write an assertion that expects the file name to be present
10-
// in the DOM (you can use `.toBeTruthy()` for now).
10+
// 🐨 Write an assertion that expects the file name to be visible.
1111
// 💰 screen.getByText(expectedText)
12+
// 💰 expect(element).toBeVisible()
1213
//
1314
// 🐨 Similarly, write another assertion that expects
14-
// the file contents to be present in the DOM.
15+
// the file contents to be visible for the user.
1516
})

exercises/01.sunsetting-jsdom/01.solution.break-jsdom/README.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Here, two things have my interest:
2424
I reflect those two expectations like so:
2525

2626
```tsx nonumber nocopy
27-
expect(screen.getByText('file.txt')).toBeTruthy()
28-
expect(screen.getByText('hello world')).toBeTruthy()
27+
expect(screen.getByText('file.txt')).toBeVisible()
28+
expect(screen.getByText('hello world')).toBeVisible()
2929
```
3030

3131
Looks like the first test is done. All that's left is to run via `npm test` and... Oh, no.

exercises/01.sunsetting-jsdom/01.solution.break-jsdom/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@tailwindcss/vite": "^4.0.7",
1515
"@testing-library/dom": "^10.4.0",
16+
"@testing-library/jest-dom": "^6.6.3",
1617
"@testing-library/react": "^16.1.0",
1718
"@types/react": "^19.0.6",
1819
"@types/react-dom": "^19.0.3",
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import '@testing-library/jest-dom/vitest'
12
import { render, screen } from '@testing-library/react'
23
import { FilePreview } from './file-preview.tsx'
34

45
test('displays the preview card', () => {
56
render(<FilePreview file={new File(['hello world'], 'file.txt')} />)
67

7-
expect(screen.getByText('file.txt')).toBeTruthy()
8-
expect(screen.getByText('hello world')).toBeTruthy()
8+
expect(screen.getByText('file.txt')).toBeVisible()
9+
expect(screen.getByText('hello world')).toBeVisible()
910
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ It's time to take the first step forward Vitest Browser Mode! In this one, you a
77
As the first order of business, I'm going to uninstall the packages I won't be needing anymore:
88

99
```sh nonumber
10-
npm uninstall jsdom @testing-library/react
10+
npm uninstall jsdom @testing-library/react @testing-library/jest-dom/vitest
1111
```
1212

1313
> 🦉 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.

exercises/02.vitest-browser-mode/01.problem.installation-and-setup/src/file-preview.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { FilePreview } from './file-preview.tsx'
44
test('displays the preview card', () => {
55
render(<FilePreview file={new File(['hello world'], 'file.txt')} />)
66

7-
expect(screen.getByText('file.txt')).toBeTruthy()
8-
expect(screen.getByText('hello world')).toBeTruthy()
7+
expect(screen.getByText('file.txt')).toBeVisible()
8+
expect(screen.getByText('hello world')).toBeVisible()
99
})

exercises/02.vitest-browser-mode/01.solution.installation-and-setup/src/file-preview.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { FilePreview } from './file-preview.tsx'
44
test('displays the preview card', async () => {
55
render(<FilePreview file={new File(['hello world'], 'file.txt')} />)
66

7-
expect(screen.getByText('file.txt')).toBeTruthy()
8-
expect(screen.getByText('hello world')).toBeTruthy()
7+
expect(screen.getByText('file.txt')).toBeVisible()
8+
expect(screen.getByText('hello world')).toBeVisible()
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
@@ -15,9 +15,9 @@ test('displays the preview card', async () => {
1515
// 🐨 Replace the `screen.getByText` function with
1616
// `page.getByText`.
1717
screen.getByText('file.txt'),
18-
).toBeTruthy()
18+
).toBeVisible()
1919

2020
// 🐨 Using the previous assertion as an example,
2121
// apply the necessary changes to this `expect` as well.
22-
expect(screen.getByText('hello world')).toBeTruthy()
22+
expect(screen.getByText('hello world')).toBeVisible()
2323
})

exercises/02.vitest-browser-mode/02.solution.migrate-the-test/README.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ Finally, it's time to adjust the assertions around the elements rendered by our
7070
This is how they looked like before:
7171

7272
```tsx filename=file-preview.test.tsx nonumber
73-
expect(page.getByText('file.txt')).toBeTruthy()
74-
expect(page.getByText('hello world')).toBeTruthy()
73+
expect(page.getByText('file.txt')).toBeVisible()
74+
expect(page.getByText('hello world')).toBeVisible()
7575
```
7676

7777
There's a few things to change here.

0 commit comments

Comments
 (0)