Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-matcher-structure

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-matcher-structure

A custom jest matcher for validating object structures

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

jest-matcher-structure

jest-matcher-structure Build Status v0.1.1

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

Keywords

FAQs

Package last updated on 16 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc