jest-structure
Custom Jest matchers to test Structure instances.
Example usage
; ; ; ; ; ;
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 (in a setup file or directly in the top of your test file):
; expect;
- By allowing jest-structure to add itself to Jest matchers:
;
Matchers
toBeValidStructure()
This matcher passes if the structure is valid:
const User = {}; const validUser = name: 'Me' ; ; // passes const invalidUser = ; ; // fails
toBeInvalidStructure()
This matcher passes if the structure is invalid:
const User = {}; const invalidUser = ; ; // passes const validUser = name: 'Me' ; ; // fails
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:
const User = {}; const user = age: 42 ; // passes, because name is invalid; // fails, because age is valid; // passes, because name is invalid with this message; // fails, because name is invalid but not with this message; // passes. Notice that you can even use arrayContaining to check for a subset of the errros; // passes. And stringContaining can be used as well;
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:
const User = {}; const user = age: 42 ; // passes, because name is invalid; // fails, because age is valid; // fails, because name is invalid but age is valid; // passes, because name is invalid with this message; // fails, because name is invalid but not with this message; // passes. Notice that you can even use arrayContaining to check for a subset of the errros; // passes. And stringContaining can be used as well;