下一个路由器的下一个JS测试问题

发布于 2025-02-05 04:42:56 字数 189 浏览 2 评论 0原文

我正在尝试在NextJ中为我的项目编写React-Testing-Library编写单元测试,但是

const t = locale === 'fa' ? fa : en;

在进行测试时,我在下一个路由器方面遇到 了麻烦。 无法破坏“路由器”的“地方”,因为它是空的。 任何人对这个问题有任何想法吗?

I am trying to writing unit test with react-testing-library for my project in nextjs but I have trouble with next router specially localization

const t = locale === 'fa' ? fa : en;

I face to this error while I run the test
Cannot destructure property 'locale' of 'router' as it is null.
any body have idea about this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-02-12 04:42:56

您只需模拟userOuter()即可按下

jest.mock('next/router', () => ({
  useRouter() {
    return {
      locale: 'en'
      //You can add whatever keys that you required
    };
  }
}));

“描述”块上方

You will need to just mock the useRouter() like follow

jest.mock('next/router', () => ({
  useRouter() {
    return {
      locale: 'en'
      //You can add whatever keys that you required
    };
  }
}));

Place this above the "describe" block

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文