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

Feature Request: add typeguard to query options to improve TS type checking

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

Nobody has claimed this yet.

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

Description

Describe the feature you'd like:

It would be nice to be able to add a typeguard to query options.

My use case (and I imagine it's a common one) is to assert that the element I am querying for is of a particular type. Currently, I do this with a separate check:

const { getByLabelText } = render(<MyComponent />)
const myCheckbox = getByLabelText('Checkbox Label') // returns type `HTMLElement`
// I need to assert that it is actually an HTMLInputElement, otherwise typescript
// complains about the assertion below, "property 'checked' does not exist on type 'HTMLElement'
if (!(myInput instanceof HTMLInputElement)) {
  throw new Error('Selected input is not an input')
}
expect(myCheckbox.checked).toBe(true)

It would be nice to have a simple built-in way to tell Typescript that we're definitely getting an HTMLInputElement (or any other specified type)

Suggested implementation:

possibly:

const myCheckbox = getByLabelText(
  'Checkbox Label',
  {
    instanceOf: HTMLInputElement
  }
)

Or, more flexible yet more verbose:

const inputTypeguard = (el: HTMLElement): el is HTMLInputElement => el instanceof HTMLInputElement
const myCheckbox = getByLabelText(
  'Checkbox Label',
  {
    typeguard: inputTypeguard
  }
)

I believe I have the chops to implement this if it is something that is worth adding!

Describe alternatives you've considered:

So far, I've just been writing my own helper functions for this

Teachability, Documentation, Adoption, Migration Strategy:

Teachability, Documentation, Adoption: This would be a helper for those using Typescript, the docs would need to explain this in the options API for each ByXXX query.

Migration: Should not present any problems as it would be adding an optional argument to the options.

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

The issue names no files or tests; start by reviewing the options API for each ByXXX query and the existing TypeScript query signatures. Compare the proposed instanceOf and typeguard shapes, including their type-narrowing behavior and runtime checks. Done means the API is agreed, implemented, and documented for the relevant query options without breaking existing optional arguments.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.