You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

jest-validate

Package Overview
Dependencies
Maintainers
6
Versions
218
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-validate

Generic configuration validation tool that helps you with warnings, errors and deprecation messages as well as showing users examples of correct configuration.


Version published
Weekly downloads
26M
decreased by-1.28%
Maintainers
6
Install size
912 kB
Created
Weekly downloads
 

Package description

What is jest-validate?

The jest-validate package is a utility for validating and ensuring that configurations passed to Jest, the JavaScript testing framework, are correct. It checks if the provided configuration object adheres to Jest's expected configurations and provides warnings or errors for any invalid or unknown options. This helps developers to quickly identify issues with their Jest setup.

What are jest-validate's main functionalities?

Validation of configuration objects

This feature allows developers to validate their Jest configuration objects against a schema. It ensures that the configuration provided matches the expected format and values that Jest can work with.

{"validateConfig": require('jest-validate').validateConfig, "config": { "verbose": true }, "exampleConfig": { "verbose": false }}

Customization of validation messages

Developers can provide custom messages for unknown or deprecated options. This helps in guiding the user to correct their configuration with helpful feedback.

{"validateConfig": require('jest-validate').validateConfig, "config": { "unknownOption": true }, "exampleConfig": { "verbose": false }, "options": { "comment": "A custom message for unknownOption" }}

Deprecation warnings

jest-validate can warn users about deprecated configuration options. It provides a mechanism to inform users about the new options they should use instead.

{"validateConfig": require('jest-validate').validateConfig, "config": { "scriptPreprocessor": "<rootDir>/preprocessor.js" }, "exampleConfig": { "transform": {"^.+\\.js$": "<rootDir>/preprocessor.js"} }, "deprecatedConfig": { "scriptPreprocessor": "Please use `transform` instead" }}

Other packages similar to jest-validate

Changelog

Source

24.9.0

Features

  • [expect] Highlight substring differences when matcher fails, part 1 (#8448)
  • [expect] Highlight substring differences when matcher fails, part 2 (#8528)
  • [expect] Improve report when mock-spy matcher fails, part 1 (#8640)
  • [expect] Improve report when mock-spy matcher fails, part 2 (#8649)
  • [expect] Improve report when mock-spy matcher fails, part 3 (#8697)
  • [expect] Improve report when mock-spy matcher fails, part 4 (#8710)
  • [expect] Throw matcher error when received cannot be jasmine spy (#8747)
  • [expect] Improve report when negative CalledWith assertion fails (#8755)
  • [expect] Improve report when positive CalledWith assertion fails (#8771)
  • [expect] Display equal values for ReturnedWith similar to CalledWith (#8791)
  • [expect, jest-snapshot] Change color from green for some args in matcher hints (#8812)
  • [jest-snapshot] Highlight substring differences when matcher fails, part 3 (#8569)
  • [jest-core] Improve report when snapshots are obsolete (#8665)
  • [jest-cli] Improve chai support (with detailed output, to match jest exceptions) (#8454)
  • [*] Manage the global timeout with --testTimeout command line argument. (#8456)
  • [pretty-format] Render custom displayName of memoized components (#8546)
  • [jest-validate] Allow maxWorkers as part of the jest.config.js (#8565)
  • [jest-runtime] Allow passing configuration objects to transformers (#7288)
  • [@jest/core, @jest/test-sequencer] Support async sort in custom testSequencer (#8642)
  • [jest-runtime, @jest/fake-timers] Add jest.advanceTimersToNextTimer (#8713)
  • [@jest-transform] Extract transforming require logic within jest-core into @jest-transform (#8756)
  • [jest-matcher-utils] Add color options to matcherHint (#8795)
  • [jest-circus/jest-jasmine2] Give clearer output for Node assert errors (#8792)
  • [jest-runner] Export all types in the type signature of jest-runner (#8825)

Fixes

  • [jest-cli] Detect side-effect only imports when running --onlyChanged or --changedSince (#8670)
  • [jest-cli] Allow --maxWorkers to work with % input again (#8565)
  • [babel-plugin-jest-hoist] Expand list of whitelisted globals in global mocks (#8429)
  • [jest-core] Make watch plugin initialization errors look nice (#8422)
  • [jest-snapshot] Prevent inline snapshots from drifting when inline snapshots are updated (#8492)
  • [jest-haste-map] Don't throw on missing mapper in Node crawler (#8558)
  • [jest-core] Fix incorrect passWithNoTests warning (#8595)
  • [jest-snapshots] Fix test retries that contain snapshots (#8629)
  • [jest-mock] Fix incorrect assignments when restoring mocks in instances where they originally didn't exist (#8631)
  • [expect] Fix stack overflow when matching objects with circular references (#8687)
  • [jest-haste-map] Workaround a node >=12.5.0 bug that causes the process not to exit after tests have completed and cancerous memory growth (#8787)

Chore & Maintenance

  • [docs] Replace FlowType with TypeScript in CONTRIBUTING.MD code conventions
  • [jest-leak-detector] remove code repeat (#8438)
  • [docs] Add example to jest.requireActual (#8482)
  • [docs] Add example to jest.mock for mocking ES6 modules with the factory parameter (#8550)
  • [docs] Add information about using jest.doMock with ES6 imports (#8573)
  • [docs] Fix variable name in custom-matcher-api code example (#8582)
  • [docs] Fix example used in custom environment docs (#8617)
  • [docs] Updated react tutorial to refer to new package of react-testing-library (@testing-library/react) (#8753)
  • [docs] Updated imports of react-testing-library to @testing-library/react in website (#8757)
  • [jest-core] Add getVersion (moved from jest-cli) (#8706)
  • [docs] Fix MockFunctions example that was using toContain instead of toContainEqual (#8765)
  • [*] Make sure copyright header comment includes license (#8783)
  • [*] Check copyright and license as one joined substring (#8815)
  • [docs] Fix WatchPlugins jestHooks.shouldRunTestSuite example that receives an object (#8784)
  • [*] Enforce LF line endings (#8809)
  • [pretty-format] Delete obsolete link and simplify structure in README (#8824)
  • [docs] Fix broken transform link on webpack page (#9155)

Performance

  • [jest-watcher] Minor optimization for JestHook (#8746)
  • [@jest/reporters] Prevent runaway CPU usage with --notify on macOS (#8831)

Readme

Source

jest-validate

Generic configuration validation tool that helps you with warnings, errors and deprecation messages as well as showing users examples of correct configuration.

npm install --save jest-validate

Usage

import {validate} from 'jest-validate';

validate((config: Object), (options: ValidationOptions)); // => {hasDeprecationWarnings: boolean, isValid: boolean}

Where ValidationOptions are:

type ValidationOptions = {
  blacklist?: Array<string>,
  comment?: string,
  condition?: (option: any, validOption: any) => boolean,
  deprecate?: (
    config: Object,
    option: string,
    deprecatedOptions: Object,
    options: ValidationOptions,
  ) => true,
  deprecatedConfig?: {[key: string]: Function},
  error?: (
    option: string,
    received: any,
    defaultValue: any,
    options: ValidationOptions,
  ) => void,
  exampleConfig: Object,
  recursive?: boolean,
  title?: Title,
  unknown?: (
    config: Object,
    exampleConfig: Object,
    option: string,
    options: ValidationOptions,
  ) => void,
};

type Title = {|
  deprecation?: string,
  error?: string,
  warning?: string,
|};

exampleConfig is the only option required.

API

By default jest-validate will print generic warning and error messages. You can however customize this behavior by providing options: ValidationOptions object as a second argument:

Almost anything can be overwritten to suite your needs.

Options

  • recursiveBlacklist – optional array of string keyPaths that should be excluded from deep (recursive) validation.
  • comment – optional string to be rendered below error/warning message.
  • condition – an optional function with validation condition.
  • deprecate, error, unknown – optional functions responsible for displaying warning and error messages.
  • deprecatedConfig – optional object with deprecated config keys.
  • exampleConfig – the only required option with configuration against which you'd like to test.
  • recursive - optional boolean determining whether recursively compare exampleConfig to config (default: true).
  • title – optional object of titles for errors and messages.

You will find examples of condition, deprecate, error, unknown, and deprecatedConfig inside source of this repository, named respectively.

exampleConfig syntax

exampleConfig should be an object with key/value pairs that contain an example of a valid value for each key. A configuration value is considered valid when:

  • it matches the JavaScript type of the example value, e.g. string, number, array, boolean, function, or object
  • it is null or undefined
  • it matches the Javascript type of any of arguments passed to MultipleValidOptions(...)

The last condition is a special syntax that allows validating where more than one type is permissible; see example below. It's acceptable to have multiple values of the same type in the example, so you can also use this syntax to provide more than one example. When a validation failure occurs, the error message will show all other values in the array as examples.

Examples

Minimal example:

validate(config, {exampleConfig});

Example with slight modifications:

validate(config, {
  comment: '  Documentation: http://custom-docs.com',
  deprecatedConfig,
  exampleConfig,
  title: {
    deprecation: 'Custom Deprecation',
    // leaving 'error' and 'warning' as default
  },
});

This will output:

Warning:
● Validation Warning:

  Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
  This is either a typing error or a user mistake. Fixing it will remove this message.

  Documentation: http://custom-docs.com
Error:
● Validation Error:

  Option transform must be of type:
    object
  but instead received:
    string

  Example:
  {
    "transform": {
      "^.+\\.js$": "<rootDir>/preprocessor.js"
    }
  }

  Documentation: http://custom-docs.com

Example validating multiple types

import {multipleValidOptions} from 'jest-validate';

validate(config, {
  // `bar` will accept either a string or a number
  bar: multipleValidOptions('string is ok', 2),
});
Error:
● Validation Error:

  Option foo must be of type:
    string or number
  but instead received:
    array

  Example:
  {
    "bar": "string is ok"
  }

  or

  {
    "bar": 2
  }

  Documentation: http://custom-docs.com
Deprecation

Based on deprecatedConfig object with proper deprecation messages. Note custom title:

Custom Deprecation:

  Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.

  Jest now treats your current configuration as:
  {
    "transform": {".*": "xxx"}
  }

  Please update your configuration.

  Documentation: http://custom-docs.com

FAQs

Package last updated on 16 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc