Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are obsolete snapshots and snapshot files?

Tags:

jestjs

I find the Jest Snapshot Summary a bit confusing. After running tests in one of our repositories, I get the following Summary:

Snapshot Summary
 › 2 snapshots written in 1 test suite.
 › 50 obsolete snapshot files found, re-run with `-u` to remove them.
 › 3 obsolete snapshots found, re-run with `-u` to remove them.

Snapshot testing means we compare the current tests' output against the output before our changes, to catch side effects.

Hence, if I get it right, the summary means

  • 2 tests are new, no snapshots were available to compare against
  • 50 tests still provide the same output as before
  • 3 tests have been removed, but the snapshots are still around

So running with -u would

  • Update the time stamp for 50 snapshots, but not change their contents
  • Delete the files for 3 snapshots that are useless

Is that understanding correct?

like image 968
Andy Avatar asked Sep 04 '19 17:09

Andy


People also ask

What are snapshots in Jest?

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.

Why are snapshots useful?

Snapshots have lots of benefits. They allow a quick roll-back to a previous point in time and allow for much more frequent protection than backups, without affecting production systems. Meanwhile, backups are likely to run once a day and outside main production hours because of their impact on resources.

What is snapshot testing used for in react?

- [Instructor] Snapshot tests are useful for making sure that the UI will not change unexpectedly. An additional Facebook package needs to be added to the project via npm to help us do that. It's called react-test-renderer, and it allows us to render React components as pure JavaScript objects.

Is snapshot testing useful?

So, snapshots make testing much easier. You can easily track code changes and fix errors. Also, it is easier to update the components when they change.


1 Answers

It's been a while I posted this question and by know I can answer it myself:

"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. If you rename a test, the old snapshot is still in the snapshots file, but recognised as "obsolete".

› 2 snapshots written in 1 test suite.

⇒ 2 tests are new, no snapshots were available to compare against

This one holds true.

› 50 obsolete snapshot files found

50 tests still provide the same output as before

Is wrong, the 50 corresponding test suites have been renamed, moved or removed. Such a high number is unusual and you should probably find a way to re-map the snapshots to their tests, before updating them.

› 3 obsolete snapshots found

⇒ 3 tests have been removed, but the snapshots are still around

So this is only partly right, since the tests might have been renamed, not removed.

like image 102
Andy Avatar answered Sep 20 '22 17:09

Andy