adriantoine / enzyme-to-json

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

Allow depth option when serializing mounted wrappers #169

Open iamogbz opened 4 years ago

iamogbz commented 4 years ago

Description

Sometimes you have to mount a component for it too fully render e.g. in the case of functional components. However when snapshotting the rendered enzyme wrapper, you do not need the entire tree.

Currently there are two options deep and shallow. Deep does the entire tree which most times is not what you want to snapshot. Shallow only serializes the initial component instance.

Adding the ability to specify depth would be great for cases where you need to only snapshot to a certain level of granularity.

Depth Description
0 Equivalent to mode: shallow
1 Equivalent to shallow render
..
Infinity Equivalent to mode: deep

Note

Workarounds do exist e.g.

const serialized = toJson(wrapper.find(Base).children(), { mode: 'shallow' })
expect(serialized).toMatchSnapshot()