shallow wrapper instance is null
- Dominant language
- JavaScript
- Stars
- 19.8k
- Forks
- 2k
- PR merge metrics
- No merged PRs in 30d
Description
```jsx
import React from 'react';
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
import { shallow } from 'enzyme';
import renderer from "react-test-renderer";
import Logout from './Logout';
import mockStore from "../../../../test/mockStoreHelper";
describe('', () => {
const defaultProps = {
actions: { signOut: jest.fn() },
history: { push: jest.fn() },
};
const renderShallow = (props = {}) =>
shallow((
));
it('renders', () => {
const jsonTree = renderer
.create(renderShallow())
.toJSON();
expect(jsonTree).toMatchSnapshot();
});
it('checks redirectLogin', () => {
const wrapper = renderShallow();
const instance = wrapper.instance();
instance.redirectLogin();
expect(defaultProps.actions.signOut).toBeCalled();
expect(defaultProps.history.push).toBeCalledWith('/login');
});
});
```
### Current behavior
I'm getting this error - TypeError: Cannot read property 'redirectLogin' of null
Instance is always null
### Your environment
Windows
#### API
- [ ] shallow
#### Version
| library | version
| ------------------------------| -------
| enzyme | 3.9.0
| enzyme-adapter-react-16 | 1.12.1
| react | 16.8.4
| react-dom | 16.8.4
| react-test-renderer | 16.8.6
#### Adapter
- [ ] enzyme-adapter-react-16
Contributor guide
Research direction
Run the shown Logout test and inspect ./Logout together with the renderShallow helper and test/mockStoreHelper. Check how the shallow wrapper is created around Provider and BrowserRouter, then verify the expected redirectLogin behavior and assertions after identifying the cause of the null instance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100