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-redux-7.1.0 Provider undefined in tests

See original GitHub issue

What is the current behavior? I am trying to write some tests for our custom hooks that use redux. I soon realized that any import from react-redux in tests only include the connect function. Is this expected? Using Jest as a test runner and CRA to run them.

import { Provider } from "react-redux"

console.log(Provider) // undefined

...

What is the expected behavior?

Provider to be defined.

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/react-dom: 16.8.2
react-redux: 7.1.0
redux: 4.0.1

Issue Analytics

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

github_iconTop GitHub Comments

11reactions
StKostyantincommented, Jul 21, 2020

I found the reason for the error.

jest.mock('react-redux', () => ({
  connect: () => jest.fn(),
  useSelector: jest.fn(fn => fn()),
  useDispatch: () => jest.fn()
}));

I mocked this library and this led to the fact that I could not use the Provider in tests

0reactions
markeriksoncommented, Apr 5, 2022

Hah, nice 😃 For the record we would really recommend against trying to mock the react-redux lib or any of its APIs. Instead, we recommend actually creating real Redux stores in tests and rendering a <Provider store={store}> around the component tree being tested:

https://redux.js.org/usage/writing-tests#components

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when trying to install react-redux dependency
It looks like you are using the latest npm version (v7). ... Incase you are facing this issue with a react-native dependency then...
Read more >
Continuous integration for React applications using Jest and ...
This tutorial covers: Cloning and setting up a sample React and Redux app; Writing tests for the application; Configuring continuous integration ...
Read more >
Hooks - React Redux
Hooks. React's new "hooks" APIs give function components the ability to use local component state, execute side effects, and more.
Read more >
React v17.0 Release Candidate: No New Features
In React 17, the behavior for forwardRef and memo components is consistent with regular function and class components. Returning undefined from ...
Read more >
Get rid of "Unresolved function/method/variable" warning in ...
... function/method/variable" warning in Jest test files Follow ... get 'can not install Noe.js modul: Please specify npm or yarn package'.
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