Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Testing Library - "messageParent" can only be used inside a worker

I'm testinng a react component using the RTL, and everytime I ran a test, I get,

"messageParent" can only be used inside a worker

**Here's the code

describe('Header', () => {
  it('validates header component is rendered', () => {
    const { getByTestId } = render(<Header patientName="Jake Bland" />);
    expect(getByTestId('patient-name')).toHaveTextContent(/^Jake Bland$/);
    expect(getByTestId('dateRange')).toBe(dateRange);
  });
});

Any help on this will be greatly appreciated.

like image 755
napster499 Avatar asked May 26 '21 21:05

napster499


People also ask

How does react testing library work?

React Testing Library encourages you to test the behavior of your application instead of implementation details. By testing your application the way a user would use it, you can be confident that your application will behave as expected when all test cases have passed.

How do you test the react component with a hook?

If you need to test a custom Hook, you can do so by creating a component in your test, and using your Hook from it. Then you can test the component you wrote. To reduce the boilerplate, we recommend using React Testing Library which is designed to encourage writing tests that use your components as the end users do.

Is react testing library jest?

React Testing Library is not an alternative to Jest. Each performs a clear task, and you need them both to test your components. Jest is a test runner that finds tests, runs the tests, and determines whether the tests passed or failed.


Video Answer


1 Answers

I was having the same issue, and it was caused by calling toBe() or toEqual() on an HTMLElement. So your most likely culprit is here, since getByTestId returns an HTMLElement:

expect(getByTestId('dateRange')).toBe(dateRange);

I fixed my issue by testing against the element's text content instead.

expect(getByTestId('dateRange')).toHaveTextContent('...');

like image 171
Eric Avatar answered Oct 25 '22 06:10

Eric