Make assertions specific
When you make an assertion (most often in tests, but also in validation/parsing), make the assertion specific enough that people who see the error understand what the intended behavior is and what about the current state falls short of that intended behavior.
Examples
- You're testing a profile update form, and instead of writing an assertion like `expect(document.body).toMatchSnapshot()` which effectively asserts the entire page, you write `expect(screen.getByText(/success/i)).toBeInTheDocument()`. This makes it clear what the specific expectation is, allowing developers to understand at a glance that the update should produce a success message specific to the profile.
- In data validation, instead of writing `if (!response.ok) throw new Error('user data request failed')` you write `if (!response.ok) throw new Error(`user data request failed with status ${response.status}: ${response.statusText}`)`. Giving as much information as you reasonably can in the error message.
Share this principle