Enzyme: Error: ShallowWrapper::state() can only be called on the root

Created on 17 Feb 2017  路  6Comments  路  Source: enzymejs/enzyme

I am getting this error:

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': {}};
    const wrapper = shallow(<IntlProvider><SignUpForm router={router} /></IntlProvider>);
    expect(wrapper.find(SignUpForm).state('errors')).to.equal({});
});

Would anyone be able to tell me how I can access/test the state of the SignUpForm component?

shallow question

Most helpful comment

I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use dive() but there is no similar method for mounted components. This is the solution I did:

const wrapper = mount(shallow(<MyComponent />).get(0))

Works like a charm :)

All 6 comments

I think #431 might be relevant here.

You want wrapper.dive() so that it re-shallow-renders the SignUpForm.

I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use dive() but there is no similar method for mounted components. This is the solution I did:

const wrapper = mount(shallow(<MyComponent />).get(0))

Works like a charm :)

@dabit1 Thank you very much your tip worked like a charm for me too

@dabit1 When I do that I just get a bunch of errors:
Warning: Failed prop type: Invalid prop 'Component' supplied toWrapperComponent.
Error: Uncaught [TypeError: Cannot read property 'hasOwnProperty' of null][..]

Hi!
I can verify that it still works in 2019 (in contrast of the react intl team's enzyme + jest tutorial which doesn't :( )

"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-intl": "^2.9.0",
"jest": "^24.8.0",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nelsonchen90 picture nelsonchen90  路  3Comments

modemuser picture modemuser  路  3Comments

AdamYahid picture AdamYahid  路  3Comments

ivanbtrujillo picture ivanbtrujillo  路  3Comments

blainekasten picture blainekasten  路  3Comments