Clojure(Script) library for snapshot testing
{:deps {juxt/snap {:git/url "https://github.com/juxt/snap" :sha "..."}}}
(deftest react-test
(snap.core/match-snapshot ::Root (js->clj (RootComponent.toJSON))))
The first time the test is run it creates a snapshot of the test value. After a snapshot is created every successive run compares the supplied value against the snapshot. If a test failure is found you can either:
-
Fix the code that created the unintended change
-
Ensure this is an intended change, delete the old snapshot and let
snap
create a new one
Make sure you commit the snapshots to source code!
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
A typical snapshot test case for a mobile app renders a UI component, takes a screenshot, then compares it to a reference image stored alongside the test. The test will fail if the two images do not match: either the change is unexpected, or the screenshot needs to be updated to the new version of the UI component.
This library is inspired by Jest snapshot testing The idea seems so good to me that it can be applied to other cases beyond UI tests.