Skip to content

typescript declaration for ToHaveValue doesn't allow null #240

Closed
@timrobinson33

Description

@timrobinson33
  • @testing-library/jest-dom version: 5.5.0
  • node version: 12.16.2
  • npm (or yarn) version: 6.14.4
  • react-testing-library version: (if applicable) 10.0.4

Relevant code or config:

expect(inputElement).toHaveValue(null);

==> typescript warning

What you did:

I'm using <input type="number"> to prompt for an optional numeric value. It seems the obvious way to handle an empty value is to use null , and certainly React doesn't give any warnings when you try to do this.

What happened:

Typescript gives a warning because the method signature for toHaveValue doesn't allow null. using the non-null assertion operator (i.e. expect(inputElement).toHaveValue(null!); works fine and is the best workaround IMO.

It might be possible to set an empty string to indicate an empty value, but this would require me to declare my state variable as number | string | null which is definitely not desirable

Suggested solution:

From what I can tell there are already some tests calling it with null (just google toHaveValue null and they come up at the top of the list), so I presume it is valid to do this and you just need to update the typescript declarations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions