Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
jest-matcher-structure
Advanced tools
A custom jest
matcher for validating object structures
Install • Usage • List of Comparisons • TODO • Created By • License
$ npm install --save-dev jest-matcher-structure
jest-matcher-structure walks key-by-key through a structure object and a comparison object and verifies that the value in the structure object accurately describes the value in the comparison object.
It's easy to get started! Here's a quick and simple example:
import { toMatchStructure } from 'jest-matcher-structure'
expect.extend({ toMatchStructure })
test("structure matches object's structure", () => {
const structure = {
string: 'string',
number: 'number',
boolean: 'boolean',
function: n => n > 5,
literal: 'literal value',
nested: {
string: 'string',
number: 'number',
function: n => x < 5,
},
arrayLiteral: ['hello', 'hi', 1, 2, 3, true, false],
regex: /\d+/,
null: null,
}
const object = {
string: 'hello world',
number: 123,
boolean: true,
function: 10,
literal: 'literal value',
nested: {
string: 'string',
number: 1,
function: 3,
},
arrayLiteral: [false, true, 3, 2, 1, 'hi', 'hello'],
regex: '1234',
null: null,
}
expect(structure).toMatchStructure(object)
})
jest-matcher-structure comes with the helper functions some
, every
, and repeat
.
some
and every
are for fields where you need to test the comparison value against multiple truths. You pass them an array and jest-matcher-structure works its magic! Here's an example:
import { some, every, toMatchStructure } from 'jest-matcher-structure'
test('some and every', () => {
const structure = {
field1: some(['A', 'B']),
field2: every(['string', x => x >= 1200 && x <= 1500]),
}
const object = {
field1: 'A',
field2: '1300',
}
expect(structure).toMatchStructure(object)
})
repeat
is for consuming repeating comparisons in an array. For example, if you need to make sure an array is filled with strings, you can do the following:
import { repeat, toMatchStructure } from 'jest-matcher-structure'
test('repeat', () => {
const structure = {
field1: [repeat('string')],
}
const object = {
field1: ['hello', 'world', 'fubar'],
}
expect(structure).toMatchStructure(object)
})
some
every
repeat
some
, every
, repeat
MIT
FAQs
A custom jest matcher for validating object structures
The npm package jest-matcher-structure receives a total of 5 weekly downloads. As such, jest-matcher-structure popularity was classified as not popular.
We found that jest-matcher-structure 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
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.