
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
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
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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.