question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ricvillagranacommented, Jul 31, 2019

Same issue here, with the same versions.

0reactions
ElliDycommented, Sep 12, 2019

With making a provider around my component like this

shallowWrapper = shallow(
    <Provider store={mockStore(initialState)}>
      <ChargingSessionsScreen navigation={navigationProp} screenProps={{translate: jest.fn()}}/>
    </Provider>
  ); 

I am able to get my state with

shallowWrapper.state().store.getState()

Why you need to make childAt(0) behind it?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found