2

I am receiving the error below after running Jest. I dont have much experience using Jest so hoping someone can please provide information on why this test is failing.

Results: Jest test result:

- Snapshot
+ Received
- <span
-   className="icon icon-dismiss size-2x "
-   style={
-     Object {
-       "color": "inherit",
+ <Fragment>
+   <span
+     className="icon icon-dismiss size-2x "
+     style={
+       Object {
+         "color": "inherit",
+       }
      }
-   }
-   title=""
- />
+     title=""
+   />
+ </Fragment>

  26 |     };
  27 |     const wrapper = shallow(<Icon {...props} />);
> 28 |     expect(toJson(wrapper)).toMatchSnapshot();
     |                             ^
  29 |   });
  30 | });

Here is the test file: Test File

Component.spec.jsx

describe('Snapshot test - <Icon />', () => {
  it('renders Icon correctly in clean state', () => {
    const props = {
      icon: 'dismiss',
    };
    const wrapper = shallow(<Icon {...props} />);
    expect(toJson(wrapper)).toMatchSnapshot();
  });
  it('renders Icon correctly when colour provided', () => {
    const props = {
      icon: 'dismiss',
      color: '#000000',
    };
    const wrapper = shallow(<Icon {...props} />);
    expect(toJson(wrapper)).toMatchSnapshot();
  });

});

Here is the component file: component.js


class Icon extends React.PureComponent<IconProps> {
  render() {
    const {
      className, icon, size, colour, style, title,
    } = this.props;

    return (
      <React.Fragment>
        {icon === 'spinner' ? <LoadingSpinnerSmall /> : (
          <span
            title={title}
            className={`${css.icon} ${css[`icon-${icon}`]} ${css[`size-${size}`]} ${className}`}
            style={{ color: colour === null ? 'initial' : colour, ...style }}
          />
        )
      }
      </React.Fragment>
    );
  }
}

export default Icon;

5
  • This happens when you change your test and run the tests after the last time you ran those tests. Try deleting your previous snapshots and then running the tests. Commented Oct 29, 2019 at 13:19
  • @AtinSingh I added '</React.Fragment>' to the component file and ever since then the test has failed. Commented Oct 29, 2019 at 13:22
  • Yes that's why it failed because your component changed. See the error. It says received + <Fragment> try deleting your old snapshot and then try running the tests. Commented Oct 29, 2019 at 13:24
  • Thanks - I will try that Commented Oct 29, 2019 at 13:25
  • Do tell me if it worked or not? Commented Oct 29, 2019 at 13:26

1 Answer 1

1

Your snapshot match test result

- Snapshot
+ Received
- <span
-   className="icon icon-dismiss size-2x "
-   style={
-     Object {
-       "color": "inherit",
+ <Fragment>
+   <span
+     className="icon icon-dismiss size-2x "
+     style={
+       Object {
+         "color": "inherit",
+       }
      }
-   }
-   title=""
- />
+     title=""
+   />
+ </Fragment>

This shows your component has extra code from the previous time you ran the tests which is the <Fragment> opening tag and </Fragment> closing tag.

To solve this just delete your old snapshots and re run your tests.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.