question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React default props are eagerly evaluated in tests.

See original GitHub issue

Do 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:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rickhanloniicommented, Mar 11, 2018

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 to defaultProps, 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 formed

Consider this example:

// foo.js
const someFunction = () => {
  console.log('someFunction will be called when module is imported');
  return 'bar';
};

export const anyObject = {
  foo: someFunction(),
};

// foo.test.js
import { anyObject } from './foo';

test('expected', () => {
  expect(anyObject).toEqual({ foo: 'bar' });
});

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

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test default props functions is attached to component ...
I know we can test function when we pass as props but looking for help to test default functions. Thanks, import React from...
Read more >
React v18.0 – React Blog
In our testing, we've upgraded thousands of components to React 18. What we've found is that nearly all existing components “just work” with ......
Read more >
Frontend testing standards and style guidelines - GitLab Docs
The default timeout for Jest is set in /spec/frontend/test_setup.js . ... Testing the hasMetricTypes computed prop would seem like a given here.
Read more >
How To Setup React Component In Tests - Selleo
On every test case, the component receives all props needed for it to be tested. This way of setting up the component has...
Read more >
Default Props in React/TypeScript - DEV Community ‍ ‍
So I've been diving in eagerly. But in the last week-or-so, something really threw me for a loop. To illustrate the issue, I'm...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found