Skip to content

Display provided selector in *ByText error message #1158

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
raplemie opened this issue Aug 15, 2022 · 1 comment · Fixed by #1159
Closed

Display provided selector in *ByText error message #1158

raplemie opened this issue Aug 15, 2022 · 1 comment · Fixed by #1159
Labels

Comments

@raplemie
Copy link
Contributor

Describe the feature you'd like:

When getByText returns an error message it currently does not mention if any selector other than "*" was provided to the query and returns the prettied body. (Where you might actually see the text you are looking for...)

Adding any selector different than "*" might bring attention to them to the reviewer of the test straight from the error message and help understand why it wasnt found.

Suggested implementation:

In src/queries/text.ts:

const getMissingError: GetErrorFunction<[Matcher, SelectorMatcherOptions]> = (
  c,
  text,
  options = {},
) => {
  const {collapseWhitespace, trim, normalizer, selector} = options
  const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
  const normalizedText = matchNormalizer(text.toString())
  const isNormalizedDifferent = normalizedText !== text.toString()
  const isUsingSelector = selector !== "*";
  return `Unable to find an element with the text: ${
    isNormalizedDifferent
      ? `${normalizedText} (normalized from '${text}')`
      : text
  }${
    isUsingSelector
      ? `, which satisfies selector: '${selector}'
      : ""
  }. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`
}

Describe alternatives you've considered:

I did not see much alternatives to draw attention to the selector other than this suggestion.

Teachability, Documentation, Adoption, Migration Strategy:

As this is only adding information to the error messages if a praticular feature is used, I doubt the documentation would require any change, adoption or migration, unless the messages are parsed in some way...

@github-actions
Copy link

github-actions bot commented Oct 8, 2022

🎉 This issue has been resolved in version 8.19.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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant