Testing

If you use Jest, Structure has a Jest extension called jest-structure that provides assertions to make it easy to test intances.

Installation

jest-structure is available in npm, so you can install it with npm or yarn as a development dependency:

npm install --save-dev jest-structure

# or

yarn --dev add jest-structure

Setup

After installing, you need to tell Jest to use jest-structure, this can be done in two ways:

By importing and manually adding it to Jest:

import jestStructure from 'jest-structure';

expect.extend(jestStructure);

Or by allowing jest-structure to add itself to Jest matchers:

import 'jest-structure/extend-expect';

Both ways can be done in a setup file or directly at the top of your test file

Matchers

toBeValidStructure()

This matcher passes if the structure is valid:

toBeInvalidStructure()

This matcher passes if the structure is invalid:

toHaveInvalidAttribute(path, messages)

This matcher allows you to assert that a single attribute of the structure is invalid, optionally passing the array of error messages for that attribute:

toHaveInvalidAttributes([ { path, messages } ])

This matcher allows you to assert that multiple attributes of the structure are invalid, optionally passing the array of error messages for each attribute:

Last updated

Was this helpful?