testing-library / testing-library/dom-testing-library

Queries having multiple types

Open
#1,305 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

Describe the feature you'd like:

Happy to have a go at this!

When looking into this issue I opened with the React testing library I took a peek at the source code here to see if it could be fixed here. I noticed that some of the queries have the wrong types locally but are fine when published. For example in the below screenshot the queryByRole has three possible types, which include those of queryAllByRole and findByRole, which are the wrong types:

VSCode screenshot of queries with wrong types

It looks like the type is only correct in the published packaged because the return type is explicitly defined here. I then found this todo (from 3 years ago!) when looking for the function implementation.

Suggested implementation:

Unsure yet, maybe adding an explicit return tuple type to buildQueries? I don't know the knock on effect this would have but it seems like a quick fix, screenshots below (only did return types for queryByRole and findByRole for brevity):

VSCode screenshot of explicit return tuple types VSCode screenshot of using return types

However I think the problem is bigger as the function implementation type deviates from the published type and could lead to unexpected behaviour. IMHO it would be better for the function implementation to create the type, not overwrite it.

Describe alternatives you've considered:

Open to suggestions

Teachability, Documentation, Adoption, Migration Strategy:

Would improve DX for maintainers and contributors.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Compare the implementation and inferred types in src/query-helpers.ts, especially the buildQueries TODO, with the explicit declarations in types/queries.d.ts around line 280. Trace the queryByRole, queryAllByRole, and findByRole types, then verify that the implementation produces the same precise types as the published package without needing declaration overrides.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience, testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.