chai-exclude
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

chai-exclude

npm npm Build Status JavaScript Style Guide

Exclude keys to compare from a deep equal operation with chaijs expect.

Why?

Sometimes you'll need to exclude object properties that generate unique values while doing a deep equal operation. This plugin makes it easier to remove those properties before comparison.

https://github.com/chaijs/chai/issues/885

Installation

npm install chai-exclude --only=dev
yarn add chai-exclude --dev

Usage

Require

const chai = require('chai');
const chaiExclude = require('chai-exclude');

chai.use(chaiExclude);

ES6 Import

import { use } from 'chai';
import chaiExclude from 'chai-exclude';

use(chaiExclude);

TypeScript

import { use } from 'chai';
import chaiExclude = require('chai-exclude');

use(chaiExclude);

// The typings for chai-exclude are included with the package itself.

Example

a) excluding

  1. Excluding a top level property from an object
expect({ a: 'a', b: 'b' }).excluding('a').to.deep.equal({ b: 'b' })
expect({ a: 'a', b: 'b' }).excluding('a').to.deep.equal({ a: 'z', b: 'b' })
  1. Excluding multiple top level properties from an object
const obj = {
  a: 'a',
  b: 'b',
  c: {
    d: 'd',
    e: 'e'
  }
}

expect(obj).excluding(['a', 'c']).to.deep.equal({ b: 'b' })
expect(obj).excluding(['a', 'c']).to.deep.equal({ a:'z', b: 'b' })

b) excludingEvery

  1. Excluding every property in a deeply nested object
const actual = {
  a: 'a',
  b: 'b',
  c: {
    a: 'a',
    b: {
      a: 'a',
      d: {
        a: 'a',
        b: 'b',
        d: null
      }
    }
  },
  d: ['a', 'c']
}

const expected = {
  a: 'z',     // a is excluded from comparison
  b: 'b',
  c: {
    b: {
      d: {
        b: 'b',
        d: null
      }
    }
  },
  d: ['a', 'c']
}

expect(actual).excludingEvery('a').to.deep.equal(expected)
  1. Excluding multiple properties in a deeply nested object
const actual = {
  a: 'a',
  b: 'b',
  c: {
    a: 'a',
    b: {
      a: 'a',
      d: {
        a: 'a',
        b: 'b',
        d: null
      }
    }
  },
  d: ['a', 'c']
}

const expected = {
  b: 'b',
  c: {
    b: {
      d: {      // d is not removed because it is an object
        b: 'b'
      }
    }
  }
}

expect(actual).excludingEvery(['a', 'd']).to.deep.equal(expected)

Note: excludingEvery will not remove the property if it is an object in a deeply nested object.

Contributing

Contributions are welcome. If you have any questions create an issue here.

License

MIT

Package Sidebar

Install

npm i chai-exclude@1.0.3

Version

1.0.3

License

MIT

Last publish

Collaborators

  • mesaugat