1.3.0 • Published 8 years ago

chai-shallow-deep-almost-equal v1.3.0

Weekly downloads
498
License
-
Repository
github
Last release
8 years ago

chai-shallow-deep-almost-equal

It's a fork of https://github.com/michelsalib/chai-shallow-deep-equal which adds .001 float treshold almost-equality feature.

Will shallowly perform a deep almost equal assertion. In other terms is consist of checking that an object, or objects graph, is contained within another one (see examples bellow).

NPM version Build Status

Usage

Browser

<script src="chai.js"></script>
<script src="chai-shallow-deep-almost-equal.js"></script>

Node

var chai = require('chai');
chai.use(require('chai-shallow-deep-almost-equal'));

Assertions

ShallowDeepAlmostEqual is available for all chai assertion styles:

var a = {x: 10, y: 10};
var b = {x: 10.001};

a.should.ShallowDeepAlmostEqual(b);
expect(a).to.ShallowDeepAlmostEqual(b);
assert.ShallowDeepAlmostEqual(a, b);

Example

assert.ShallowDeepAlmostEqual({x: 10, y: 10}, {x: 10.001}); // true
assert.ShallowDeepAlmostEqual({x: 10, y: 10}, {x: 9.999}); // true
// assert.ShallowDeepAlmostEqual({x: 10, y: 10}, {x: 9.9}); // fails

// the rest are the original shallowDeepEqualTests
assert.ShallowDeepAlmostEqual({
  name: 'Michel',
  language: 'javascript',
  tags: [
    'developer',
    'gamer'
  ]},
  {
  name: 'Michel',
  tags: [
    'developer'
  ]}); // true

assert.ShallowDeepAlmostEqual([
    {brand: 'apple', color: 'red'},
    {brand: 'samsung', color: 'blue'},
  ],
  {
  length: 2,
  0: {color: 'red'},
  1: {brand: 'samsung'},
  }); // true

assert.ShallowDeepAlmostEqual({
  name: 'Michel',
  age: undefined
  },
  {
  name: 'Michel',
  age: 37
  }); // false (age should not be defined)