Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

An opinionated Jest matcher that generates strict JSON schemas as it validates objects.

License

Notifications You must be signed in to change notification settings

ryparker/jest-schema-matcher

Repository files navigation

Jest-Schema-Matcher

npm version Lint-Build-Test-Publish semantic-release XO code style

An opinionated Jest matcher that generates, validates, and versions JSON Schemas for your APIs.

🚀 Quick start

  1. Add dependency
yarn add --dev jest-schema-matcher
  1. Set global variable SHOULD_UPDATE_SCHEMAS in your jest's global config
// jest.config.js

module.exports = {
  ...
  globals: {
    SHOULD_UPDATE_SCHEMAS: true, // If true, schemas will be written/updated
  }
}
  1. Add the matcher to your tests
test('Check against schema', () => {
  const object = {
    username: 'test_user_a124',
    password: 'fixtures_password',
  }

  expect(object).toMatchSchema('schemaName')
  /**
   * 1. If SHOULD_UPDATE_SCHEMAS === true:
   * Will create a new schema or update existing schema in same path as test file.
   * E.g. If test path is `.../__tests__/sample.tests.js`
   * Then schema will be created in `.../__tests__/schemas/schemaName.json`
   * --or--
   * If SHOULD_UPDATE_SCHEMAS === false:
   * Will infer a new schema and recommend schema changes if the saved schema could be improved.
   */

  /** 2. Will validate `object` against saved schema.*/
})

About

An opinionated Jest matcher that generates strict JSON schemas as it validates objects.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •