jest snapshot testing: how to ignore part of the snapshot file in jest test results

sawa we picture sawa we · Mar 21, 2017 · Viewed 18.4k times · Source

Problem: ignore some part of the .snap file test results

the question here: there are some components in my test that have a random values and i don't really care about testing them. is there any way to ignore part of my X.snap file? so when i run tests in the future it won't give me test fail results.

Answer

Abdellah Alaoui picture Abdellah Alaoui · Mar 29, 2017

Actually, you need to mock the moving parts.

As stated in jest docs:

Your tests should be deterministic. That is, running the same tests multiple times on a component that has not changed should produce the same results every time. You're responsible for making sure your generated snapshots do not include platform specific or other non-deterministic data.

If it's something related to time, you could use

Date.now = jest.fn(() => 1482363367071);