0
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?

【React】useNavigateでのページ遷移がJestでうまくテストできない2

Posted at

前回の記事で紹介した修正方法を適用しても、特定のケースでページ遷移のテストがうまくいかない問題に直面しました。

原因

調査の結果、以下の問題が原因であることが判明しました。

ライブラリの混在
テストコードと実際のページコードで、react-routerreact-router-dom が混在して使用されていました。これにより、Jest でモックしたuseNavigateと実際にページで使用しているuseNavigateが異なっていたためモックが意図通りできていませんでした。

修正方法

ライブラリの統一

react-routerreact-router-dom のどちらかに統一する必要があります。通常、react-router-dom を使用するのが一般的です。以下のようにインポートを修正します。

// 修正前
import { useNavigate } from "react-router";

// 修正後
import { useNavigate } from "react-router-dom";

まとめ

importは基本的に VSCode で自動的に追加していたためどのライブラリから取得しているかを意識していませんでした。そのためこのようなバグが発生していました。

今後は気を付けてimportしていきたいです。

この記事が皆様のコーディングライフの助けになれば幸いです。

参考

0
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
0
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?