ShallowWrapper::state() can only be called on the root
See original GitHub issuewhen 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
const setUp = (initialState = {}) => {
const store = testStore(initialState);
const wrapper = shallow(
<Login navigation={{ navigate: jest.fn() }} store={store} />
).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: {}
});
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Error: ShallowWrapper::state() can only be called on the root ...
Here is my test: it('default "errors" state is an empty object', () => { const router = {'react-router': {}} ...
Read more >ShallowWrapper::setState() can only be called on class ...
Class name doesn't matter. OriginalStoryAppClass should be original class component. If you export it like it was suggested, import it like ...
Read more >instance() => ReactComponent - Enzyme - GitHub Pages
Returns the single-node wrapper's node's underlying class instance; this in its methods. NOTE: can only be called on a wrapper instance that is...
Read more >enzyme.ShallowWrapper.instance JavaScript and Node.js ...
Gets the instance of the component being rendered as the root node passed into shallow(). NOTE: can only be called on a wrapper...
Read more >update() | Enzyme - UNPKG
NOTE: can only be called on a wrapper instance that is also the root instance. Returns. ShallowWrapper : Returns itself. Example. class ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Same issue here, with the same versions.
With making a provider around my component like this
I am able to get my state with
Why you need to make childAt(0) behind it?