TypeError: (0, _local.default) is not a function when using next/font/local in Jest

huangapple go评论73阅读模式
英文:

TypeError: (0, _local.default) is not a function when using next/font/local in Jest

问题

我正在使用 MUI、React 和 TypeScript 开发一个 UI 库,使用 Nx 作为构建系统。为了测试,我正在使用 Jest。然而,在尝试按照 Next.js 文档中提供的说明向我的项目添加字体时,我遇到了一个错误。

引发问题的代码段如下:

import localFont from 'next/font/local';

const myFont = localFont({ src: './my-font.woff2' });

当运行 Jest 测试时,我收到以下错误消息:

TypeError: (0, _local.default) is not a function

我希望能够在 Jest 环境中成功导入并执行来自 next/font/locallocalFont 函数,而不会出现任何错误。myFont 变量应该被分配适当的值,以便我可以在我的 UI 库中使用该字体。我想要了解为什么会出现 "TypeError: (0, _local.default) is not a function" 错误消息,并找到解决方法。

英文:

I am developing a UI library using MUI, React, and TypeScript, with Nx as the build system. For testing purposes, I am using Jest. However, I encountered an error when trying to add a font to my project following the instructions provided in the Next.js documentation.

The code snippet causing the issue is as follows:

import localFont from 'next/font/local';

const myFont = localFont({ src: './my-font.woff2' });

When running Jest tests, I receive the following error message:

TypeError: (0, _local.default) is not a function

I would appreciate any insights or suggestions on how to resolve this error and successfully add the font to my project while using Jest for testing. Thank you!

I expect that the localFont function from next/font/local should be successfully imported and executed without any errors in the Jest environment. The myFont variable should be assigned the appropriate value, allowing me to use the font in my UI library. I would like to understand why the error message "TypeError: (0, _local.default) is not a function" is occurring and find a solution to resolve it.

答案1

得分: 2

发生了同样的情况,我不得不在我的 setupTests 文件中模拟导入,在每个测试文件中执行或在一个通用文件中执行(就像我所做的那样):

jest.mock("next/font/local", () => ({
  Rubik: () => ({
    style: {
      fontFamily: "mocked",
    },
  }),
}));

在这种情况下,我只是模拟了该对象,因为我只在我的测试中使用了这些属性。

英文:

It happened the same to me, I had to mock the import in my setupTests file, do so in each of your test files or in a general file (like I did):

jest.mock("next/font/local", () => ({
  Rubik: () => ({
    style: {
      fontFamily: "mocked",
    },
  }),
}));

In this case I just mocked that object because I only use those properties in my tests

答案2

得分: 1

问题涉及到使用'localFont'。我最近遇到了同样的问题,但上面的解决方案对我不起作用,因为我还在使用'next/font/local'

/*
  NextJS的字体优化内置了对任何字体文件的自动自托管。优化自动下载任何Google字体并在构建时将Google字体和本地字体放入应用的静态资产中。
  在运行测试时,根据您使用的字体优化,重要的是模拟模块导入'next/font/google'和'next/font/local'。

  localFont()函数的模拟。
*/
jest.mock("next/font/local", () => function() {
  return {
        style: {
          fontFamily: 'my_font'
      }
    }
  }
);
英文:

The question pertains to the use of 'localFont'. I recently had the same problem, but the solution above did not work for me, because I was also using 'next/font/local'

/*
  NextJS's font optimization has built-in automatic self-hosting for any font file.  The optimization automatically
  downloads any Google font and places Google and local fonts into an app's static assets all at BUILD time.  
  When running tests it's important to mock the module import 'next/font/google' and 'next/font/local' depending on
  which font optimization you're using.

  A mock for the function, localFont().
*/
jest.mock("next/font/local", () => function() {
  return {
        style: {
          fontFamily: 'my_font'
      }
    }
  }
);

huangapple
  • 本文由 发表于 2023年7月11日 14:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定