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?

React Testing Library の検証( .toBeInTheDocument() / .toHaveTextContent() )についてまとめてみた

Posted at

はじめに

React Testing Library でテストを書くとき、この「結果確認」が最後に必ず必要になります(テスト対象の画面が、想定した状態になっているかを expect でチェックすること)。

そこでこの記事では、まず覚えるべき3つのマッチャー をシンプルにまとめます。

1. 要素が存在するか確認:.toBeInTheDocument()

expect(element).toBeInTheDocument();

画面にその要素がちゃんとあるかを確認する。
「ボタンが表示されているか」「エラー文が出ているか」などでよく使います。

2. テキスト内容を確認:.toHaveTextContent()

expect(element).toHaveTextContent("ログイン成功");

要素の中にこの文字が入っているかを確認する。
ラベルやメッセージの内容が正しいかをチェックするのに便利。

3. 無効になっているか確認:.toBeDisabled()

expect(button).toBeDisabled();

ボタンや入力欄が操作できない状態かどうかを確認する。
逆に有効状態を確認したいときは .toBeEnabled() を使います。

まとめ

  • 存在チェック → .toBeInTheDocument()

  • 文字チェック → .toHaveTextContent()

  • 無効チェック → .toBeDisabled()

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?