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

Error Testing Fragments Introduced in v3.4.0 #1754

Closed
mmk0083 opened this issue Aug 15, 2018 · 2 comments
Closed

Error Testing Fragments Introduced in v3.4.0 #1754

mmk0083 opened this issue Aug 15, 2018 · 2 comments

Comments

@mmk0083
Copy link

mmk0083 commented Aug 15, 2018

Describe the bug
Not sure if this is a bug or intended behavior/us doing something wrong. It seems that after upgrading to enzyme v3.4+ our tests for components with fragments have started failing.

To Reproduce
Consider the following example:

import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';

class MyComponent extends React.Component {
  state = { isOpen: false };

  openMe = () => { this.setState({ isOpen: true }); }

  render() {
    return [
      <button onClick={this.openMe}>Click Me</button>,
      <div>Some div</Menu>
    ];
  }
}

// test....
describe('<MyComponent />', function() {
  it('initializes the state', function() {
    const wrapper = shallow(<MyComponent />);
    expect(wrapper.state('isOpen')).to.be.false;
  });
});

Expected behavior
This test worked prior to v3.4. wrapper.state() in that instance returned false as expected. It now causes an error. Namely: "Error: Method "state" is only meant to be run on a single node. 2 found instead."

Additional context

  • react@16.4.2
  • enzyme@3.4+
  • enzyme-adapter-react-16@1.2.0
  • mocha@5.2.0
  • chai@4.1.2
@ljharb
Copy link
Member

ljharb commented Aug 15, 2018

The support that was added in enzyme v3.4 is for React.Fragment - what's also confusingly referred to as "fragments" is the ability for components to render non-nodes - ie, to render arrays, strings, and numbers. This is not yet supported by enzyme - see #1213.

We're working on adding support for that, but in the meantime, you'll either need to not use this React feature, not test components that use it, or find a way to make the tests pass anyways.

@mmk0083
Copy link
Author

mmk0083 commented Aug 16, 2018

@ljharb Thanks for the feedback. I switched to <React.Fragment> and my tests went back to passing as expected. I didn't realize that there were different flavors of 'fragment' - I thought the one was just syntactic sugar for the other. Apparently not the case.

@ljharb ljharb closed this as completed Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants