Skip to content

Commit 9bfe1c4

Browse files
committed
02/02: update element assertions in the solution
1 parent 2944906 commit 9bfe1c4

File tree

1 file changed

+9
-12
lines changed
  • exercises/02.vitest-browser-mode/02.solution.migrate-the-test

1 file changed

+9
-12
lines changed

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

+9-12
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,24 @@ That being said, you don't normally deal with HTML elements directly. Instead, y
6565

6666
## Element assertions
6767

68-
Finally, it's time to adjust the assertions around the elements rendered by our `<FilePreview />` component.
69-
70-
This is how they looked like before:
68+
With React Testing Library, our element assertions looked like this:
7169

7270
```tsx filename=file-preview.test.tsx nonumber
73-
expect(page.getByText('file.txt')).toBeVisible()
74-
expect(page.getByText('hello world')).toBeVisible()
71+
expect(screen.getByText('file.txt')).toBeVisible()
72+
expect(screen.getByText('hello world')).toBeVisible()
7573
```
7674

77-
There's a few things to change here.
75+
Since `screen` has been replaced by `page` from Vitest, we need to adjust them.
7876

79-
Now that I know that `page.getBy*` functions return _locators_, they will always be truthy and so the `.toBeTruthhy()` assertion makes little sense here. It wasn't written from the user's perspective, to begin with, and it would be best to use a more fitting matcher like `.toBeVisible()`:
80-
81-
```tsx filename=file-preview.test.tsx nonumber
77+
```tsx filename=file-preview.test.tsx nonumber remove=1,4 add=2,5
78+
expect(screen.getByText('file.txt')).toBeVisible()
8279
expect(page.getByText('file.txt')).toBeVisible()
83-
// ^^^^^^^^^^^^^
80+
81+
expect(screen.getByText('hello world')).toBeVisible()
8482
expect(page.getByText('hello world')).toBeVisible()
85-
// ^^^^^^^^^^^^^
8683
```
8784

88-
On its own, this is enough. But there's one more thing that can make these assertions stellar. I can use `expect.element()` instead of `expect()` to have a built-in retry for resolving the given locators:
85+
There's one more thing we can do to make these assertions rock-solid. If we use `await expect.element()` instead of plain `expect()`, we can rely on Vitest's built-in retry mechanism when resolving the given locators;
8986

9087
```tsx filename=file-preview.test.tsx nonumber
9188
await expect.element(page.getByText('file.txt')).toBeVisible()

0 commit comments

Comments
 (0)