Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
chai-match-pattern
Advanced tools
The chai-match-pattern package is a plugin for the Chai assertion library that allows for pattern-based matching of objects. It is particularly useful for testing complex data structures where exact matches are not necessary, and patterns can be used to validate the structure and content of the data.
Pattern Matching
This feature allows you to use lodash predicates to define patterns that the actual data should match. In this example, the actual object is checked against a pattern where 'id' and 'age' should be numbers and 'name' should be a string.
const chai = require('chai');
const chaiMatchPattern = require('chai-match-pattern');
const _ = chaiMatchPattern.getLodashModule();
chai.use(chaiMatchPattern);
const expect = chai.expect;
const actual = {
id: 123,
name: 'John Doe',
age: 30
};
const pattern = {
id: _.isNumber,
name: _.isString,
age: _.isNumber
};
expect(actual).to.matchPattern(pattern);
Partial Matching
Partial matching allows you to specify only a subset of the properties that need to be matched. In this example, the pattern only specifies that 'id' should be a number and 'address.city' should be 'New York', ignoring other properties.
const actual = {
id: 123,
name: 'John Doe',
age: 30,
address: {
city: 'New York',
zip: '10001'
}
};
const pattern = {
id: _.isNumber,
address: {
city: 'New York'
}
};
expect(actual).to.matchPattern(pattern);
Custom Pattern Matching
Custom pattern matching allows you to define your own functions to validate the properties. In this example, the 'id' should be greater than 100 and 'name' should start with 'John'.
const actual = {
id: 123,
name: 'John Doe',
age: 30
};
const pattern = {
id: (val) => val > 100,
name: (val) => val.startsWith('John')
};
expect(actual).to.matchPattern(pattern);
chai-subset is a Chai plugin that allows for partial matching of objects. It is similar to chai-match-pattern in that it allows you to assert that an object contains a subset of properties. However, chai-subset does not support the use of predicates or custom functions for matching, which makes chai-match-pattern more flexible for complex pattern matching.
chai-like is another Chai plugin that provides similar functionality to chai-match-pattern by allowing pattern-based matching of objects. It supports partial matching and custom matchers, but it is less focused on using lodash predicates, which is a key feature of chai-match-pattern.
Jest is a popular testing framework that includes built-in support for pattern matching with its 'expect.objectContaining' and 'expect.arrayContaining' matchers. While Jest is a full testing framework, chai-match-pattern is a specific plugin for Chai, making it more suitable for projects already using Chai.
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
checkit
(e.g. _.isURL
, _.isEmail
)lodash
(e.g. _.isObject
, _.isNaN
)# with npm
npm install chai-match-pattern --save-dev
# or with yarn
yarn add --dev chai-match-pattern
In your test file insert
const chai = require('chai');
const chaiMatchPattern = require('chai-match-pattern');
chai.use(chaiMatchPattern);
const _ = 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);
FAQs
Validates a deep structured JSON pattern
The npm package chai-match-pattern receives a total of 152,656 weekly downloads. As such, chai-match-pattern popularity was classified as popular.
We found that chai-match-pattern demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.