Mockable React render function #912
stalniy
started this conversation in
Contribution RFC
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
This document aims to improve testability of React components by introducing additional prop which represents all used child components. This makes it possible to mock those chlid components without using
jest.mock
and relying on module pathsMotivation
jest.mock
is kind of hack that allows to mock nodejs modules in commonjs written code. There are few issues with this approach:jest.mock
call if developer forgets to remove it. There is no tool that will help us to identify whether that component is used in render function or notjest.mock
cannot be called in test and instead we need to create shared spy and modify/reset it in every testExample
jest.mock
approachTestable component
In order for component's render function to become mockable, we need to introduce additional prop on it which will allow us to provide custom child component implementations in unit tests:
And its test
Main benefits
jest.mock
Drawbacks
Beta Was this translation helpful? Give feedback.
All reactions