ShallowWrapper::state() can only be called on the root
- Dominant language
- JavaScript
- Stars
- 19.8k
- Forks
- 2k
- PR merge metrics
- No merged PRs in 30d
Description
when i run my test i am getting this error,
● Login Component › should initiate state
ShallowWrapper::state() can only be called on the root
36 |
37 | it("should initiate state", () => {
> 38 | expect(wrapper.state()).toEqual({
| ^
39 | email: "",
40 | password: "",
41 | validEmail: true,
at ShallowWrapper.state (node_modules/enzyme/src/ShallowWrapper.js:1174:13)
at Object.state (__tests__/login.test.js:38:20)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 1 passed, 1 total
Time: 12.148s
Ran all test suites.
Watch Usage: Press w to show more.
here i am attaching my code below
```jsx
const setUp = (initialState = {}) => {
const store = testStore(initialState);
const wrapper = shallow(
).childAt(0).dive();
return wrapper;
};
describe("Login Component", () => {
let wrapper;
beforeEach(() => {
const initialState = {
auth: {
user: {}
}
};
wrapper = setUp(initialState);
});
it("Login page snapshot test", () => {
const tree = renderer.create(wrapper).toJSON();
expect(wrapper).toMatchSnapshot();
});
it("should initiate state", () => {
expect(wrapper.state()).toEqual({
email: "",
password: "",
validEmail: true,
submitState: false,
loginActionState: false,
secureTextEntry: true,
errors: {}
});
});
```
Contributor guide
Research direction
Reproduce the failure from __tests__/login.test.js and inspect ShallowWrapper.state at node_modules/enzyme/src/ShallowWrapper.js:1174. Determine whether the reported behavior is an Enzyme bug or a test-wrapper usage issue; done means the expected behavior and any needed regression coverage are established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100