Home

Run a Single Jest Test

Learn how to run a single test within a file, how to target a series of tests within a file, or how to only target a single file when using Jest.

Let’s take a look at how you can target running a single test in Jest.

Understanding How Jest Runs Tests

To set the context here, the most important thing to understand is that Jest loads test files and runs them in parallel. Jest only knows what it should and shouldn’t run at a global scale based on what you tell it to do through the command. More on this later.

Methods for Running a Single Test within a Test File

Within a single file, you have multiple options to run a specific test.

Targeting a Specific Test

Running single tests in Jest means using the test method. You can target a single test using test.only:

test.only("This will run", () => {
// Do something ...
});

test("This will NOT run", () => {
// This will be skipped ...
});

Being that it is an alias of test, you can also use it.only(). Or, as a shorter alias, you can use fit:

fit("This will run", () => {
// Do something ...
});

it("This will NOT run", () => {
// This will be skipped ...
});

Targeting a Group of Tests

Tests are typically grouped together using describe. If you want to run all tests within a single describe block, but no other tests in the file you can use describe.only():

describe.only("These tests will run", () => {
// All tests in here will run ...
});

describe("These tests will NOT run", () => {
// All these tests will be skipped ...
});

You can also use fdescribe as an alias:

fdescribe("These tests will run", () => {
// All tests in here will run ...
});

describe("These tests will NOT run", () => {
// All these tests will be skipped ...
});

Targeting a Single Test File

In some cases, you may be concerned with targeting a single test within a single test file. I have another post that goes into detail about running a subset of test files within a Jest test suite. You can pair the methods in that post with the methods above to pinpoint a single test throughout your suite.

References

When I was first working to solve this problem, this StackOverflow question and answer helped me get started. There is additional context within the answers that may help.

Let's Connect

Keep Reading

Delete Entire Word in Terminal and iTerm2

Ever find yourself holding down the Delete key on the command line? Here's a way to work faster.

Feb 08, 2021

Pro Tip: Use “1.” for Every Ordered List Item when Writing Markdown

A quick tip for making ordered lists in markdown easier to write.

May 31, 2022

How to Upload Jest Image Snapshot Diffs to S3

Add snapshots from failed to S3 to enable running jest-image-snapshot on continuous integration server.

Jan 26, 2022