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.
Only the first test runs. The same pattern works with it.only and describe.only.
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.
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.
To target one test inside one file, combine the file path with a name filter.
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.
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.
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
.onlyfor fast temporary debugging in a local branch - use CLI filters for repeatable commands and safer daily work
- use
--runTestsByPathwhen 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:
The double dash passes the later flags through to Jest.
Common Pitfalls
- Leaving
test.onlyordescribe.onlyin 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 throughnpm testprevents 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, ordescribe.onlyfor temporary focused execution in code. - Prefer CLI filters such as
-tand--runTestsByPathwhen 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.

