enzyme: shallow().dive() does not work with react-redux Provider
it('should work', () => {
const Component = () => {
return <div>Hello World</div>;
};
const wrapper = shallow(
<Provider store={mockStore()}>
<Component />
</Provider>
).dive();
expect(wrapper).toMatchSnapshot();
});
This is just a simple example - please assume that Component
needs to have access to the react-redux context so I can use useSelector()
in there, so something like .find(Component).dive()
won’t work
Current behavior
exports[`<UserMenu /> hopefully works 1`] = `<Component />`;
Expected behavior
exports[`<UserMenu /> hopefully works 1`] = `
<div>
Hello World
</div>
`;
API
- shallow
Version
library | version |
---|---|
enzyme | 3.10.0 |
react | 16.8.6 |
react-dom | 16.8.6 |
react-test-renderer | - |
adapter (below) | 1.14.0 |
Adapter
- enzyme-adapter-react-16
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 49
- Comments: 59 (16 by maintainers)
Any solution for this issue, out there?
+1 here, doesn’t seem to work for me either.
throws this
Same Problem. Found a temporary solution though
https://stackoverflow.com/questions/59191129/enzyme-jest-react-testing-shallow-connected-component-with-react-redux-6
use
mount
instead ofshallow
@idangozlan that’s why i suggested using the
wrappingComponent
option:Same here. seems like enzyme and redux hooks are not getting along.
@chris-fran pass the store as prop to connect HOC.
This is very actual problem. I spent several hours to solve it and i just wrote mount() instead of shallow()
I was having issues with TypeScript and React-Redux Provider. I don’t know if this helps anyone but I was able to get a DOM snapshot via the below code.
testUtils.ts
test.tsx
Though I do receive a Type error on ‘store’ on this line
<ConnectedSplashScreen store={store}/>
Indeed, I tried it myself again and even without snapshots etc, the problem is that
wrappingComponent
doesn’t seem to work withuseContext
(which react-redux uses internally)…@joan-saum and it will still have no solution in 2119, since it’s blocked on the react team providing hooks for testing/reaching into hooks.
Closing; if
wrappingComponent
doesn’t work for anyone, please file a new issue.@ljharb Still happening (
Invariant Violation: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
).The sample code is:
Test.js:
The test:
How come this issue has still no solution? React hooks has been introduced in 2019, and it is still not possible to access the redux store with react hooks during a shallow render…
I had the same problem using dive twice solved my issues.Thats interesting.
const setup = (initialState = {}) => { const store = storeFactory(initialState); return shallow(<Input store={store} />) .dive() .dive(); };
@ajGingrich i agree; the goal is that it works with
shallow
. If you’re still having that trouble, it’d be great if you filed a new issue about it so we could try to work through it.@renjithspace
yea I have tried that but no luck with
wrappingComponentProps: { store }
and in fact I’ve given up entirely on thewrappingComponent
route, it just doesn’t work for any of my tests for different use cases period including using Router from react-router to wrap components under test. I don’t trust it…it just doesn’t work for me period in any cases I’ve tried so far.I ended up with this working for me (had to add a
childAt()
oddly enough) due to how react-redux is structured these says in v7:redux v7 re-introduced the ability to pass store as a prop directly onto React components (as I like it).
@dschinkel you’d use the
wrappingComponentProps
option to supply props to theProvider
.Using
wrappingComponent
is got worked 👍@Ba-stian don’t use mount; use the
wrappingComponent
option to pass Provider, and pass<ReportButton />
directly toshallow
.@vojtatranta how do inject it when the connected component is actually a child?
@idangozlan, shallow() doesn’t work with React’s useContext hook. If you’re using a version of react-redux higher than 5.1.1, you’re going to have a bad time dealing with contexts. See this comment for some workarounds.