Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking if input value is empty string using toHaveValue #545

Open
romulof opened this issue Oct 30, 2023 · 2 comments
Open

Checking if input value is empty string using toHaveValue #545

romulof opened this issue Oct 30, 2023 · 2 comments

Comments

@romulof
Copy link

romulof commented Oct 30, 2023

  • @testing-library/jest-dom version: 5.14.1
  • node version: 18.18.2
  • jest (or vitest) version: 29.7.0
  • npm (or yarn) version: 1.22.19 (yarn)

Relevant code or config:

expect(inputElement).toHaveValue('');

What you did:

I wanted to check if an input value is empty.

What happened:

Jest fails assertion saying that expeted '' but received null

Reproduction:

Just create an input with empty value.

Problem description:

jest-dom internally converts empty string to null which is confusing, because when I write the code bellow it works:

expect(inputElement.value).toBe('');

Suggested solution:

Either revert this internal conversion, or also toHaveValue parameter if it is an empty string.

@tomjakubowski
Copy link

One workaround is to replace .toHaveValue("") with .toBeEmpty()

@gabisanmartin12
Copy link

toBeEmpty() is deprecated. This is the right way:

expect(inputElement).not.toHaveValue()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants