question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tests data-testid is being ignored / not being passed down

See original GitHub issue

Version 5.1.4

Steps to reproduce

// App.tsx
export default () => (
  <Typeahead
    data-testid='search-bar-input'
    placeholder="Test"
    filterBy={() => true}
    autoFocus={true}
    options={[]}
  />
)

Then import that file into a test using react-testing-library

// App.test.tsx
import { render } from '@testing-library/react';
import App from './App';

it('should be focused', async () => {
  const { getByTestId, container } = render(<App />);
  // console.log(container.innerHTML)
  const input = getByTestId('search-bar-input')
  expect(input).toHaveFocus()
});

Expected Behavior

It should pass the test

Actual Behavior

It cannot find search-bar-input If we do a console.log(container.innerHTML)the result has no ‘search-bar-input’ at all

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
orekavcommented, Mar 22, 2021

Temporally I have added the attribute ‘inputProps’ with a forced typing

inputProps={{
  'data-testid': 'search-bar-input',
} as InputProps}
1reaction
IAMtheIAMcommented, Jul 28, 2021

Passing inputProps without a type works fine and perfectly for react-testing-library! (or any other testing library like Enzyme)

<Typeahead
    inputProps={{
       'name': 'selectItem',
    }}
 />
Read more comments on GitHub >

github_iconTop Results From Across the Web

The data-testid won't work if div isn't wrapped #377 - GitHub
I've been a real lover of react-testing-library these days. ... or it passes the test id down to the child native component that...
Read more >
Unable to find the element with data-testid - Stack Overflow
The react-testing-library is not able to find the element even though it exists. TEST test('Renders step based on the active step', () =>...
Read more >
Configuration Options | Testing Library
The library can be configured via the configure function, which accepts: a plain JS object; this will be merged into the existing configuration....
Read more >
Some components ignore html data attributes
Some components ignore html data attributes. In our unit tests, we sometimes want to be able to use the ByTestId methods of the...
Read more >
Testing-library: avoid these mistakes in async tests
Sometimes, tests start to unexpectedly fail even if no changes were made to the business logic. It may happen after e.g. you updated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found