Seeing(when using useDispatch
hook):
FAIL tests/index.test.js: Invariant Violation: Could not find react-redux context value; please ensure the component is wrapped in a <Provider>
17 | const Index = () => {
> 18 | const dispatch = useDispatch();
Steps to reproduce the behavior, please provide code snippets or a repository:
const dispatch = useDispatch();
in page/index.js
yarn test
The test runs without code error.
It turns out I needed to wrap my component in a Provider
just like the error message stated.
Here is an example:
import React from 'react'
import { render } from 'react-testing-library'
import App from '../pages/index.js'
import { Provider } from 'react-redux'
import configureStore from 'redux-mock-store'
describe('With React Testing Library', () => {
const initialState = {output:10}
const mockStore = configureStore()
let store,wrapper
it('Shows "Hello world!"', () => {
store = mockStore(initialState)
const { getByText } = render(<Provider store={store}><App /></Provider>)
expect(getByText('Hello Worldd!')).not.toBeNull()
})
})
Most helpful comment
It turns out I needed to wrap my component in a
Provider
just like the error message stated.Here is an example: