expect-proptypes

0.2.0 • Public • Published

expect-proptypes

This library extends expect to allow for assertions about object shapes, using React's PropTypes library or the standalone proptypes library on npm.

Usage

const {PropTypes} = require("react");
 
// Extend expect with this library.
const expect = require("expect");
const expectPropTypes = require("expect-proptypes")
expect.extend(expectPropTypes);
 
// Find some object you want to check the shape of
const obj = {
  num: 1,
  foo: "bar",
};
 
// `toHaveProps` does not allow additional props
expect(obj).toHaveProps({
  num: PropTypes.number.isRequired,
  foo: PropTypes.string.isRequired,
  optional: PropTypes.bool,
});
 
// `toContainProps` allows additional propses
expect(obj).toContainProps({
  num: PropTypes.number.isRequired,
});

How can I get better error logging?

If your proptypes have a property __name__ which is a string, this will be used in the error logs.

What if I want more detailed types?

Consider using my library extended-proptypes, which adds many useful validators, such as dates,constants, constants and regex matching for strings and object keys.

Readme

Keywords

none

Package Sidebar

Install

npm i expect-proptypes

Weekly Downloads

3

Version

0.2.0

License

ISC

Last publish

Collaborators

  • peterkhayes