🪟 🔧 Ignore classnames during jest snapshot comparison #17773
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
This changes the jest tests to ignore class names during snapshot comparison.
Previously when using jest's
toMatchSnapshot()
method on a@testing-library/react
output, we'd compare the whole DOM tree, which included CSS classnames which where minified from SASS modules. Changing CSS could lead to classnames in completely unrelated files getting a new hash and thus those file's tests would fail on completely unrelated PRs. See https://github.com/airbytehq/airbyte/pull/17582/files for an example whereFrequentlyUsedDestinations
andStartWithDestination
tests failed despite those component had no changes at all.How
There is unfortunately no built-in way to Jest or testing-library to ignore specific properties in snapshot testing. Thus I added a custom plugin for snapshot serialization, that will be used whenever a
RenderResult
(the result oftesting-library/react
render
method) is tried to be snapshotted.That plugin will then clone the whole DOM tree that should be snapshotted and modify each class name in it to be redacted to just the count of classnames that the element had (e.g.
<2 classnames>
).Also it's now enough to just pass in the
RenderResult
directly to theexpect
and no longer required to call theasFragment()
on it.Why the many changes in the snapshots?
As a result of no longer using
asFragment()
, but theRenderResult
directly, the wrapping elements of the snapshot are now<body><div>
instead of<DocumentFragement>
which caused all snapshots to change (besides the change inclass
that this PR intended to do).