Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 863
sawa we Avatar asked Mar 21 '17 18:03

sawa we


People also ask

Is snapshot testing worth it?

Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly. A typical snapshot test case renders a UI component, takes a snapshot, then compares it to a reference snapshot file stored alongside the test.

What is obsolete snapshot Jest?

"Obsolete" refers to snapshots or snapshot files, for which no . toMatchSnapshot() exists any more. Snapshots are organised in one file per test suite. Single snapshots in those files are stored along with the name of their test, given in jest's it() function.

How do I update snapshot Jest?

Note: Alternatively, if you have Jest installed globally, you can run jest --updateSnapshot or jest -u . This will update the snapshots to match the updates you made, and your tests will pass.

How do I view Jest snapshots?

To review your snapshots, run npm run jest-html ( yarn run jest-html ). This launches your default browser and opens the jest-html application. By default, jest-html looks for snapshots under **/*. snap,!


1 Answers

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); 
like image 180
Abdellah Alaoui Avatar answered Sep 30 '22 16:09

Abdellah Alaoui