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

snapshot-serializers

v1.2.0

Published

Snapshot serializers for Jest and Vitest

Downloads

418

Readme

CI npm npm

Snapshot Serializers

A collection of snapshot serializers for Jest and Vitest to help manage dynamic values in your test snapshots.

Installation

npm install snapshot-serializers

Usage

Snapshot serializers must be added by using expect.addSnapshotSerializer API:

import { replaceProperty } from 'snapshot-serializers';

expect.addSnapshotSerializer(replaceProperty({ property: 'foo' }));

Replace Properties

Replace dynamic properties like auto-generated IDs or timestamps with placeholders in your snapshots:

import { replaceProperty } from 'snapshot-serializers';

// Replace single property with default placeholder '[SNAPSHOT_PLACEHOLDER]'
expect.addSnapshotSerializer(replaceProperty({ property: 'id' }));

// Replace multiple properties with custom placeholder
expect.addSnapshotSerializer(
  replaceProperty({
    property: ['createdAt', 'updatedAt'],
    placeholder: '[TIMESTAMP]'
  })
);

// Replace properties matching a pattern
expect.addSnapshotSerializer(
  replaceProperty({
    property: /^date/,
    placeholder: '[DATE]'
  })
);

test('user data snapshot', () => {
  const user = {
    id: '123e4567-e89b-12d3-a456-426614174000',
    name: 'John Doe',
    createdAt: '2024-03-20T12:00:00Z',
    updatedAt: '2024-03-21T15:30:00Z',
    dateJoined: '2024-03-20',
    dateLastLogin: '2024-03-21'
  };
  expect(user).toMatchInlineSnapshot(`
    {
      "id": "[SNAPSHOT_PLACEHOLDER]",
      "name": "John Doe",
      "createdAt": "[TIMESTAMP]",
      "updatedAt": "[TIMESTAMP]",
      "dateJoined": "[DATE]",
      "dateLastLogin": "[DATE]"
    }
  `);
});

Remove Properties

Remove properties that shouldn't be included in snapshots:

import { removeProperty } from 'snapshot-serializers';

// Remove single property
expect.addSnapshotSerializer(removeProperty({ property: 'password' }));

// Remove multiple properties
expect.addSnapshotSerializer(
  removeProperty({ property: ['password', 'secretKey', 'token'] })
);

// Remove properties matching a pattern
expect.addSnapshotSerializer(
  removeProperty({ property: /^secret/ })
);

test('user data without sensitive info', () => {
  const user = {
    id: '123',
    email: '[email protected]',
    password: 'secret123',
    secretKey: 'abc123',
    secretToken: 'xyz789',
    token: 'jwt-token'
  };
  expect(user).toMatchInlineSnapshot(`
    {
      "id": "123",
      "email": "[email protected]"
    }
  `);
});

Type Safety

Provide a type argument to the replaceProperty and removeProperty functions to get type safety for your properties:

import { replaceProperty, removeProperty } from 'snapshot-serializers';

type User = {
  id: string;
  name: string;
  createdAt: string;
};

expect.addSnapshotSerializer(
  // type of property is "id" | "name" | "createdAt"
  replaceProperty<User>({ property: 'id' })
);

expect.addSnapshotSerializer(
  // "invalid" is not assignable to type '"id" | "name" | "createdAt"
  removeProperty<User>({ property: 'invalid' })
);

API

replaceProperty<T>(options)

Creates a serializer that replaces specific property values with placeholders.

  • options.property: The property to replace. Can be:
    • A single property name
    • An array of property names
    • A RegExp pattern to match multiple properties
  • options.placeholder: (Optional) Custom placeholder text. Defaults to [SNAPSHOT_PLACEHOLDER]

removeProperty<T>(options)

Creates a serializer that removes specific properties from the snapshot.

  • options.property: The property to remove. Can be:
    • A single property name
    • An array of property names
    • A RegExp pattern to match multiple properties

License

MIT