Git Product home page Git Product logo

jest-property-matchers's Introduction

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),
  },
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.