Additional Globals
Explore the tools that Jest provides to help write and run our tests as efficiently as possible.
Looking deeper into Jest globals
To help with writing and running our tests efficiently, Jest provides several additional globals that we can use. These globals allow us to do things like iterate over test cases and run only the test we are working on.
Additional functionality of describe
-
describe.each(table)(name, fn, timeout)
helps to abstract repetitive testing logic that just needs to be run with multiple data sets. The
tableargument is an array of arrays representing the data to pass to the anonymous function (
fn`) and ultimately for us to use in our individual tests. The name is still the descriptor for the test suite, i.e., function or component name, and we have an optional timeout again. -
describe.only(name, fn)
tells Jest to run only this describe block. If a test file has multipledescribe
blocks and we are working on or debugging only one, running all test suites in the file every time is unnecessarily time-consuming. We can usedescribe.only
to avoid this. -
describe.only.each(table)(name, fn)
has the benefits of both the functions above, enabling us to run only the test suite with the dataset we want to pass the function in. -
describe.skip(name, fn)
is the opposite ofdescribe.only
, specifying a test suite that we do not want to run. -
describe.skip.each(table)(name, fn)
is the opposite ofdescribe.only.each
, specifying a test suite and the corresponding dataset that we do not want to run.
Examples
Examples of each of these are included below. If you run the test file, you’ll see that only the blocks with describe.only
and describe.only.each
actually run. This is expected given their functionality. Examples of each are still included to show how they should be used. Feel free to comment out the .only
tests to see the others in action:
Get hands-on with 1200+ tech skills courses.