npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

png-test-snapshotter

v2.0.0

Published

A test utility for creating and diffing PNG snapshots

Downloads

44

Readme

png-test-snapshotter

A test utility for creating and diffing PNG snapshots. The snapshotter will create a visual representation of any differences between expected result and actual input.

Install

This utility is available as a package on the public NPM registry. Install it using your package manager of choice (npm, pnpm, bun, etc.). Example using npm:

npm install --save-dev png-test-snapshotter

Example usage

import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import createPNGSnapshotter from "png-test-snapshotter";

import generateImage from "./generate-image.js";

let assertSnapshot;
before(async () => {
    assertSnapshot = await createPNGSnapshotter(new URL(import.meta.url));
});

after(() => {
    assertSnapshot.assertNoUnmatchedSnapshots();
});

test("image generation works as expected", async (t) => {
    const image = generateImage(...);
    await assertSnapshot(
        t.name,
        image,
    );
});

API

Main entrypoint: createPNGSnapshotter(parentURL: URL, options)

This is the module's default export. It expects the first argument passed in to be the URL of the test suite that will be using the snapshotter. This will in most cases be new URL(import.meta.url).

An optional second argument can be provided, which when provided can contain the following fields:

  • snapshotsLocation: URL: The location of the snapshots to evaluate. Defaults to a directory named "__snapshots__" with the same parent as the test suite (parentURL).
  • diffsLocation: URL: The desired output location for any visual difference representation generated as the result of a failed match. Defaults to the OS' temporary directory.
  • failOnUnmatchedSnapshots: boolean: Whether to raise an exception during cleanup (see below) if there are any unmatched snapshots in snapshotDirname. Defaults to false if node was invoked with the --test-only command-line option, true otherwise.
  • updateSnapshots: boolean: When true the snapshotter will skip the matching process and instead write the input to the corresponding snapshot file. Defaults to true if node was invoked with the --test-update-snapshots command-line option, false otherwise.

The return value is the snapshotter function which will be used to create snapshots and assert no differences in future runs:

snapshotter(testName: string, png: Buffer)

The first argument should be set to the test name. The second argument is a Buffer containing the PNG. This function will raise an exception if the provided PNG differs from an existing snapshot. The assertion message will contain the location of the diff which can be viewed for a visual representation of the difference.

The snapshotter also has an opt-in cleanup step that can be run at the end of a test suite to ensure there are no unmatched snapshots remaining in the snapshot directory:

snapshotter.assertNoUnmatchedSnapshots()

This will do nothing if failOnUnmatchedSnapshots is false. This cleanup step is implicitly invoked if you use explicit resource management to create the snapshotter:

import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import createPNGSnapshotter from "png-test-snapshotter";

import generateImage from "./generate-image.js";

test("image generation works as expected", async (t) => {
    using assertSnapshot = await createPNGSnapshotter(new URL(import.meta.url));
    const image = generateImage(...);
    await assertSnapshot(
        t.name,
        image,
    );
    // assertSnapshot.assertNoUnmatchedSnapshots() will be invoked automatically when this block is exited
});

This could help you write terser code if you only have a single test that needs to use the snapshotter. If you want to use this syntax but need to create multiple instances of the snapshotter you will probably want to opt out of the cleanup step by setting failOnUnmatchedSnapshots to false.

Please see the accompanying TypeScript declaration file for more exact type information.