You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/prefer-screen-queries.md
+22-4Lines changed: 22 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,16 @@
1
-
# Suggest using `screen` while using queries (`testing-library/prefer-screen-queries`)
1
+
# Suggest using `screen` while querying (`testing-library/prefer-screen-queries`)
2
2
3
3
## Rule Details
4
4
5
-
DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.
6
-
This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result. Given the screen component does not expose utility methods such as `rerender()` or the `container` property, it is correct to use the `render` response in those scenarios.
5
+
DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.
6
+
7
+
This rule aims to force writing tests using built-in queries directly from `screen` object rather than destructuring them from `render` result. Given the screen component does not expose utility methods such as `rerender()` or the `container` property, it is correct to use the `render` returned value in those scenarios.
8
+
9
+
However, there are 3 exceptions when this rule won't suggest using `screen` for querying:
10
+
11
+
1. You are using a query chained to `within`
12
+
2. You are using custom queries, so you can't access them through `screen`
13
+
3. You are setting the `container` or `baseElement`, so you need to use the queries returned from `render`
-[Common mistakes with React Testing Library - Not using `screen`](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-screen)
0 commit comments