testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.26k stars 466 forks source link

Custom queries created with `buildQueries` do not play nice with `throwSuggestions` #1189

Open CreativeTechGuy opened 1 year ago

CreativeTechGuy commented 1 year ago

Relevant code or config:

// Setup
import { configure } from '@testing-library/dom';
configure({
    throwSuggestions: true,
});
// Later build Custom Queries
const [
  queryByCustom,
  getAllByCustom,
  getByCustom,
  findAllByCustom,
  findByCustom,
] = buildQueries(queryAllByCustom, getMultipleError, getMissingError);

(Actual example of real-world custom queries from shadow-dom-testing-library.)

What you did:

When calling one of these custom queries created with buildQueries it'll try to suggest better queries which often don't work because these queries are custom for a reason.

What happened:

Testing Library will fail the test and suggest a different query despite it not being correct.

Reproduction:

It's not easy to reproduce in CodeSandbox but hopefully the description here is clear enough.

Problem description:

Using custom queries require { suggest: false } options in many places as tests are incorrectly failing due to bad suggestions.

Suggested solution:

Easiest solution, give an option to disable suggestions when using buildQueries. Custom queries should rarely if ever be treated the same as native queries and suggestions don't make sense for them.

The perfect world solution would be to make throwSuggestions aware of all custom queries to be able to accurately suggest those too and take them into account. But I highly doubt this is possible or reasonable.