Giter Club home page Giter Club logo

check-prop-types's Introduction

checkPropTypes

Build Status View on npm

Manually check PropTypes-compatible proptypes, returning any errors instead of logging them to console.error.

This function is more suitable for checking propTypes in unit tests than mocking console.error, avoiding some serious problems with that approach.

Install

$ npm install --save-dev check-prop-types

Usage

Call it just like PropTypes.checkPropTypes, but it returns any problems as an error message string.

import PropTypes from 'prop-types';
import checkPropTypes from 'check-prop-types';

const HelloComponent = ({ name }) => (
  <h1>Hi, {name}</h1>
);

HelloComponent.propTypes = {
  name: PropTypes.string.isRequired,
};

let result = checkPropTypes(HelloComponent.propTypes, { name: 'Julia' }, 'prop', HelloComponent.name);
assert(result === undefined);

result = checkPropTypes(HelloComponent.propTypes, { name: 123 }, 'prop', HelloComponent.name);
assert(result === 'Failed prop type: Invalid prop `name` of type `number` supplied to `HelloComponent`, expected `string`.');

assertPropTypes

To throw errors instead of returning them, a helper called assertPropTypes is included:

import PropTypes from 'prop-types';
import { assertPropTypes } from 'check-prop-types';

const HelloComponent = ({ name }) => (
  <h1>Hi, {name}</h1>
);

HelloComponent.propTypes = {
  name: PropTypes.string.isRequired,
};

assertPropTypes(HelloComponent.propTypes, { name: 'Julia' }, 'prop', HelloComponent.name);
// fine...

assertPropTypes(HelloComponent.propTypes, { name: 123 }, 'prop', HelloComponent.name);
// throws Error: Failed prop type: Invalid prop `name` of type `number` supplied to `HelloComponent`, expected `string`.

Working with multiple Props and PropTypes.isRequired()

When testing PropTypes on components that feature multiple props, or required props, specifying a subset of the component's props in the test will maintain test isolation:

import PropTypes from 'prop-types'
import { checkPropTypes } from 'check-prop-types';

const HelloComponent = ({ firstName, lastName }) => (
    <h1>Hi, {firstName} {lastName}</h2>
);

HelloComponent.propTypes = {
    firstName: PropTypes.string.isRequired,
    lastName: PropTypes.string
};

checkPropTypes(
    { firstName: HelloComponent.propTypes.firstName },
    {},
    'prop',
    'HelloComponent'
); 
// Returns: Failed prop type: The prop `firstName` is marked as required in HelloComponent

checkPropTypes(
    { lastName: HelloComponent.propTypes.lastName },
    { lastName: 123 },
    'prop'
    'HelloComponent'
); 
// Returns: Failed prop type: Invalid prop `lastName` of type `number` supplied to HelloComponent

See test.js for more usage examples.

check-prop-types's People

Contributors

chrisrichard avatar decompil3d avatar firec0der avatar stevethatcodes avatar uniphil avatar voziv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.