testing-library/prefer-query-by-disappearance

January 5, 2026 ยท View on GitHub

๐Ÿ“ Suggest using queryBy* queries when waiting for disappearance.

๐Ÿ’ผ This rule is enabled in the following configs: badge-angular angular, badge-dom dom, badge-marko marko, badge-react react, badge-svelte svelte, badge-vue vue.

Rule Details

This rule enforces using queryBy* queries when waiting for disappearance with waitForElementToBeRemoved.

Using queryBy* queries in a waitForElementToBeRemoved yields more descriptive error messages and helps to achieve more consistency in a codebase.

// TestingLibraryElementError: Unable to find an element by: [data-testid="loader"]
await waitForElementToBeRemoved(screen.getByTestId('loader'));

// The element(s) given to waitForElementToBeRemoved are already removed.
// waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.
await waitForElementToBeRemoved(screen.queryByTestId('loader'));

Example of incorrect code for this rule:

await waitForElementToBeRemoved(() => screen.getByText('hello'));
await waitForElementToBeRemoved(() => screen.findByText('hello'));

await waitForElementToBeRemoved(screen.getByText('hello'));
await waitForElementToBeRemoved(screen.findByText('hello'));

Examples of correct code for this rule:

await waitForElementToBeRemoved(() => screen.queryByText('hello'));
await waitForElementToBeRemoved(screen.queryByText('hello'));