React default props are eagerly evaluated in tests.
See original GitHub issueDo you want to request a feature or report a bug?
A bug
What is the current behavior?
As soon as a React component is imported from a test, its defaultProps
evaluate, even without rendering the component.
See this repo demonstrating the behavior.
What is the expected behavior?
Default props would not be evaluated unless the component is rendered. Otherwise, it’s a leaky abstraction. In this case, two unrelated components use moment
differently, which I’m mocking in a test. I shouldn’t need to know all the ways moment
gets used, just the way it’s used in the component(s) I’m rendering.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest - 22.4.2 node - 8.4.0 npm - 5.6.0 OS - OS X 10.12.6
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
The
defaultProps
object is defined in the top level scope of the module, so it’s evaluated when the module is imported. This isn’t specific todefaultProps
, any object declared in the top level of a module will be evaluated when the module is imported – this is how the module’s scope is formedConsider this example:
If you have more questions, I’m happy to help on our discord channel or StackOverflow where there is an active jestjs tag for questions
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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.