【发布时间】:2021-08-17 04:45:21
【问题描述】:
我想在 Next.js 的 getServerSideProps 函数上使用 Jest 和 Enzyme 运行测试。该函数如下所示:
export const getServerSideProps: GetServerSideProps = async (context) => {
const id = context?.params?.id;
const businessName = getBusinessName(id);
return {
props: {
businessName: response.data?.name,
businessID: id,
},
};
};
但是,由于我在函数中使用了context 参数,因此我需要将上下文传递到我的测试中。我现在的测试看起来像:
it("check on good case", () => {
const value = getServerSideProps(/*I'm not sure what to put here*/);
expect(value).toEqual({props: {businessName: "Name", businessID: "fjdks"}})
});
我的问题是我将什么传递给上下文参数。我知道它需要是类型:GetServerSidePropsContext<ParsedUrlQuery>。但我不确定如何创建该类型。我可以将什么传递给函数,同时还允许我在参数中添加 id 值?
【问题讨论】:
标签: reactjs typescript jestjs next.js