How to test React PropTypes through Jest?

血红的双手。 提交于 2019-11-29 01:40:22

问题


I'm writing Jest tests for my React code and hoping to make use of/test the PropType checks. I am quite new to the Javascript universe. I'm using npm to install react-0.11.2 and have a simple:

var React = require('react/addons');

In my tests. My test looks quite similar to the jest/react tutorial example with code like:

var eventCell = TestUtils.renderIntoDocument(
  <EventCell
    slot={slot}
    weekId={weekId}
    day={day}
    eventTypes={eventTypes}
    />
);

var time = TestUtils.findRenderedDOMComponentWithClass(eventCell, 'time');
expect(time.getDOMNode().textContent).toEqual('19:00 ');

However it seems that the PropType checks in the EventCell component aren't being triggered. I understand that the checks are only run in Development mode but then I also thought that getting react through npm gave you the development version. The checks trigger in my browser when I build the component with watchify.

What am I missing?


回答1:


The underlying problem is How to test console.log?

The short answer is that you should replace the console.{method} for the duration of the test. The common approach is to use spies. In this particular case, you might want to use stubs to prevent the output.

Here is an example implementation using Sinon.js (Sinon.js provides standalone spies, stubs and mocks):

import {
    expect
} from 'chai';
import DateName from './../../src/app/components/DateName';
import createComponent from './create-component';
import sinon from 'sinon';

describe('DateName', () => {
    it('throws an error if date input does not represent 12:00:00 AM UTC', () => {
        let stub;

        stub = sinon.stub(console, 'error');

        createComponent(DateName, {date: 1470009600000});

        expect(stub.calledOnce).to.equal(true);
        expect(stub.calledWithExactly('Warning: Failed propType: Date unix timestamp must represent 00:00:00 (HH:mm:ss) time.')).to.equal(true);

        console.error.restore();
    });
});

In this example DataName component will throw an error when initialised with a timestamp value that does not represent a precise date (12:00:00 AM).

I am stubbing the console.error method (This is what Facebook warning module is using internally to generate the error). I ensure that the stub has been called once and with exactly one parameter representing the error.




回答2:


Intro

The answer by @Gajus definitely helped me (so, thanks Gajus). However, I thought I would provide an answer that:

  • Uses a more up-to-date React (v15.4.1)
  • Uses Jest (which comes with React)
  • Allows testing multiple prop values for a single prop
  • Is more generic

Summary

Like the approach suggested here by Gajus and elsewhere by others, the basic approach I'm suggesting is also to determine whether or not console.error is used by React in response to an unacceptable test prop value. Specifically, this approach involves doing the following for each test prop value:

  • mocking and clearing console.error (to ensure prior calls to console.error aren't interfering),
  • creating the component using the test prop value under consideration, and
  • confirming whether or not console.error was fired as expected.

The testPropTypes Function

The following code can be placed either within the test or as a separate imported/required module/file:

const testPropTypes = (component, propName, arraysOfTestValues, otherProps) => {
    console.error = jest.fn();
    const _test = (testValues, expectError) => {
        for (let propValue of testValues) {
            console.error.mockClear();
            React.createElement(component, {...otherProps, [propName]: propValue});
            expect(console.error).toHaveBeenCalledTimes(expectError ? 1 : 0);
        }
    };
    _test(arraysOfTestValues[0], false);
    _test(arraysOfTestValues[1], true);
};

Calling the Function

Any test examining propTypes can call testPropTypes using three or four parameters:

  • component, the React component that is modified by the prop;
  • propName, the string name of the prop under test;
  • arraysOfTestValues, an array of arrays of all the desired test values of the prop to be tested:
    • the first sub-array contains all acceptable test prop values, while
    • the second sub-array contains all unacceptable test prop values; and
  • optionally, otherProps, an object containing prop name/value pairs for any other required props of this component.

    The otherProps object is needed to ensure React doesn't make irrelevant calls to console.error because other required props are inadvertently missing. Simply include a single acceptable value for any required props, e.g. {requiredPropName1: anyAcceptableValue, requiredPropName2: anyAcceptableValue}.

Function Logic

The function does the following:

  • It sets up a mock of console.error which is what React uses to report props of incorrect type.

  • For each sub-array of test prop values provided it loops through each test prop value in each sub-array to test for prop type:

    • The first of the two sub-arrays should be a list of acceptable test prop values.
    • The second should be of unacceptable test prop values.
  • Within the loop for each individual test prop value, the console.error mock is first cleared so that any error messages detected can be assumed to have come from this test.

  • An instance of the component is then created using the test prop value as well as any other necessary required props not currently being tested.

  • Finally, a check is made to see whether a warning has been triggered, which should happen if your test tried to create a component using an inappropriate or missing prop.

Testing for Optional versus Required Props

Note that assigning null (or undefined) to a prop value is, from React's perspective, essentially the same thing as not providing any value for that prop. By definition this is acceptable for an optional prop but unacceptable for a required one. Thus, by placing null in either the array of acceptable or unacceptable values you test whether that prop is optional or required respectively.

Example Code

MyComponent.js (just the propTypes):

MyComponent.propTypes = {
    myProp1: React.PropTypes.number,      // optional number
    myProp2: React.PropTypes.oneOfType([  // required number or array of numbers
        React.PropTypes.number,
        React.PropTypes.arrayOf(React.PropTypes.number)
    ]).isRequired

MyComponent.test.js:

describe('MyComponent', () => {

    it('should accept an optional number for myProp1', () => {
        const testValues = [
            [0, null],   // acceptable values; note: null is acceptable
            ['', []] // unacceptable values
        ];
        testPropTypes(MyComponent, 'myProp1', testValues, {myProp2: 123});
    });

    it('should require a number or an array of numbers for myProp2', () => {
        const testValues = [
            [0, [0]], // acceptable values
            ['', null] // unacceptable values; note: null is unacceptable
        ];
        testPropTypes(MyComponent, 'myProp2', testValues);
    });
});

Limitation of This Approach (IMPORTANT)

There are currently some significant limitations on how you can use this approach which, if over-stepped, could be the source of some hard-to-trace testing bugs. The reasons for, and implications of, these limitations are explained in this other SO question/answer. In summary, for simple prop types, like for myProp1, you can test as many unacceptable non-null test prop values as you want as long as they are all of different data types. For some complex prop types, like for myProp2, you can only test a single unacceptable non-null prop value of any type. See that other question/answer for a more in-depth discussion.




回答3:


Mocking console.error is not suitable for use in unit tests! @AndrewWillems linked to another SO question in a comment above that describes the problems with this approach.

Check out this issue on facebook/prop-types for discussion about the ability for that library to throw instead of logging propType errors (at the time of writing, it's not supported).

I have published a helper library to provide that behaviour in the mean time, check-prop-types. You can use it like this:

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` === null);

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`.');



回答4:


A new package jest-prop-type-error is simple to add and fails on PropType errors:

Install via:

yarn add -D jest-prop-type-error

Then add the following to your package.json's setupFiles in the jest section:

"setupFiles": [
  "jest-prop-type-error"
]



回答5:


Since ReactJS will only send warnings to the console but will not actually throw an error, I test prop values in this way:

var myTestElement = TestUtils.renderIntoDocument(
<MyTestElement height={100} /> );

it("check MyTestElement props", function() {

   expect( typeof myTestElement.props.height ).toEqual ( 'number' );

});


来源:https://stackoverflow.com/questions/26124914/how-to-test-react-proptypes-through-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!