Test::Snapshot(3pm)
π Test::Snapshot π
okay bestie, gather round, because this is the Perl module Test::Snapshot (3pm, she's a Perl one, living in the user-contributed docs) and she is so good at what she does. iykyk. β¨
πΈ NAME πΈ
Test::Snapshot β the module that lets you test against data stored in a file that literally names itself. no cap. π
π¦ SYNOPSIS π¦
use Test::Snapshot;
my $got = function_generating_data();
is_deeply_snapshot $got, 'test description'; # could also be in a subtest
# command line:
TEST_SNAPSHOT_UPDATE=1 prove -lr t
# or
TEST_SNAPSHOT_UPDATE=1 make test
# if your code means the expected data should change, then inspect with
git diff -w
like. look at her. she's doing the most with the least. π₯
π DESCRIPTION π
first things first: she is not connected with Test::Snapshots (plural!) β that one is built on a similar concept but it's for running executables. different girl, different vibe. don't mix them up fr fr. π
so what she does: she implements a function that automates the storing and updating of expected test outputs. this whole thing is based on an idea the frontend devs call "snapshot testing", which is where the module name comes from. iconic origin story tbh. β¨
these snapshots get stored in files whose names are automatically generated from:
- the test filename (
$0) - any subtests' names surrounding and including this one
- the test description, if there is one
and if that file isn't there? she treats it as though it contains an undef. the audacity, the confidence, we love it. π
π FUNCTIONS π
π
is_deeply_snapshot
exported by default, as a treat. takes two mandatory arguments:
- the
gotdata β mandatory β a scalar which might be a reference. it gets passed tois_deeplyin Test::More so it can be compared against the snapshotted data. π«Ά - a text description of this test β mandatory. she uses it for reporting results, but also to derive the filename where the
expecteddata lives. multitasking queen. π
she will return the truth value of whether the test passed this time around β see below for the automatic updating of the expected data. β
π ENVIRONMENT π
to have this module automatically update her expected data, set the environment variable TEST_SNAPSHOT_UPDATE to a true value. here's the tea though: if the got and expected data do not match, a test failure will be reported β but the expected data will be updated anyway. π she said "i'm telling on you AND fixing it."
which means it is safe to always have the variable set to a true value, as long as you are using source control (you are using source control, right? bestie. answer the question. π) and you check your diffs before committing.
ngl, always-on updating plus source control is the whole vibe. just look at the diff before you commit. do not skip the diff. π
π FILE FORMAT π
the expected data is stored in a format generated by Dumper in Data::Dumper, with these values set to true β chosen to maximise readability (and to minimise diffs, because pretty diffs are a love language) of the stored data:
SortkeysIndentTerse
three little settings, one big glow-up. β¨
π§ AUTHOR π§
Ed J, "<etj at cpan.org>" β say thank you to the icon. π«Ά
π LICENSE π
Copyright (C) Ed J
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. periodt. π
and that's the whole page, bestie!! she's perl v5.36.0, dated 2023-07-12, and she's Test::Snapshot(3pm) β now go write some snapshots and go check your git diff. mwah. πβ¨ππ¦
π¦ where this page comes from
- debian trixie: libtest-snapshot-perl 0.06-2 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu noble: libtest-snapshot-perl 0.06-2 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu resolute: libtest-snapshot-perl 0.06-2 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu stonking: libtest-snapshot-perl 0.06-2 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms