An extension of the popular jest-json-schema package - JSON schema matching for jest.
const {
dateTime,
strictObject,
uuidType,
numberType,
booleanType,
arrayOfItems,
anyOf,
exactly,
stringType,
stringTypeCanBeEmpty,
expectToMatchSchema,
objectWithRequiredProps,
} = require("jest-json-schema-extended")
const objectToTest = {
id: "47bddd19-e142-45ee-8679-463be8763022",
enabled: false,
created: "2019-06-12T15:08:40.292Z",
requestors: [],
owners: [
{
id: 1,
lastName: "Robson",
firstName: "",
additionalInfo: {
favouriteTvShow: "The Simpsons",
goodAtCooking: false,
propertyThatCanHaveVaryingTypes: "Hello",
},
},
{
id: 2,
lastName: "Haroldson",
firstName: "Doris",
additionalInfo: {
favouriteTvShow: "Parks & Recreation",
married: true,
propertyThatCanHaveVaryingTypes: 42,
},
},
],
}
const schema = strictObject({
id: uuidType,
enabled: booleanType,
created: dateTime,
requestors: exactly([]),
owners: arrayOfItems(
strictObject({
id: numberType,
lastName: stringType,
firstName: stringTypeCanBeEmpty,
additionalInfo: objectWithRequiredProps({
favouriteTvShow: stringType,
propertyThatCanHaveVaryingTypes: anyOf([
stringType,
numberType,
]),
}),
})
),
})
expectToMatchSchema(objectToTest, schema)
npm i --save-dev jest-json-schema-extended
- In order to be able to use
expectToMatchSchema
inside jest, you need to call the following inside your test file (or you place it inside a test framework setup file):
const {setup} = require("jest-json-schema-extended");
setup()
Asserts that the property is an object.
Asserts that the property is a string. The string is not allowed to be empty - use stringTypeCanBeEmpty instead if emptiness is needed.
Asserts that the property is a string. Allows the string to be empty.
Asserts that the property is a string looking like an URL.
Asserts that the property is a string in date-time format.
Asserts that the property is a string looking like a UUID.
Asserts that the property is a string looking like a UNIX path.
Asserts that the property is a number.
Asserts that the property holds the value null
.
Asserts that the property is a boolean.
Asserts that the property is an array.
Loosely asserts that the property is an array of objects.
Assert that a value matches a JSON schema. Prints out errors if mismatches found.
Param | Type | Description |
---|---|---|
value | Object |
The JSON object or value (string, boolean etc.) to test. |
schema | Object |
The JSON schema to test the object against. |
Asserts that the object contains all the properties specified - additional properties are not allowed.
Param | Type | Description |
---|---|---|
properties | Object |
Asserts for any key on the object that the property exists. Example: {propOne: stringType, propTwo: numberType} |
[options] | Object |
Accepts a property optionalProps which can be a list of optional properties that don't need to be present. |
Asserts that the object contains all the properties specified - additional properties are allowed.
Param | Type | Description |
---|---|---|
properties | Object |
Asserts for any key on the object that the property exists. Example: {propOne: stringType, propTwo: numberType} |
Assert a JSON schema against each array item.
Param | Type | Description |
---|---|---|
itemSchema | ||
options | Object |
Accepts an option property minItems which can be used to check that the array contains at least x amount of items. |
Assert that the property is one of the provided JSON schemas.
Param |
---|
itemSchemas |
Asserts that the property is a string matching the regular expression provided.
Param | Type |
---|---|
regex | RegExp |
Asserts that the property is exactly the string as specified.
Param | Type |
---|---|
expStr | String |
Asserts that the property is a number greater than the given number.
Param | Type |
---|---|
minimum | Number |
Asserts that the property is a number less than the given number.
Param | Type |
---|---|
maximum | Number |
Asserts that the property received is one of the values given in the array.
Param | Type |
---|---|
valuesExpected | Array.<any> |
Asserts that the property is an array of the exactly specified length.
Param | Type |
---|---|
length | Number |
Asserts that the property is exactly the value as specified. Can be anything - an object, an array, a string, a boolean, ...
Param | Type |
---|---|
valueExpected | any |
Helper function to check whether the passed in object is a JSON schema or a plain object.
Param | Type |
---|---|
toBeDetermined | Object |