Jest
Testing
JavaScript
Software Development
Web Development

How do I run a single test using Jest?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Running one Jest test at a time is useful when you are debugging failures or shortening feedback loops during development. Jest supports this in two main ways: by focusing a test in code, or by filtering from the command line without modifying the test file.

Focus a Test Temporarily in Code

The most direct option is .only. It tells Jest to run just that test or suite.

javascript
1test.only('adds two numbers', () => {
2  expect(1 + 1).toBe(2);
3});
4
5test('multiplies two numbers', () => {
6  expect(2 * 3).toBe(6);
7});

Only the first test runs. The same pattern works with it.only and describe.only.

javascript
1describe.only('math helpers', () => {
2  test('adds values', () => {
3    expect(4 + 5).toBe(9);
4  });
5
6  test('subtracts values', () => {
7    expect(7 - 2).toBe(5);
8  });
9});

This is fast and convenient while debugging, but it changes source code, which means it is easy to commit accidentally if you are not careful.

Filter from the Command Line

For safer day-to-day work, the CLI is usually better. If you know the test name, use -t or --testNamePattern.

bash
npx jest -t "adds two numbers"

Jest matches against the test name, so this can target one test or a small set of related tests.

If you want to limit execution to one file, provide its path.

bash
npx jest src/utils/math.test.js

To target one test inside one file, combine the file path with a name filter.

bash
npx jest src/utils/math.test.js -t "adds two numbers"

This is often the cleanest option because it leaves the test source untouched and works well in shell history or editor tasks.

Use --runTestsByPath When Path Matching Gets Ambiguous

In larger repositories, Jest path matching can be broader than expected. --runTestsByPath tells Jest to treat the argument as an exact test file path.

bash
npx jest --runTestsByPath src/components/Button.test.js

That is useful when multiple files have similar names or when regular path matching behaves differently across shells.

If you are in watch mode, you can also interactively narrow the run set instead of restarting Jest from scratch.

bash
npx jest --watch

From there, Jest lets you filter by filename or test name through the watch interface.

Pick the Right Strategy for the Situation

A practical rule is:

  • use .only for fast temporary debugging in a local branch
  • use CLI filters for repeatable commands and safer daily work
  • use --runTestsByPath when file targeting must be exact

For CI, avoid focused tests entirely. CI should run the full intended suite, not a locally narrowed subset.

If your package uses npm scripts, the command typically becomes:

bash
npm test -- --runTestsByPath src/utils/math.test.js -t "adds two numbers"

The double dash passes the later flags through to Jest.

Common Pitfalls

  • Leaving test.only or describe.only in committed code makes the suite look green while most tests are skipped.
  • Using a test-name filter that matches multiple tests can run more cases than expected.
  • Forgetting the extra -- when invoking Jest through npm test prevents the flags from reaching Jest.
  • Assuming file path matching is exact can cause surprise when several similarly named files exist.
  • Debugging with a focused test only and never rerunning the broader suite can hide regressions elsewhere.

Summary

  • Use test.only, it.only, or describe.only for temporary focused execution in code.
  • Prefer CLI filters such as -t and --runTestsByPath when you want repeatable commands.
  • Combine file paths and test-name filters to target one specific test cleanly.
  • Be careful not to commit focused tests.
  • Always rerun the wider suite before considering the change complete.

Course illustration
Course illustration

All Rights Reserved.