Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this is undefined in bound functions when shallow rendering #1756

Closed
adityavm opened this issue Aug 16, 2018 · 16 comments
Closed

this is undefined in bound functions when shallow rendering #1756

adityavm opened this issue Aug 16, 2018 · 16 comments
Labels

Comments

@adityavm
Copy link

Describe the bug
The following test passes in 3.4.1 but fails in 3.4.2.

class Comp extends Component {
  state = {
    key: "",
  }

  componentDidMount() {
    this.instanceFunction();
  }

  instanceFunction = () => this.setState(() => ({ key: "value" }));

  render() {
    const { key } = this.state;
    return null;
  }
}

it("should work", () => {
  shallow(<Comp />);
});

The error in 3.4.2 is:

FAIL src/test.js
  ✕ should work (16ms)

  ● should work

    Method “setState” is only meant to be run on a single node. undefined found instead.

      11 |   }
      12 |
    > 13 |   instanceFunction = () => this.setState(() => ({ key: "value" }));
         |                                 ^
      14 |
      15 |   render() {
      16 |     const { key } = this.state;

      at ShallowWrapper.single (node_modules/enzyme/build/ShallowWrapper.js:1718:17)
      at ShallowWrapper.setState (node_modules/enzyme/build/ShallowWrapper.js:499:14)
      at Comp.ShallowWrapper.instance.setState (node_modules/enzyme/build/ShallowWrapper.js:184:33)
      at Comp.setState [as instanceFunction] (src/test.js:13:33)
      at Comp.instanceFunction [as componentDidMount] (src/test.js:10:10)
      at node_modules/enzyme/build/ShallowWrapper.js:189:20
      at Object.batchedUpdates (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:392:22)
      at new ShallowWrapper (node_modules/enzyme/build/ShallowWrapper.js:188:24)
      at shallow (node_modules/enzyme/build/shallow.js:21:10)
      at Object.<anonymous> (src/test.js:22:3)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.147s
Ran all test suites matching /src\/test.js/i.
error Command failed with exit code 1.

To Reproduce
Steps to reproduce the behavior:

  1. Run the above test with [email protected]

Expected behavior
The test should pass.

Desktop (please complete the following information):

  • OS: Mac OSX (but also fails in Windows)
  • Browser n/a
  • Version n/a
@adityavm adityavm changed the title this is undefined in instance functions when shallow rendering this is undefined in bound functions when shallow rendering Aug 16, 2018
@apieceofbart
Copy link

+1
Probably introduced here: a50570d#diff-fa27c82ee4fde075bae6173848e17c82

@vishalsehgal111
Copy link

+1 yes happening to us too, due to https://github.com/airbnb/enzyme/blob/a50570d847fac11dc122c43651107e46ba03de73/packages/enzyme/src/ShallowWrapper.js#L177-L180 as setState is getting appended to instance.
I think we should avoid addingsetState to instance as then also the code will get executed without appending instance.

@mmairs9-zz
Copy link

+1 got this error this morning aswell.

@rafikul-sekh
Copy link

+1 got this same issue after upgrade to 3.4.2

@ljharb ljharb added the bug label Aug 16, 2018
@purdrew
Copy link

purdrew commented Aug 16, 2018

Any ETA on fixing this or backing out the change? I have a bunch of builds that are failing and would hate to have to go through each of them and pin to a version.

@ljharb
Copy link
Member

ljharb commented Aug 16, 2018

@deedubbu i'm working on it; i hope to have a patch out tonight.

@koba04
Copy link
Contributor

koba04 commented Aug 17, 2018

Sorry for the regression. I’m going to write a patch for this.

@ljharb
Copy link
Member

ljharb commented Aug 17, 2018

@koba04 please review #1763

@koba04
Copy link
Contributor

koba04 commented Aug 17, 2018

@ljharb Thank you! I'm reviewing it.

ljharb added a commit that referenced this issue Aug 17, 2018
@koba04
Copy link
Contributor

koba04 commented Aug 17, 2018

Please upgrade to v3.4.3 🙏

@Tinusw
Copy link

Tinusw commented Aug 17, 2018

I believe this may still be an issue. In my shallow rendered component tests I'm getting:

Method “setState” is only meant to be run on a single node. undefined found instead.
 
componentDidMount(){
   this.setState({ valid: this.props.oldEmail != this.state.email })
}

Resolved by reverting to 3.4.1 for now....

Let me know if I can provide more info :)

@koba04
Copy link
Contributor

koba04 commented Aug 17, 2018

@Tinusw Please open a new issue with a test case that reproduces it.

@selbekk
Copy link

selbekk commented Aug 17, 2018

Looks like any change will trigger componentDidMount.

@monken
Copy link

monken commented Aug 17, 2018

Not sure why, but the 3.4.3 release broke my tests. I receive Error: ShallowWrapper::setState() can only be called on the root errors all over the place.

The relevant test is attached. I was able to figure out that componentDidMount is called twice with the 3.4.3 release, which was not the case before.

  it('<MyComponent />', async () => {
    fetch.mockResponse(JSON.stringify(jsonData));
    const wrapper = shallow(<MyComponent />, { disableLifecycleMethods: true });
    await wrapper.instance().componentDidMount();
    wrapper.update();
    expect(fetch.mock.calls.length).toEqual(1);
    expect(wrapper.find(Table).length).toEqual(1);
  });

@koba04
Copy link
Contributor

koba04 commented Aug 17, 2018

It's very helpful to open a new issue with a test case that reproduces it.

@selbekk
Copy link

selbekk commented Aug 17, 2018

Sorry about that, I'm on it :)

elsaywang pushed a commit to elsaywang/destination that referenced this issue Mar 5, 2019
See enzymejs/enzyme#1756 for more info.

Will restore `^` and upgrade to latest once they release a patch
to fix this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests