javascript - Jest 测试通过但得到一个错误:在渲染组件时序列化从 `getStaticProps` 返回的 `symbol` 时出错。为什么?
问题描述
尝试将 JSX.Element 类型参数作为道具传递给我的组件时,运行时出现序列化错误yarn dev
但我的笑话测试通过了。
为什么实际上组件没有正确渲染时渲染测试通过了?
这是代码:
//Index.tsx
export const getStaticProps: GetStaticProps = async (context) => {
const greeting = <DateFC date={new Date()} />;
const saved_locations: JSX.Element[] = [];
return {
props: {
greeting: greeting,
saved_locations: saved_locations,
},
};
};
和错误
Server Error
Error: Error serializing `.greeting.$$typeof` returned from `getStaticProps` in "/".
Reason: `symbol` cannot be serialized as JSON. Please only return JSON serializable data types.
This error happened while generating the page. Any console logs will be displayed in the terminal window.
使用 react-testing-library 中的 render 函数传递渲染 Index.tsx 的测试
test("should contain a header section with a role of banner", () => {
const { container: dom_node } = render(<Index greeting={<DateFC />} />);
expect(screen.getByRole("banner")).toStrictEqual(
dom_node.querySelector("header")
);
});
和 Index.tsx 组件定义
function Index(props: IndexFCProps) {
return (
<>
<header>{props.greeting}</header>
<main>
<section aria-label="weather in saved locations">
{props.saved_locations}
</section>
<section aria-label="weather in favorite location">
<WeatherTile withTime={false}></WeatherTile>
</section>
</main>
<nav>
<Nav></Nav>
</nav>
</>
);
}
我正在使用 NextJs 反应框架。
解决方案
推荐阅读
- android - 我在安装 intel HAXM 时遇到问题
- javascript - 如何在 Vanilla JavaScript 中制作 Jquery 的 .html() 函数
- python - Python /从异常文件中获取唯一令牌
- android - 无法使用 Glide v4 加载位图
- django - Django:创建将两个“用户”对象关联在一起的权限
- java - 使用paintComponent时正确的MVC方法
- java - VSCode 调试器不会像 eclipse 在 Java 中那样精确地突出错误
- javascript - 带有 setState 的 Onblur 事件不允许在 React 中对子项进行 onClick
- html - 在 Bootstrap 轮播中垂直居中 div
- python - 使用 curl 推荐获取统计信息列表的问题 - REST API