Best Practices for Testing with Jest
Learn best practices for creating and maintaining Jest tests.
We'll cover the following...
Know what is being tested
This may seem obvious. Of course, we write our tests in such a way that we inherently think that we know what is being tested, but that doesn't mean that we do. There are a few things we can do to ensure that what we think is being tested is actually what is being tested. As we know, our test suites are only as helpful as we write them to be.
Accurately describe individual tests
The importance of accurate describe
and it
statements cannot be overstated. Neither can the ease with which we can accidentally inaccurately describe our tests. Take a button that makes an API call, for instance. If we state that we are testing whether clicking the button triggers the API call, we must ensure that we are testing that the function making the request has been called. We cannot merely test that we can click it and it succeeds or that the button exists. Similarly, if we test that an API call doesn’t throw, we must ensure that the API call is in fact being made and that the test isn't passing because we've inadvertently not actually made it to the API call.
Our descriptions must match what is actually being tested. Inaccurate or misleading descriptors can lead to unknowingly missing ...