0

I noticed that the snapshot looks like this:

exports[`shallow 1`] = `
<div>
  <Component />
  <Component />
</div>
`;

Where means when doing snapshot the two components below are identical:

export const Page1 = () => {
    return <div>
        <Title/>  
        <Content/>    
    </div>
}

export const Page2 = () => {
    return <div>
        <Content/>  
        <Title/>    
    </div>
}

Where Title and Content are:

export const Title = ()=>{
    return <h1>Hello World</h1>
}

export const Content= () =>{
    return <p>The quick fox jumped over the brown dog</p>
}

I understand that Shallow is not meant to fully render the child components, ie the h1 tags and 'Hello World', but I thought it should at least care the order of those child components?

How do I check that a components children hasn't changed place, or been replaced by other components, without mounting the entire component tree?

EDIT: Here's the whole of my example.test.js file and the entire snapshot:

import React from "react";
import { shallow } from "enzyme";
import toJson from 'enzyme-to-json';

export const Page1 = () => {
    return <div>
        <Title/>  
        <Content/>    
    </div>
}

export const Page2 = () => {
    return <div>
        <Content/>  
        <Title/>    
    </div>
}

export const Title = ()=>{
    return <h1>Hello World</h1>
}

export const Content= () =>{
    return <p>The quick fox jumped over the brown dog</p>
}

test("Page", () => {
    const wrapper1 = shallow(<Page1/>);
    const wrapper2 = shallow(<Page2/>);
    expect(toJson(wrapper1)).toMatchSnapshot();
    expect(toJson(wrapper2)).toMatchSnapshot();
})

This is the snap it produces

// Jest Snapshot v1

exports[`Page 1`] = `
<div>
  <Component />
  <Component />
</div>
`;

exports[`Page 2`] = `
<div>
  <Component />
  <Component />
</div>
`;
davidx1
  • 3,525
  • 9
  • 38
  • 65
  • 1
    Maybe you could add the test as well, otherwise it's hard to tell why you believe the snapshot will be the same. Enzyme will always render `Title` and `Content` not sure how the snapshot could look like in your example. – Andreas Köberle Apr 04 '18 at 06:47
  • @AndreasKöberle I have included my original test file and the snapshot it generates. – davidx1 Apr 04 '18 at 12:18
  • Strange for me it renders the expected snapshot: `
    `. What version of `enzyme ` and `enzyme-to-json` you use. Minor note I use `"snapshotSerializers": ["enzyme-to-json/serializer"]` in my jest settings, instead of `toJson(wrapper1)`. Not sure if this causes the problem.
    – Andreas Köberle Apr 04 '18 at 12:23
  • @AndreasKöberle Hmm... so I have a configuration issue somewhere – davidx1 Apr 04 '18 at 12:27
  • @AndreasKöberle I have tried setting the snapshotSerializer, but still getting in the snapshot, here are my package versions "enzyme": "^3.3.0", "enzyme-adapter-react-16": "^1.1.1", "enzyme-to-json": "^3.3.3", – davidx1 Apr 04 '18 at 12:33
  • Looks like for some reason the components dont get the correct name, this normally happens when you use `export default () =>` instead of `export const ComponentName = () => ` – Andreas Köberle Apr 04 '18 at 12:33
  • 1
    Only differnence to my setup: `"enzyme-to-json": "3.3.1",` – Andreas Köberle Apr 04 '18 at 12:34
  • @AndreasKöberle It appears downgrading to v3.3.1 for enzyme-to-json fixed the problem. I wonder if the change was intentional or by accident – davidx1 Apr 04 '18 at 12:41
  • Would make sense to open an issue for this – Andreas Köberle Apr 04 '18 at 12:44
  • @AndreasKöberle Yup, doing it now. Thanks so much for your help – davidx1 Apr 04 '18 at 12:46

1 Answers1

0

This turned out to be some configuration issue. Instead of using toJson, I put the serializer in the jest config. And I downgraded the enzyme-to-json. And that solved the my problem. I then deleted my node_modules, and reinstalled everything.

Now that it's working, even if I change everything back to how it was to begin with, with enzyme-to-json at version 3.3.3, and using the toJson function, everything still works. I still don't know what the root cause was, but I'm suspecting something with how my packages were installed.

davidx1
  • 3,525
  • 9
  • 38
  • 65