DEV Community

Discussion on: Enzyme or react-testing-library and why?

yuritoledo profile image
Yuri Toledo Author

Yeah, its clear, thank you bro

Thread Thread
nersoh profile image
Nelson Henrique • Edited on

"And I hate the fact that I cant find a comp by id or name hehe"

I don't know if you already figured out how to do it, but I actually can by doing, for example:

const { container } = render(Component);
expect(container.querySelector("#myid")).not.toBeNull();

Thread Thread
lacasa profile image
𝙻𝚊𝚌𝚊𝚜𝚊 • Edited on

You can create your custom selectors like this:

import { queryByAttribute, render } from '@testing-library/react';

const getByName = queryByAttribute.bind(null, 'name');

it('should...', () => {
  const { container } = render(<Component />);
  expect(getByName(container, 'the-name').toEqual(...)
})

I have some custom selectors created in a utils file (name, type, id...).