adriantoine / enzyme-to-json

Snapshot test your Enzyme wrappers
MIT License
947 stars 64 forks source link

When `jest.fn()` inside a enzyme ReactWrapper, snapshots get really large #107

Open swashata opened 6 years ago

swashata commented 6 years ago

Yes I know that we should not snapshot test against something dynamic as jest.fn() and I swear 😇it was unintentional.

But I have found that when jest.fn() is passed as a prop to some component, the snapshot fails everytime (which is expected) but along with this, it generates some really really large snapshot files.

An example for reproducing this can be found here https://github.com/swashata/enzyme-to-json-bug

You can check the file https://github.com/swashata/enzyme-to-json-bug/blob/master/__snapshots__/Test.spec.jsx.snap which is like 7.6MB.

I guess this can be a enzyme-to-json thing?

JemarJones commented 6 years ago

I've noticed the same thing. It seems to include the entire mock function in the snapshot, thus requiring that the mock function be included in final code of whatever component you have. I don't really want to have that dependency in my tests, so i was thinking maybe i could use the https://jestjs.io/docs/en/snapshot-testing#property-matchers of jest to simply say that i expect.any(Function) in the place of that. But unfortunately i'm not seeing how property matchers can be used easily with the output of this library. Is there maybe a helper matcher function that exists/could be created?

JemarJones commented 6 years ago

Kind of dumb, but my temporary solution (note, only if you dont care whether the mockfunction specifically becomes a prop of your component) is just to do the following

const passThroughCallback = (callback) => (...args) => callback(...args);
...
const tree = shallow(
  <ComponentImTesting
    propFunc={passThroughCallback(mockCallback)}
  />
 );

Now the snapshot will just have someThing={[Function]} instead of including the entire MockFunction wherever the propFunc is passed.

Exomnius commented 6 years ago

Any update on this issue? Or if anyone has a way of implementing a fix, we can create a PR.