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
Assertions passed with a number argument should be conditionally be casted as a string on DOM string accessors #7314
Comments
I think the assertion in the OP should throw an error so the user can fix the problem on their side, because automatically stringifying the argument has some other, confusing, implications. How will this auto-casting handle precision? How will it handle limited float precision? Should this pass too? <div>1.50</div>
// will this pass? String(1.50) === '1.5' !== '1.50'
cy.get("div").should('have.text', 1.50)
// or...
<div>1</div>
// will this pass?
// String(1.00000000000000000000000000000001) === '1'
cy.get("div").should('have.text', 1.00000000000000000000000000000001) Also, numbers above |
This is a valid point - but I think because this is already in an assertion - there's no reason to double throw here. The worst thing that can happen is that our conversion misses and fails for another reason. But not doing the string conversion and keeping the input as a number will always fail. Therefore your suggestion and our incorrect casting would still both result in an error. The provided solution will most of the time work except when there are those edge cases you provided. However, in both cases the casted value will be in the assertion failure message - leading the user to then fix it. For this reason, I say we keep the PR as is and merge. |
I don't know, it feels like a big assumption that this will be an improvement to the user experience. I feel like this will be the user story if this change goes in as-is:
I think for a significant % of people who run into this issue, the solution will be non-obvious. Compare to the user story if we throw an error up front:
I get what you mean, users are probably unlikely to run into this bug... but once they do, it will become very confusing very quickly. 🤷 |
The code for this is done in cypress-io/cypress#7315, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior:
Passing a
number
to the the following assertions always fails, but casting it to a string passes.have.text
have.id
have.data
have.value
have.attr
Desired behavior:
Numbers should automatically be casted to a string when necessary.
Test code to reproduce
And other combinations listed above ^^
Versions
4.5.0
The text was updated successfully, but these errors were encountered: