Socket
Book a DemoInstallSign in
Socket

deep-assert

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deep-assert

Better deep-equals object expectations, supporting dynamic bottom-up assertions using any() and satisfies().

Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
169
259.57%
Maintainers
1
Weekly downloads
 
Created
Source

deep-assert

Build Status npm

Providing a better deep-equals assertion experience.

  • Easily write object and array expectations, with any() and satisfies()
  • Create your own custom assertions
  • Short, but precise diffs, even for large nested objects
  • Works with objects, arrays, dates, buffers, etc
  • Zero dependencies

Terminal

Installation

npm install deep-assert

Usage

Basic

Let's say we want to check if an array of user objects matches our expectation, but we don't know what the id is gonna be, since it's a random ID. It's easy, using any().

import * as assert from "assert-deep"

assert.deepEquals(getUsers(), [
  {
    id: assert.any(),
    name: "John Smith",
    active: true
  },
  {
    id: assert.any(),
    name: "Jane Smith",
    active: false
  }
])

Custom assertions

Let's try the previous use case again, but this time we check that the id is a valid UUIDv4. We use the satisfies() helper function to create a custom assertion to be used within the object expectation.

import * as assert from "assert-deep"

const uuidRegex = /^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/
const assertUUID = assert.satisfies(value => typeof value === "string" && value.match(uuidRegex))

assert.deepEquals(getUsers(), [
  {
    id: assertUUID(),
    name: "John Smith",
    active: true
  },
  {
    id: assertUUID(),
    name: "Jane Smith",
    active: false
  }
])

Spreading any()

Normally deepEquals() will fail if there are unexpected properties on the tested object. We can use any() with the object spread operator to allow additional properties to be present.

deepEquals() will then only check the expected properties and ignore all other ones.

import * as assert from "assert-deep"

assert.deepEquals(getUsers()[0], {
  name: "John Smith",
  active: true,
  ...assert.any()
})

Recursive objects

import * as assert from "assert-deep"

const a = { foo: {} }
a.foo.parent = a.foo

assert.deepEquals(a, {
  foo: assert.satisfies(foo => assert.deepEquals(foo, { parent: foo }))
})

License

MIT

Keywords

assert

FAQs

Package last updated on 29 Apr 2019

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