メインコンテンツにスキップ

要素内のクエリ

withingetQueriesForElement のエイリアス)は DOM 要素を取り、それを生のクエリ関数にバインドして、コンテナーを指定せずに使用できるようにします。この API に基づいて構築されたライブラリに推奨される方法であり、React Testing Library と Vue Testing Library で内部的に使用されています。

例: 「messages」と呼ばれるセクション内だけで「hello」というテキストを取得するには、次のようにできます。

import {within} from '@testing-library/dom'

const messages = document.getElementById('messages')
const helloMessage = within(messages).getByText('hello')