Skip to content

Throw better error message by findByRole query #970

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
vinaymahamuni opened this issue Jun 4, 2021 · 4 comments · Fixed by #1143
Closed

Throw better error message by findByRole query #970

vinaymahamuni opened this issue Jun 4, 2021 · 4 comments · Fixed by #1143
Labels
enhancement New feature or request

Comments

@vinaymahamuni
Copy link

Describe the feature you'd like:

Currently if findByRole query doesn't find the element for given role and name, it throws error with message TestingLibraryElementError: Unable to find role="button"

Test to verify above behavior

it("Doesn't tell where it failed", async () => {
  render(<button>Click me</button>)
  
  await screen.findByRole('button', { name: 'submit' })
  await screen.findByRole('button', { name: 'Click me' })
})

In above test, I don't get feedback for which name the findByRole query failed.

For same scenario, if getByRole query is used, it gives the name as well
TestingLibraryElementError: Unable to find an accessible element with the role "button" and name "submit"

This helps in debugging. Specially when it fails on CI.

Suggested implementation:

I looked the code. This behavior happen for all the queries which internally use runWithExpensiveErrorDiagnosticsDisabled function to run callback. In src/queries/role.js there is getMissingError function. In that there is one condition applied which tries to avoid expensive error diagnostics. This condition applies to findByRole function as well. I agree with decision. But I think that adding only namehint to the error string won't hit the performance.

Describe alternatives you've considered:

Currently, I try to figure out where error might have occur using line number printed in stacktrace. But that is very difficult as I am using typescript + snowpack+ web test runner which runs test on browser and I don't get easy access to generated js files

Teachability, Documentation, Adoption, Migration Strategy:

we can easily extract out function returning namehint and reuse it in first if condition

const getNameHint = (name) => {
  let nameHint = ''
  if (name === undefined) {
    nameHint = ''
  } else if (typeof name === 'string') {
    nameHint = ` and name "${name}"`
  } else {
    nameHint = ` and name \`${name}\``
  }
  return nameHint
}

const getMissingError = (
  container,
  role,
  {hidden = getConfig().defaultHidden, name} = {},
) => {
  if (getConfig()._disableExpensiveErrorDiagnostics) {
    return `Unable to find role="${role}${getNameHint(name)}"`
  }
  ...
  ...
  ...
  ...
  ...
  ...
  ...
}

@vinaymahamuni vinaymahamuni changed the title Throw better error in findByRole query Throw better error message by findByRole query Jun 4, 2021
@MatanBobi
Copy link
Member

Hi @vinaymahamuni, thanks for opening this one!
I think the reason this is happening is because findBy* functions are using waitFor under the hood to wait for the result.
waitFor runs the query with a flag to get a minimal error.
I'm not too familiar with why are we doing that just for waitFor. Maybe @eps1lon will know?

@eps1lon
Copy link
Member

eps1lon commented Oct 18, 2021

@MatanBobi The why is not questioned by the issue. For its rationale, check the git-blame: #590

As to the issue about the missing name hint: I'm all for it. Thanks for catching this 👍🏻

@eps1lon eps1lon added the enhancement New feature or request label Oct 18, 2021
@MatanBobi
Copy link
Member

@MatanBobi The why is not questioned by the issue. For its rationale, check the git-blame: #590

@eps1lon I wasn't questioning the why, just wanted to understand the use case to see that a fix for this one won't cause a regression, sorry if it seemed like I was questioning it.

Regarding this issue, I'm up for doing it but I'm not sure how we can without running the expensive part of understanding the correct error message. Any ideas?

@eps1lon
Copy link
Member

eps1lon commented Oct 18, 2021

Regarding this issue, I'm up for doing it but I'm not sure how we can without running the expensive part of understanding the correct error message. Any ideas?

The issuer just asked for adding the expected name. That seems fine to me and doesn't require any expensive diagnostics as far as I can tell.

sorry if it seemed like I was questioning it.

No need to appologize.

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.

3 participants