Description
@testing-library/jest-dom
version: 5.10.0
Relevant code or config:
render(<select data-testid="displayvalue">
<option value="null"></option>
<option value="choice" selected>Choice 1</option>
</select>
);
expect(screen.getByTestId("displayvalue")).toHaveDisplayValue(""); // Passes?
expect(screen.getByTestId("displayvalue")).toHaveDisplayValue(/^$/); // Fails
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