Skip to content

Wrong typings for queries by attribute #239

Closed
@danielkcz

Description

@danielkcz
  • react-testing-library version: 5.3.1
  • react version: 16.7.0-alpha.2
  • typescript version: 3.2.1
  • node version: 10.12.0
  • yarn version: 1.10.1

Relevant code or config:

const { queryByRole } = render(<Something />)
expect(queryByRole('alertdialog')).toBeFalsy()

What you did:

As you can see there is argument called text, but it's of HTMLElement type.

image

Tried the following to satisfy typings, but then it fails in runtime with error TypeError: matcher.test is not a function apparently because the container is nor string or a function, so it expects the RegExp.

image

Reproduction:

I would create one, but CodeSandbox for TypeScript is rather wonky and doesn't work correctly every time. I think that images above are a clear reproduction :) Whole repo is probably overkill

Problem description:

It's rather curious that getByRole has no such issue.

image

Suggested solution:

I tried to look into it, but my current mental capacity cannot cover this. For once I don't get how getByRole is typed correctly without any modification on react-testing-library side, but queries are wrong. This is some advanced TypeScript technique I am failing to comprehend.

@jpavon @pbomb Looks like you touched that part before. Do you have some insight perhaps?

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypeScriptRelated to TypeScript. Note: only certain maintainers handle TypeScript labeled issues.released

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions