Skip to content
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

Tests: incorrect use of .findBy...() #6383

Open
shaunanoordin opened this issue Oct 14, 2024 · 3 comments
Open

Tests: incorrect use of .findBy...() #6383

shaunanoordin opened this issue Oct 14, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@shaunanoordin
Copy link
Member

Incorrect Tests

Noted as of 4062f39

A number of our tests may be returning incorrect results, due to the use of .findBy... selectors.

Example: FinishedAnnouncementConnector.spec.js

it('should show a results link if results page exists', function () {
  render(<DefaultStory />)
  const link = screen.findByLabelText('Announcements.FinishedAnnouncement.seeResults')
  expect(link).exists()
})

This test passes. ✅

  const link = screen.findByLabelText('CALL ME ISHMAEL')
  expect(link).exists()

This test ALSO passes! ❌

This is because .findBy... returns a Promise, which will always be .ok() and will always .exist()

Suggestion: rewrite tests to use .getBy... or .queryBy... instead. Or use await .findBy... where necessary. Example, for FinishedAnnouncementConnector.spec.js:

  const link = screen.getByRole('link')
  expect(link?.getAttribute('href')).to.equal('/projects/zookeeper/galaxy-zoo/about/results')

Dev Notes

This is a list of all `*.spec.js* files that use .findBy, as of 14 Oct 2024. I've linked to the _current_ (master) version instead of 4062f39 so you can quickly check if they're already patched.

app-project

lib-classifier

lib-react-components

❌ indicates the test isn't working correctly (i.e. giving false 'pass'es), as described in this issue.
✅ indicates that the test actually seems OK, since it uses e.g. await screen.findBy...(), which is the correct way to use Promises. (Not sure if it's worth double-checking.)

Status

Not urgent, but should be assigned on the next maintenance week. The incorrect tests means we're missing some safety nets that we assume are there.

@shaunanoordin shaunanoordin added the bug Something isn't working label Oct 14, 2024
@eatyourgreens
Copy link
Contributor

I’ve fixed one of these in #6374.

@eatyourgreens
Copy link
Contributor

A testing tip: specify the accessible name of a node in role queries, to avoid false positives.

Instead of

  const link = screen.getByRole('link')
  expect(link?.getAttribute('href')).to.equal('/projects/zookeeper/galaxy-zoo/about/results')

look for the link by its name. This also helps catch links that don't have accessible link text.

  const link = screen.getByRole('link', { name: 'See results' })
  expect(link?.getAttribute('href')).to.equal('/projects/zookeeper/galaxy-zoo/about/results')

@eatyourgreens
Copy link
Contributor

eatyourgreens commented Oct 23, 2024

#6399 fixes another set of findBy tests that were passing when they should fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants