[英]Testing props, doesn't fail when wrong type is entered
I am using MERN stack and Redux.我正在使用 MERN 堆栈和 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.我尝试添加每个 PropType 的形状,也尝试添加对象本身,但似乎都没有任何区别。
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.函数 checkPropTypes() 返回 void 因此 propsErr 将始终未定义。
A solution could be to check that console.error do not output any warning for the test to pass.一个解决方案可能是检查console.error不输出任何警告以使测试通过。
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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.