I have a react component, which has two children, like this:
import {Child1} from './child1';
import {Child2} from './child2';
...
return (
<>
<Child1 />
<Child2 />
</>
)
I'm using react testing-library
and the app as created with create react app
and not ejected.
I'd like to mock them in my unit test, since they have their own tests, so I'm trying to:
jest.mock('./child1', () => 'some mocked string');
jest.mock('./child1', () => 'some mocked string');
But when I render it using import { render } from '@testing-library/react';
I see the following Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined
.
Why is that and how do I mock those components?
describe
. It works now. Any explanation on why it fails (meaning it doesn't mock and renders the real component) if I bringjest.mock(...)
inside thedescribe
block? – Frear