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.
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.
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.
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.
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('...');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With