1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Next.js × Vitest】(0 , Geist) is not a function と(0 , Inter) is not a functionの解決方法

Last updated at Posted at 2025-09-03

はじめに

Next.jsVitestを使ってテストを実行していた際に発生したエラーについての記事です。

この記事は個人的なアウトプットを目的として作成したものです。そのため、誤った情報や不足している内容が含まれている可能性があります。もし間違いや気になる点がございましたら、ぜひご指摘いただけますと幸いです

現象

テスト実行時に以下のようなエラーが発生しました。

(0 , Geist) is not a function
(0 , Inter) is not a function

原因

テスト対象のlayout.tsxではNext.jsGoogle Fonts機能を使用していました。

describe("ページコンポーネント", () => {
 test("正常にレンダリングされること", () => {
   render(
     <RootLayout>
       <div>テスト</div>
     </RootLayout>
   );
   expect(screen.getByTestId("main-content")).toBeInTheDocument();
 });
});

一方、テストコードは以下のようにRootLayoutを直接レンダリングしています。

layout.tsx
  import { Geist, Geist_Mono } from "next/font/google";
  
  const geistSans = Geist({
    variable: "--font-geist-sans",
    subsets: ["latin"],
  });
  
  const geistMono = Geist_Mono({
    variable: "--font-geist-mono",
    subsets: ["latin"],
  });
  
  <body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>

しかしVitestのテスト環境(jsdom)ではNext.jsのフォント最適化機能が動作しない ため、GeistInterが未定義となり上記のエラーが出ていました。

解決方法

今回のケースではフォント指定が必須ではなかったため、該当部分を削除しました。

layout.tsx
  // Google Fontsのインポートを削除
  
  <body className="font-sans antialiased">

これでエラーは解消されました。
もし特定のフォントを使用したい場合は、テスト時にモックを用意するなど別の方法が必要になると思います。

終わりに

Next.js独自の機能(フォント最適化など)はテスト環境でそのまま動作しないことがあるようです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?