I found out a little more since I asked this question. Here's an alternative (better?) Way to deal with mocking components that need to be passed to the details: using mock file modules.
First, create a file with the same name as the component to fake in the __mocks__ folder in the component folder, for example.
. |- /ComponentToMock.js β- /__mocks__/ComponentToMock.js <-- create this folder/file!
Note. It seems that at the time of writing this folder, the folder should be called __mocks__ (you need to create __mocks__ in each folder in which you will have to mock the components. If the underscores upset you, just pretend that they do not exist;))
Further, in this mock file you can write the file as you want, for example
// This code would live in ./__mocks__/ComponentToMock.js import React from 'react'; const ComponentToMock = ({ testProp }) => <div>A mock with '{testProp}' passed!</div>; export default ComponentToMock;
Then, in the test file, change the jest mock statement to: jest.mock('./ComponentToMock');
When Jest encounters .mock() without the second function parameter, it automatically searches for the __mocks__ folder. Nevertheless, despite the fact that the push process is called in the component under test, this does not affect the import of the layout itself, so it can import and compile the React component!
This seems to work well for mocked components that need to pass the details to, and would otherwise give prop warnings if the nulled function were returned (but this is perfectly acceptable for continued use if the component does not receive the details). Hope this helps some people.
source share