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

jest-property-matchers

v0.1.2

Published

Simplifies creation of your jest property matchers

Downloads

361

Readme

jest-property-matchers

Easily create property matchers for use with jest's snapshots.

If you are using jest's snapshots for testing, you'll most likely run into problem that not all data you are using to compare against snapshots could actually be generated in a deterministic way (so that they stay always the same, each time you run the tests). Typically it's auto-generated ids or dates that causes these issues. Jest addresses this with property matchers but sometimes it can be quite tedious to write.

jest-property-matchers package can help you with constructing matcher. Especially in cases where the matchers are not really consised or if there are too many properties to match.

Typescript declarations are included.

Install

npm install jest-property-matchers --save-dev

Example usage

See how to use jest-property-matchers in this example of a jest test:

import generateMatcher from 'jest-property-matchers'

describe('User', () => {

  test('Should create a new user in database', async () => {

    const user = await db.createUser({
      name: 'John',
    })

    // Generate your property matchers here
    const matcher = generateMatcher({
      $number: 'id', // created user will have auto-generated id
      $dateTime: ['createdAt', 'updatedAt'], // also timestamps will be always different
    })

    expect(user).toMatchSnapshot(matcher)
  })

})

Common JS usage

To use this library in environment not supporting ES import, you can require it like this:

const generateMatcher = require('jest-property-matchers').default

API

generateMatcher(matchersDefs)

Generates and returns property matcher object according to specification provided in matcherDefs argument.

matchersDefs - Object containing one or more of specialized keys that defines types of the property matcher to be used. Values of these keys are property names in the object being matched against jest snapshot or array of property names if given matcher should be applied on more thatn one property.

These are the currently supported keys:

  • $number - matches any js Number
  • $numericString - matches any numeric string (no minus sign allowed), useful for BIGINT database values that needs to be represented as strings in JS
  • $dateTime - matches js Date
  • $date - matches js Date (same as $dateTime)
  • $stringDateTime - matches ISO 8601 string representation of a dateTime (such as "2019-12-08T12:24:22.591Z")
  • $stringDate - matches date in a "YYYY-MM-DD" format
  • $string - any string

Any other properties passed inside matchersDefs argument will be propagated intact to the returned matcher so you can pass any additional matchers that are not supported by jest-property-matchers

Definitions can also be nested. See examples bellow:

simple matcher example

generateMatcher({
    $number: 'id',
    $dateTime: ['createdAt', 'updatedAt'],
})

// above call is equivalent to this jest matcher:
{
  id: expect.any(Number),
  createdAt: expect.any(Date),
  updatedAt: expect.any(Date),
}

more complex example

Matchers definitions can also be nested in sub-objects. With more complex matchers it also starts to show how much simpler it is to use jest-property-matchers to generate property matcher.

generateMatcher({
    $numericString: ['id', 'orderId'],
    $stringDateTime: ['createdAt', 'updatedAt'],
    address: {
        $number: 'id',
    }    
})

// above call is equivalent to this jest matcher:
{
  id: expect.stringMatching(/[0-9]+/u),
  orderId: expect.stringMatching(/[0-9]+/u),
  createdAt: expect.stringMatching(/^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/u),
  updatedAt: expect.stringMatching(/^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/u),
  address: {
      id: expect.any(Number),
  },
}