-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocking React components causes warnings with React 16 #5258
Comments
jest.mock('./Widget', () => () => 'Widget'); It's a mock factory, so you have to pass a function which returns whatever you want to return. A string is not a valid React Element. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Happy to reopen if you can show it's a bug in jest |
Thanks for the response. I appreciate that this isn't a help channel, but this feels like more like a regression to me (with the caveat that googling turns up no-one having the same problem, which is suspicious). I've updated the repo to add snapshots on both branches. If I change the
However the Jest mock documentation shows |
The docs seem wrong, then, good catch. It's not a regression in Jest if the docs are correct for React 15 though - something in React changed. But we should either make it work again, or fix the docs. (Seems like a useful feature, so I lean towards fixing it for React 16 if there's anything we can do on our side) @gaearon Is this a known thing/on purpose? |
Hmm. I guess this wouldn’t emit warnings: jest.mock('./Widget', () => 'my-widget'); because React would treat it as a custom element. Is that too weird? |
...yes? 😅 |
Well.. it's kind of how the DOM works. You wouldn't get these messages if you used the test renderer. |
Here's a few options:
This way you won't see the props, but it's definitely the most straightforward one: jest.mock('./Widget', () => () => 'Widget');
DOM "custom elements" aren't checked for anything and shouldn't fire warnings. They are lowercase and have a dash in the name. jest.mock('./Widget', () => 'mock-widget');
Test renderer doesn't care about the element types and will happily accept |
Aha - so if I only use Separately, do we want an update to the Jest docs? I could trying and write something around this. |
Yes please! |
Closes jestjs#5258. Update the documentation to describe the warnings that are emitted by React 16 when using Enzyme, mocked components and snapshot testing.
Closes jestjs#5258. Update the documentation to describe the warnings that are emitted by React 16 when using Enzyme, mocked components and snapshot testing.
Closes jestjs#5258. Update the documentation to describe the warnings that are emitted by React 16 when using Enzyme, mocked components and snapshot testing.
* Document caveat with mocks, Enzyme, snapshots and React 16. Closes #5258. Update the documentation to describe the warnings that are emitted by React 16 when using Enzyme, mocked components and snapshot testing. * Update CHANGELOG.md * Describe further options to work around warnings
In React 16. This is actually the only feasible way when doing a full mount using https://github.com/Root-App/react-native-mock-render refs jestjs#5258
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I am reporting a bug, as reproduced in pugnascotia/test-jest-mock.
With React 16, when I mocked a component like so:
I routinely get warnings such as:
The latter is less common, but the former happens a lot in my main application. I discovered it while migrating to React 16. The tests do pass and the snapshots seem OK, but the swathe of warnings is rather alarming.
In pugnascotia/test-jest-mock, this behaviour is seem both on the
master
branch, which uses Jest 20, and thejest22
branch, which uses Jest 22.Needless to say, I didn't expect to see these warnings, because I never saw them using Jest and React 15.
My versions are:
The text was updated successfully, but these errors were encountered: