chai-like
A JSON matcher for chai
Last updated 2 years ago by zation .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install chai-like 
SYNC missed versions from official npm registry.

Build status Coverage Status npm/chai-like version license

chai-like

A JSON matcher for chai. This is really useful when you are testing API and want to ignore some attributes like: updatedAt, createdAt, id.

Install

Install with npm

npm install --save-dev chai-like

Assertions

like(value)

Compare two JSON and ignore some keys based on expectation.

var object = {
  id: 1,
  name: 'test',
  updatedAt: 'now'
};
object.should.like({
  name: 'test'
});
object.should.not.like({
  name: 'test1'
});

Deeply compare.

var object = {
  id: 1,
  name: 'test',
  product: {
    id: 1,
    name: 'product'
  },
  updatedAt: 'now'
};
object.should.like({
  name: 'test',
  product: {
    name: 'product'
  }
});
object.should.not.like({
  name: 'test',
  product: {
    name: 'product1'
  }
});

Compare array.

var array = [{
  id: 1,
  name: 'test',
  product: {
    id: 1,
    name: 'product'
  },
  updatedAt: 'now'
}];
array.should.like([{
  name: 'test',
  product: {
    name: 'product'
  }
}]);
array.should.not.like([{
  name: 'test',
  product: {
    name: 'product1'
  }
}]);

Compare JSON with an array sub node.

var object = {
  id: 1,
  name: 'test',
  products: [{
    id: 1,
    name: 'product'
  }],
  updatedAt: 'now'
};
object.should.like({
  name: 'test',
  products: [{
    name: 'product'
  }]
});
object.should.not.like({
  name: 'test',
  products: [{
    name: 'product1'
  }]
});

Plugins

You can extend chai-like with plugins as below format:

var chai = require('chai');
var like = require('chai-like');

var numberStringPlugin = {
  match: function(object) {
    return !isNaN(Number(object));
  },
  assert: function(object, expected) {
    return object === Number(expected);
  }
};
like.extend(numberStringPlugin);

chai.use(like);

Then we can assert as below:

  var object = {
    number: 123
  };
  object.should.like({
    number: '123'
  });
  object.should.not.like({
    number: 'not a number'
  });

Plugin for testing strings with RegExp

If some strings require fuzzy matching we can do this with a plugin as follows:

var chai = require('chai');
var like = require('chai-like');

var regexPlugin = like.extend({
  match: function(object, expected) {
    return typeof object === 'string' && expected instanceof RegExp;
  },
  assert: function(object, expected) {
    return expected.test(object);
  }
});

like.extend(regexPlugin);

chai.use(like);

Then we can assert as below:

var object = {
  text: 'the quick brown fox jumps over the lazy dog'
};
object.should.like({
  text: /.* jumps over .*/
});
object.should.not.like({
  text: /\d/
});

Current Tags

  • 1.1.1                                ...           latest (2 years ago)

19 Versions

  • 1.1.1                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.2.14                                ...           3 years ago
  • 0.2.13                                ...           3 years ago
  • 0.2.12                                ...           3 years ago
  • 0.2.11                                ...           3 years ago
  • 0.2.10                                ...           3 years ago
  • 0.1.10                                ...           4 years ago
  • 0.1.9                                ...           5 years ago
  • 0.1.8                                ...           5 years ago
  • 0.1.7                                ...           5 years ago
  • 0.1.6                                ...           5 years ago
  • 0.0.6                                ...           5 years ago
  • 0.0.5                                ...           5 years ago
  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 23
Dependencies (0)
None
Dev Dependencies (4)

Copyright 2014 - 2016 © taobao.org |