简体   繁体   中英

Testing props, doesn't fail when wrong type is entered

I am using MERN stack and Redux. I am trying to test some of my props on one of my components. I have defined all of the types and created some tests but they just seem to pass even when my tests have the wrong type of data entered. Anyone any idea what i am missing. I have tried adding in the shape of each PropType and also tried to add in the object itself but neither seem to make any difference.

Component

Subject.propTypes = {
  subjects: PropTypes.arrayOf(PropTypes.object).isRequired,
  comments: PropTypes.arrayOf(PropTypes.object).isRequired,
  users: PropTypes.arrayOf(PropTypes.object).isRequired,
  newPost: PropTypes.object,
};

Test

describe("Checking PropTypes", () => {
    it("Should not throw a warning", () => {
      const expectedProps = {
        subjects: [
          {
            title: "title test one",
            Summary: "summary test one",
            description: "description test one",
            rating: 1,
            noOfVotes: 1,
            author: "author test one",
            category: "category test one",
            date: Date.now,
            true: 1,
            false: 1,
            mostlyTrue: 1,
            mostlyFalse: 1,
            halfAndHalf: 1,
            links: "links test one",
          },
        ],
        comments: [
          {
            title: "comments-title test one",
            date: Date.now,
            comment: "comments-comment test one",
            author: 12345,
            subject: 12345,
            topic: "comments-topic test one",
            rating: 1,
            noOfVotes: 1,
          },
        ],
        users: [
          {
            name: "users-name test one",
            email: "users-email test one",
            password: "users-password test one",
            date: Date.now,
            rating: 1,
            noOfVotes: 1,
          },
        ],
        newPost: {
          title: "newPost-title test one",
          date: Date.now,
          comment: "newPost-comment test one",
          author: "12345",
          subject: "12345",
          topic: "newPost-topic test one",
          rating: 2,
          noOfVotes: 2,
        },
      };

      const propsErr = checkPropTypes(
        Subject.propTypes,
        expectedProps,
        "props",
        "Subject"
      );
      expect(propsErr).toBeUndefined();
    });

So even this passes

 describe("Checking PropTypes", () => {
        it("Should not throw a warning", () => {
          const expectedProps = {
            subjects: 22,
            comments: 45,
            users: 88,
            newPost: 0,
          };
    
          const propsErr = checkPropTypes(
            Subject.propTypes,
            expectedProps,
            "props",
            "Subject"
          );
          expect(propsErr).toBeUndefined();
        });

The function checkPropTypes() returns void therefore propsErr will always be undefined.

A solution could be to check that console.error do not output any warning for the test to pass.

 describe("Checking PropTypes", () => {
        it("Should not throw a warning", () => {
          
          const expectedProps = {
            // your expected props
          };

          const consoleSpy = jest.spyOn(console, "error");
          checkPropTypes(Subject.propTypes, expectedProps, "props", Subject.name);
          expect(consoleSpy).not.toHaveBeenCalled();
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM