-
Notifications
You must be signed in to change notification settings - Fork 403
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
toHaveDisplayValue checks for substring rather than equality when passed string value? #290
toHaveDisplayValue checks for substring rather than equality when passed string value? #290
Comments
I looked into fixing this, and #242 is using the |
Hmm, I think the behavior in toHaveDescription is pretty much what I was imagining. Here's the snippet of relevant logic (it's just inline, not factored out into a function): ...
checkWith instanceof RegExp
? checkWith.test(description)
: this.equals(description, checkWith)
... That's what fits my intuition, at least. It is inconsistent in the way you describe, but I agree that with a RegExp you have an easy option to anchor at the beginning and end, so I don't think it's a big problem. Also, looking at the documentation, it kinda implies that this is the way it should behave, because all of the examples where a string is passed seem to be using the entire expected display value, not a substring: expect(input).toHaveDisplayValue('Luca')
expect(input).toHaveDisplayValue(/Luc/)
expect(textarea).toHaveDisplayValue('An example description here.')
expect(textarea).toHaveDisplayValue(/example/)
expect(selectSingle).toHaveDisplayValue('Select a fruit...')
expect(selectSingle).toHaveDisplayValue(/Select/)
expect(selectMultiple).toHaveDisplayValue([/Avocado/, 'Banana']) |
I also face with this behaviour. In my test the following three passes: expect(input).toHaveValue('1995.12.17 00:00');
expect(input).toHaveDisplayValue('1995.12.17');
expect(input).toHaveDisplayValue('12.17'); IT is not the thing I would suppose. |
@kiripeti To be clear, do you expect the first line to pass and the last two to fail? |
I was also very surprised to discover this behaviour and I've been puzzled for quite a long time. I wanted to check that after I click on the delete button my form values are cleared and to my surprise the following test (written first) passed:
Especially when using testing library the behaviour is unexpected: if I try to do |
🎉 This issue has been resolved in version 5.11.7 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@testing-library/jest-dom
version: 5.10.0Relevant code or config:
Hi, I'm not sure if this is a bug, but it was pretty surprising behavior to me, and took a while to figure out. It seems like the toHaveDisplayValue assertion doesn't do an exact match if you pass a string as the expected value, but an "includes" match. If you want to check that the display value of an element is empty, for example, you have to use a regular expression, as shown above. (The test is designed to fail)
It also looks like before regular expressions were supported in
.toHaveDisplayValue
, it did do an exact match (though I have not tested this, only looked at the code changes). It seems to me more intuitive that when passed a string, it should check for a full exact match, especially since that's how.toHaveValue
works. Thoughts? Thank you!Example code in a sandbox:
https://codesandbox.io/s/react-testing-library-demo-forked-g5udj?file=/src/__tests__/hello.js
The text was updated successfully, but these errors were encountered: