Skip to content

Use normalized string in getByText error if text not found #450

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

Closed
Cerden opened this issue Feb 11, 2020 · 8 comments · Fixed by #1117
Closed

Use normalized string in getByText error if text not found #450

Cerden opened this issue Feb 11, 2020 · 8 comments · Fixed by #1117
Labels
enhancement New feature or request

Comments

@Cerden
Copy link

Cerden commented Feb 11, 2020

Describe the feature you'd like:

When getByText fails to find text that has been normalized, the error message should show the normalized version of the string instead of the original.

Currently the error message shows the original un-normalized version. This can be confusing as often that exact text can be seen in the DOM logged out afterwards.

A downside is that it might not be immediately obvious why different text is being shown in the error from what was typed. IMO, if the user is unaware of the default normalization behaviour, this scenario makes it easier to track it down in the docs, and is slightly more representative of why the test is failing.

Describe alternatives you've considered:

Possible alternative is to generically mention normalisation in the error message (alongside text being split onto two lines)

Teachability, Documentation, Adoption, Migration Strategy:

N/A

@eps1lon
Copy link
Member

eps1lon commented Feb 11, 2020

Currently the error message shows the original un-normalized version

The error message shows the text you put in and the elements. The input isn't normalized. The text of the nodes is normalized.

Are you proposing to list the possible values for which the input would return a value?

@eps1lon eps1lon added the question Further information is requested label Feb 11, 2020
@Cerden
Copy link
Author

Cerden commented Feb 11, 2020

@eps1lon Ah I see - misunderstanding on my part. In that case I think the confusion is coming from the rendered DOM not showing the same text as the matcher use, which leads to scenarios where you can see that the text is the same, and directly log out a comparison that shows they're the same, but the matcher still fails.

I guess an ideal solution would be to have the rendered DOM normalised based on the same rules the matcher uses - would that be feasible?

@eps1lon eps1lon added enhancement New feature or request and removed question Further information is requested labels Feb 11, 2020
@kentcdodds
Copy link
Member

Hmmm... I don't know whether this is possible 😬 Anyone have ideas?

@eps1lon
Copy link
Member

eps1lon commented Mar 21, 2020

I'm not sure if this is a misunderstanding of the what the normalizer does or if we can do something. @Cerden Could you add a code example where you think the error message is misleading?

@Cerden
Copy link
Author

Cerden commented Mar 23, 2020

@eps1lon
Example code:

App.js

import React from "react";

export const content = "Hello       React";

function App() {
  return <div>{content}</div>;
}

export default App;

App.test.js

import React from "react";
import { render } from "@testing-library/react";
import App, { content } from "./App";

describe("Tests", () => {
  it("should render the content", () => {
    const { getByText } = render(<App />);

    expect(getByText(content)).toBeInTheDocument();
  });
});

Error message:
Screenshot 2020-03-23 at 17 59 25

The part that's misleading to me is that the text that the error message can't find and the text that's shown in the rendered DOM are the same. Ideally (IMO) it should be clearer that the text in the node has been normalised and so the matcher isn't matching against the same string that was originally passed in.

@eps1lon
Copy link
Member

eps1lon commented Mar 25, 2020

@Cerden Thanks.

Do you think it's enough the expand the error message to

Unable to find an element with the text: "Hello React" (normalized from "Hello    React")

?

@JoshuaKGoldberg
Copy link
Contributor

Just piping in a couple months later: I have this issue & request as well, and that suggestion looks great to me @eps1lon. 😄

@MatanBobi
Copy link
Member

🎉 This fix is included in version 8.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

5 participants