This article highlights common suboptimal patterns and anti-patterns encountered when writing tests with React Testing Library. It provides actionable advice on improving test maintainability and readability by avoiding legacy practices like manual cleanup or incorrect variable naming.
Key points
Utilize official ESLint plugins to automatically identify and enforce best practices in your test suite.
Avoid naming the return value of 'render' as 'wrapper' as it is a legacy convention from older testing tools.
Manual 'cleanup' calls are generally unnecessary as modern test frameworks handle this automatically.
Prefer 'screen' over destructuring query methods to reduce cognitive load and improve code readability.
Explicitly use assertions even when a query method like 'getBy' would throw, to clearly communicate test intent.