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?

【Vitest】「TypeError: vi.mocked(...).mockReturnValue is not a function」の解決方法

1
Posted at

はじめに

Next.jsのテストをvitestで書いていたところエラーが発生しました。

問題

TypeError: vi.mocked(...).mockReturnValue is not a function

【エラーが発生したコード(抜粋)】

import { useRouter } from "next/router";

vi.mock("next/navigation");
const mockRouter = {
  refresh: vi.fn(),
} as unknown as ReturnType<typeof useRouter>;

vi.mocked(useRouter).mockReturnValue(mockRouter);

解決方法

import { useRouter } from "next/navigation";

vi.mock("next/navigation");
const mockRouter = {
  refresh: vi.fn(),
} as unknown as ReturnType<typeof useRouter>;

vi.mocked(useRouter).mockReturnValue(mockRouter);

importのパスが間違っていたので修正しました。

おわりに

今回はしょうもないミスではあるんですが、エラー文で検索しても別のパターンのエラーしか出てこなかったため記事にしました。
いろんな場合でTypeError: vi.mocked(...).mockReturnValue is not a functionになるんですね。

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?