chai-match-pattern

NPM

CircleCI David Dependencies

Related Modules: lodash-match-pattern lodash-checkit

chai-match-pattern is a Swiss Army Knife for deep matching JSON objects. It is essentially a Chai wrapper for lodash-match-pattern and detailed documentation can be found there about its pattern matching functionality. This functionality includes deep matching of JSON properties by

  • Exact values
  • Regex
  • Any matcher from checkit (e.g. _.isURL, _.isEmail)
  • Any matcher from lodash (e.g. _.isObject, _.isNaN)
  • Partial matches
  • Custom matching functions
  • Filtered values
  • And more…

Basic Usage

npm install chai-match-pattern --save-dev

In your test file insert

var chai = require('chai');
var chaiMatchPattern = require('chai-match-pattern');
chai.use(chaiMatchPattern);
var _ = chaiMatchPattern.getLodashModule(); // recommend using our lodash extension

Then use patterns to check your JSON with the .matchPattern(pattern) assertion function. For example

chai.expect({a: 1, b: 'abc'}).to.matchPattern({a: 1, b: _.isString});

See lodash-match-pattern for way, way more details.

Additionally any of the included isXxxx functions can be used directly as assertion functions. For example

chai.expect(7.5).isBetween(7, 8);