-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESLint: Enable testing-library/no-container
rule
#46160
Conversation
@@ -369,7 +369,6 @@ module.exports = { | |||
'plugin:testing-library/react', | |||
], | |||
rules: { | |||
'testing-library/no-container': 'off', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rule is automatically enabled when we include the testing-library/react
plugin above.
const searchResults = within( | ||
screen.getByRole( 'listbox', { | ||
name: /Search results for.*/, | ||
} ) | ||
).getAllByRole( 'option' ); | ||
|
||
const searchResultTextHighlightElements = Array.from( searchResults ) | ||
.map( ( result ) => result.querySelector( 'mark' ) ) | ||
.flat() | ||
.filter( Boolean ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're changing the way we query for those mark
elements to use screen queries.
@@ -48,9 +48,11 @@ describe( 'RangeControl', () => { | |||
/> | |||
); | |||
|
|||
// eslint-disable-next-line testing-library/no-container |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no better way to query for this with a screen query.
const beforeIcon = container.querySelector( | ||
'.dashicons-format-image' | ||
); | ||
// eslint-disable-next-line testing-library/no-container |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no better way to query for this with a screen query.
expect( | ||
container.querySelector( '.has-left-divider button' ) | ||
).toBe( buttons[ 1 ] ); | ||
expect( buttons[ 0 ].parentElement ).not.toHaveClass( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing it that way will create a no-node-access
violation, but we'll take care of that separately by either ignoring it or fixing it. One step at a time!
@@ -184,6 +184,7 @@ describe( 'Tooltip', () => { | |||
// Note: this is testing for implementation details, | |||
// but couldn't find a better way. | |||
const buttonRect = button.getBoundingClientRect(); | |||
// eslint-disable-next-line testing-library/no-container |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This element is not accessible by design, so querying it that way makes sense.
const eventCatcher = | ||
container.getElementsByClassName( 'event-catcher' )[ 0 ]; | ||
// eslint-disable-next-line testing-library/no-container | ||
const eventCatcher = container.querySelector( '.event-catcher' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the same query as above. This element is not accessible by design, so querying it that way makes sense.
@@ -115,6 +115,7 @@ describe( 'UnitControl', () => { | |||
withoutClassName.querySelector( '.components-unit-control' ) | |||
).not.toHaveClass( 'hello' ); | |||
expect( | |||
// eslint-disable-next-line testing-library/no-container |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no better way to access this wrapper element. Alternatively, we could use .parentElement
to traverse to it, but it wouldn't be better since it would introduce a no-node-access
violation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes in these scenarios I like to use toMatchDiffSnapshot
, and I would expect to see as the only line in the snapshot the one showing the hello
classname
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not! Done in 3d7d8a9. There's one more line difference, but that's expected since the two unit controls have different IDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted. This may generate additional differences so it's not suitable for a snapshot test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know, thanks for testing that approach!
Size Change: +1.41 kB (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @tyxla!
I was wondering about these unsolvable cases, disabling linting for those makes sense to me 👍
This reverts commit 3d7d8a9.
* ESLint: Enable testing-library/no-container rule * Use toMatchDiffSnapshot * Revert "Use toMatchDiffSnapshot" This reverts commit 3d7d8a9.
What?
Back when we enabled the
testing-library/react
ESLint plugin, we disabled some rules that needed fixing. We fixed a bunch of them and now it's time to re-enabletesting-library/no-container
.Why?
Our end goal is to follow the best practices of the
@testing-library/react
library for writing tests.How?
We're fixing some violations at the last minute, and for others, we're disabling inline, because they can't be fixed without a solid component refactor.
I'm leaving a self-review to clarify each instance separately.
Testing Instructions
Verify all checks are green - tests pass and there are no new ESLint errors.