Testing recommendations
Our general set of do's and don'ts for testing components and views.
Choose the right selectors
Follow RTL's Guiding Principles and query priorities when choosing right element selectors.
Prioritize accessible and semantic queries (e.g., [role="dialog"]
) followed by data-test-subj
attributes over
other, more complicated and prone to breaking queries (e.g. div > span.title
) whenever possible.
Check out our component-specific testing docs to find the selectors we officially support.
screen.getByRole('dialog'); // react-testing-library
cy.get('[role="dialog"]'); // cypress
driver.findElement(By.cssSelector('[role="dialog"]')); // selenium
Do: Use accessible and semantic queries.
container.querySelector('.euiFlyout'); // react-testing-library
cy.get('.euiFlyout'); // cypress
driver.findElement(By.cssSelector('.euiFlyout')); // selenium
Don't: Query by internal EUI selectors, especially when better selectors are available.
Don't use snapshots
The EUI team strongly discourages snapshot testing, despite its simplicity. Snapshot tests are prone to frequent failures due to the smallest things, like whitespace changes. Developers often update stored snapshots when they see them fail without thinking too much about why they fail.
Tests should tell a story and be considered an instant red flag whenever they fail. They should focus on the important details like the data a component is displaying or if the data is coming from a prop or being dynamically calculated.
Instead, consider writing simple but precise assertion tests.
const { getByText, getByRole } = render(<MyComponent />);
expect(getByText('Hello, World!')).toBeInTheDocument();
expect(getByRole('button')).toHaveTextContent('Save');
Do: