kentcdodds / jest-glamor-react

Jest utilities for Glamor and React
https://npm.im/jest-glamor-react
MIT License
98 stars 24 forks source link

Snapshot testing of DOM element clears out that elements children #39

Open pbomb opened 6 years ago

pbomb commented 6 years ago

https://github.com/pbomb/null-after-snaphot

What you did:

Wrote simple component and test using react-testing-library. Asserting snapshot using:

expect(container.firstChild).toMatchSnapshot();

What happened:

After this line executes, for some reason container.firstChild becomes null. It's not null before the snapshot is compared. This causes errors to be output when running cleanup from react-testing-library after each test because React complains that the DOM node we're trying to unmount doesn't seem to have the expected children.

Reproduction repository:

https://github.com/pbomb/null-after-snaphot

Problem description:

It seems that something within jest / jsdom / jest-glamor-react is setting the DOM node to null somehow.

Suggested solution:

Not really sure, unfortunately what the root cause is. I don't really even understand how the container object's firstChild property is getting set to null.

kentcdodds commented 6 years ago

I thought I'd fixed this but maybe not. I'm not sure what's causing it but it seems like cloning the element fixes the issue. Could you try that and if it works we can do that internally.

pbomb commented 6 years ago

How do you propose cloning the element? And I'd have to do that from within jest-glamor-react, right?

Additionally, we've already planned to migrate from Glamorous to Emotion, so I might end up taking that on instead of continuing down the Glamorous path as this has taken quite some time already.

kentcdodds commented 6 years ago

Try this:

expect(container.firstChild.cloneNode()).toMatchSnapshot();
kentcdodds commented 6 years ago

If that works, then we can do that for you from within jest-glamor-react.

pbomb commented 6 years ago

I no longer get the error output, but the cloned node doesn't get the children - it just becomes an empty DOM element.

kentcdodds commented 6 years ago

Hmmmm.... Well, maybe you'll be better off migration to emotion. I'm not sure I'll have time to look into this 😔