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

2 Methods for Running Multiple Jest Suites in the Same Project

Multiple approaches on running a subset of Jest tests within a project.

Jun 03, 2022

Global Content IDs to Minimize Risk in Changing URLs

Use a unique value within a URL and make the rest arbitrary. This way you have flexibility to change URLs without breaking functionality for your visitors.

Aug 28, 2022

Use Commit Hashes when Sharing GitHub URLs

Sharing GitHub commit hashes directly works for ephemeral uses, but there is a safer way for links that need to last.

Dec 15, 2022