How to Isolate Tests in Jest
test.only() and test.skip()
2 min read
A Note on the Series:
Fast Fridays 🏎 is a series where you will find fast, short and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays.
When writing unit tests with Jest, I often want to isolate a particular test case. I find this especially true when working within a test suite that has gotten pretty large due to the number of features associated with a particular component.
Instead of taking the manual approach and commenting out the tests cases I want to ignore for the moment, Jest provides two helper methods
test.skip() that makes isolating specific tests a lot simpler.
test.only() method is beneficial if you want to target 🎯 individual tests within a suite. By default, Jest will run all tests within a suite unless instructed otherwise. When a minimum of one test case uses this method, Jest will only run the test cases that reference the
I find myself using
test.only() quite a bit these days. A common workflow for me is to implement a feature and then pinpoint the individual tests that I want to hone in on. This allows me to reduce the amount of output in the terminal and focus on the main task at hand.
test.skip() method is helpful if you want to omit ⏩ certain tests from a suite. When the
.skip() method is used, Jest will skip over the relevant test case when running the test suite. I don't find myself using this method as often as
test.only(), but I still find it to be a beneficial method to have in my back pocket.
test.skip() is used on an individual test.
🚨 Warning: Make sure to remove the
.skip()methods before committing your code. These methods are only meant to be used for debugging purposes and should not be used in final tests.
El Fin 👋🏽
I hope this quick tip can be useful to you the next time you have to write tests. For an exhaustive list of global objects and methods associated with the
test object, take a look at the official documentation .
As always, thank you for reading, and happy coding!