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.

Jest snapshots change from v6 to v7

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

When generating shallow snapshots of react components with react-redux@6, if there is a connected component then its name looks like Connect(ComponentName). However, with react-redux@7, all connected component names look like Memo(ConnectFunction).

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to a CodeSandbox (https://codesandbox.io/s/new) or RN Snack (https://snack.expo.io/) example below:

If you run the tests in this sandbox you’ll see that the ConnectedButton component shows up as UNDEFINED in the snapshot. This is not the same behavior I have locally but it is also not what I would expect. I cannot determine the reason for the different behavior, maybe some dependencies from react-scripts are not up to date?

What is the expected behavior?

I would expect the component name to be something like: Memo(Connect(ComponentName)).

Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?

react@16.8.6 react-dom@16.8.6 react-redux@7.0.2 redux@4.0.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
HendrikThePendriccommented, May 13, 2019

As an alternative to the excellent suggestion by @ismay, you could also mock the connect function, like so:

jest.mock('react-redux', () => ({
    connect: jest.fn(() => component => `Connected${component.name}`),
}))

Only caveat is that the component does need to have a name.

1reaction
c0d3scommented, Nov 26, 2019

from the looks of it, this should be fixed in jest v25 https://github.com/facebook/jest/issues/9216#issuecomment-556954371

Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >
How To Write Snapshot Tests For React Components With Jest
Jest, a testing framework, will compare the snapshot to the rendered output ... This tutorial was verified with Node v14.7.0, npm v6.14.7, ...
Read more >
jest snapshot test failing to run for components using react ...
I am trying to run simple snapshot tests with jest. However after installing and utilizing React Router v6, all components using ...
Read more >
Testing with Jest: 15 Awesome Tips and Tricks - Medium
The beauty of snapshots is that it makes your code much more refactor friendly. Replaced an action creator? Changed the data shape of...
Read more >
jest-snapshot | Yarn - Package Manager
Important: This documentation covers modern versions of Yarn. For 1.x docs, see classic.yarnpkg.com. Yarn.
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