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

snappy-snaps

v1.1.0

Published

Snappy Snaps is a tiny snapshot testing tool which can be used with any test framework and is able to serialize almost anything.

Downloads

38

Readme

snappy-snaps

GitHub license build status npm version

Snappy Snaps is a tiny snapshot testing tool which can be used with any test framework and is able to serialize almost anything.

import snap from 'snappy-snaps'

const data = fetchDogs()
const expected = await snap('Dogs', data) 

deepEqual(data, expected)

Installation

This is a Node.js module available through the npm registry. Node.js 18 or higher is required.

$ npm install --save-dev snappy-snaps

Please note this package is ESM only.

Usage

The purpose of snapshot testing is to ensure that the output of a piece of code remains the same over time. The first time your test code is run the data passed to the snap() function will be stored in a file on disk. On subsequent test runs the data will be retrieved from the file, enabling you to test if your code output is the same as the previously stored value.

import { test } from 'node:test'
import assert from 'node:assert'
import snap from 'snappy-snaps'
 
const fetchDog = (id) => fetch(`/api/dogs/${id}`).then((res) => res.json())
 
test('Fetch dog data', async () => {
  const dog = await fetchDog('Rover')
  const expected = await snap('Rover', dog)

  assert.deepEqual(dog, expected)
})

Snappy snaps uses serialize-javascript to serialize and store values rather than JSON.stringify() so it supports a wider range of data types including dates, maps, sets, functions, and regular expressions.

This package does not include any tooling for writing assertions or comparing your data, for this you could use Node's own assert module, a package such as fast-deep-equal or an assertion library like Chai.

The created snapshots should be committed with your other code changes, and reviewed as part of your code review process. If you'd like to learn more, Browserstack maintain a detailed guide to snapshot testing.

Updating snapshots

You can update your snapshots by running your test command with a --updateSnapshot or -u flag, by deleting the snapshot file, specifying the UPDATE_SNAPSHOT environment variable, or setting the update option to true.

Expiring snapshots

To be reminded to update your snapshots periodically you can set a future expiry date using the expires option and providing a timestamp. Running a test after the expiry date will output a warning.

const ONE_YEAR = 1000 * 60 * 60 * 24 * 365
snap('name', data, { expires: Date.now() + ONE_YEAR })

API

snap(key, value, [options] = {})

Returns A promise that is fulfilled with the new or previously stored value.

key

A unique name to identify the snapshot.

value

A serializable value to be stored.

options

Configuration options for the snapshot.

Credits

This plugin was based on the data-snapshot package and snapshot implementation from the Vitest test framework.

License

This package is MIT licensed.