A snapshot testing library for PHP

dev-master 2017-11-03 22:34 UTC

This package is auto-updated.

Last update: 2020-02-20 00:08:09 UTC


A snapshot testing library for PHP

This library intends to be:

  • Very low developer friction.
  • Test runner agnostic.
  • Lightweight.

See phpunit-just-the-snaps for a PHPUnit plugin that uses this library.

Still under development! API may change!


composer require --dev sirbrillig/just-the-snaps

Generic Usage

This is how to use JustSnaps without any particular test runner.

For this example, assume we have a function called getData() which returns an array, and one called assertTrue() which throws an exception if its expression is not true.

$actual = getData();
$asserter = \JustSnaps\buildSnapshotAsserter('./tests/__snapshots__');

The first time this code is run, a CreatedSnapshotException will be thrown and the data being tested ($actual) will be serialized to the file tests/__snapshots__/testThatFooIsBar.snap.

The second time (and all subsequent times) this code is run, it will compare the data being tested with the contents of the snapshot file and return true or false depending on if it is the same.

This will protect against any regressions in getData().

If the results of getData() change intentionally, then the test can be "reset" by simply deleting the snapshot file. The next time the test is run, it will re-create it as above.


JustSnaps will run json_encode() on any data before writing it to the snapshot file. If your data needs some manipulation before being written, you can create custom serializers.

Each serializer consists of two objects:

  1. A class implementing SerializerTester which has one function: shouldSerialize(mixed $data): bool. This function must return true if the serializer should modify the data.
  2. A class implementing SerializerPrinter which has one function: serializeData(mixed $data): mixed. This function can manipulate the data and then must return the new data which will be written to the snapshot file. Note that the data returned from serializeData() will still be passed through json_encode() prior to writing.

Here's an example of using a custom serializer to hide sensitive information.

$actual = [ 'foo' => 'bar', 'secret' => 'thisisasecretpassword' ];
$printer = new class implements SerializerPrinter {
	public function serializeData($outputData) {
		$outputData['secret'] = 'xxx';
		return $outputData;
$tester = new class implements SerializerTester {
	public function shouldSerialize($outputData): bool {
		return is_array($outputData) && isset($outputData['secret']);
$serializer = new Serializer($tester, $printer);
$asserter = \JustSnaps\buildSnapshotAsserter('./tests/__snapshots__');