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-matcher-structure

v0.1.1

Published

A custom jest matcher for validating object structures

Downloads

32

Readme

A custom jest matcher for validating object structures

InstallUsageList of ComparisonsTODOCreated ByLicense

Install

$ npm install --save-dev jest-matcher-structure

Usage

jest-matcher-structure walks key-by-key through a structure object and a comparison object and verifies that the value in the structure object accurately describes the value in the comparison object.

It's easy to get started! Here's a quick and simple example:

import { toMatchStructure } from 'jest-matcher-structure'

expect.extend({ toMatchStructure })

test("structure matches object's structure", () => {
  const structure = {
    string: 'string',
    number: 'number',
    boolean: 'boolean',
    function: n => n > 5,
    literal: 'literal value',
    nested: {
      string: 'string',
      number: 'number',
      function: n => x < 5,
    },
    arrayLiteral: ['hello', 'hi', 1, 2, 3, true, false],
    regex: /\d+/,
    null: null,
  }

  const object = {
    string: 'hello world',
    number: 123,
    boolean: true,
    function: 10,
    literal: 'literal value',
    nested: {
      string: 'string',
      number: 1,
      function: 3,
    },
    arrayLiteral: [false, true, 3, 2, 1, 'hi', 'hello'],
    regex: '1234',
    null: null,
  }

  expect(structure).toMatchStructure(object)
})

Helper Functions

jest-matcher-structure comes with the helper functions some, every, and repeat.

some and every are for fields where you need to test the comparison value against multiple truths. You pass them an array and jest-matcher-structure works its magic! Here's an example:

import { some, every, toMatchStructure } from 'jest-matcher-structure'

test('some and every', () => {
  const structure = {
    field1: some(['A', 'B']),
    field2: every(['string', x => x >= 1200 && x <= 1500]),
  }

  const object = {
    field1: 'A',
    field2: '1300',
  }

  expect(structure).toMatchStructure(object)
})

repeat is for consuming repeating comparisons in an array. For example, if you need to make sure an array is filled with strings, you can do the following:

import { repeat, toMatchStructure } from 'jest-matcher-structure'

test('repeat', () => {
  const structure = {
    field1: [repeat('string')],
  }

  const object = {
    field1: ['hello', 'world', 'fubar'],
  }

  expect(structure).toMatchStructure(object)
})

List of Comparisons

  • literal value
  • string type
  • number type
  • boolean type
  • function
  • regex
  • some
  • every
  • objects
  • arrays
    • exact comparison match
    • repeating values in an array using repeat

Upcoming Changes

  • Code refactor
  • Flesh out test cases to make sure edge cases are covered
  • Rethink some, every, repeat

Created By

License

MIT