Registry / testing / jest-json-schema

jest-json-schema

JSON →
library6.1.0jsnpmunverified

A Jest matcher for validating objects against JSON Schema (draft-04, draft-06, draft-07, 2019-09, 2020-12 via Ajv). Version 6.1.0 supports Node ^12.13.0 || ^14.15.0 || >=15.0.0, with releases every few months. Provides `toMatchSchema` matcher and `matchersWithOptions` for advanced Ajv configuration (custom formats, keywords, Ajv class). Uses Ajv under the hood. Simpler than writing custom matchers, integrates seamlessly into Jest.

npm install jest-json-schema
INSTALL
IMPORT
SIG · JEST-JSON-SCHEMA
J
jest-json-schema
testingjavascriptv6.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

matchers
import { matchers } from 'jest-json-schema'; expect.extend(matchers);
const matchers = require('jest-json-schema').matchers; expect.extend(matchers);
ESM and CJS both supported; named import from package.
matchersWithOptions
import { matchersWithOptions } from 'jest-json-schema'; expect.extend(matchersWithOptions({ AjvClass: Ajv2020 }));
import { matchersWithOptions } from 'jest-json-schema'; expect.extend(matchersWithOptions({ ajvClass: Ajv2020 }));
Option key is 'AjvClass' (capital A, capital C). Common mistake: using 'ajvClass' (lowercase).
default
import jestJsonSchema from 'jest-json-schema';
No common wrong pattern
Default export is an object with 'matchers' and 'matchersWithOptions' properties.

Shows basic setup with import and use of toMatchSchema matcher to validate an object against a JSON schema.

import { matchers } from 'jest-json-schema'; expect.extend(matchers); const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' }, }, required: ['name', 'age'], }; test('validates my object', () => { expect({ name: 'Alice', age: 30 }).toMatchSchema(schema); });
Debug
Known issues
gotchaSchema must be a valid JSON Schema object; plain objects without schema properties will be rejected.
fix
Ensure schema has type/properties or $ref; use validators like Ajv standalone if unsure.
affects: >=1.0.0
gotchaDefaults: allErrors: true. If you prefer fail-fast behavior, override via matchersWithOptions.
fix
Use matchersWithOptions({ allErrors: false })
affects: >=2.0.0
deprecatedAjv v6 options like 'v5' are deprecated; Ajv v8 uses JSON Schema draft-07 by default.
fix
Use AjvClass option to specify draft-04/06/2019-09/2020-12.
affects: >=5.0.0
Errors
Common errors & fixes
TypeError: expect(...).toMatchSchema is not a function
matchers not extended; or imported incorrectly.
fix
Run expect.extend(matchers) after import. Ensure import is correct: import { matchers } from 'jest-json-schema';
ReferenceError: Ajv is not defined
Not installed or not imported when using matchersWithOptions with custom Ajv class.
fix
npm install ajv --save-dev and import Ajv from 'ajv'.
ValidationError: no schema with key or ref ...
Missing dependent schemas when using $ref across files.
fix
Use matchersWithOptions with addSchema to include all referenced schemas.
Upgrade
Version history
6.1.0latest on npm
Audit
Dependencies
ajvrequiredCore validation engine; this package wraps Ajv to provide Jest matchers.
Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources
jest-json-schema — npm install jest-json-schema · libregistry