| | |

Jest 21 ๐Ÿƒ Test Filtering โ€” .only, .skip, .todo, and test.failing

โฑ๏ธ Estimated read: 13 min | |

Jest runs every test it discovers by default, and that is usually what you want from a test suite. But the moment you are debugging one failing test, or you are waiting on a slow integration test, or you want to document an expected failure without failing the build, you need a way to narrow the set of tests that actually run. Jest’s filtering modifiers are the answer: .only, .skip, .todo, .failing, plus the test.each family and the test name filters on the command line.

These modifiers are not just conveniences โ€” they are how a test suite stays honest about what is and is not being run. A .only left in a file silently disables the rest of the suite, and Jest has a config flag specifically to prevent that. A .todo records work without executing it. A .failing test asserts that the code is broken, and it turns green when the code is fixed. This chapter covers all four modifiers, the it alias, the command-line filters that work on top of them, and the --onlyChanged and --findRelatedTests selectors that skip the source-code analysis.

Key point: .only, .skip, .todo, and .failing change which tests execute and what counts as a pass, and they compose โ€” a .skip inside a .only is still skipped โ€” so the safe mental model is that the most restrictive modifier wins.


Why test filtering exists

The debugging problem. When a suite has five hundred tests and one is failing, running the whole suite for every edit is wasteful. .only marks the single test or suite to run and skips everything else in the file. The feedback loop shrinks from the whole suite to the one case under investigation, which makes the edit-test cycle fast enough to be useful.

The expected-failure problem. Some code is known to be broken. The team wants a test that documents the bug and runs the assertion, but does not fail CI until the bug is fixed. .failing is exactly that: it runs the test, and it passes when the assertion fails and fails when the assertion passes. The moment the bug is fixed, the test flips to red and the team removes the .failing modifier.

The not-yet-written problem. A .todo entry records a test that exists only as a name. It shows in the test list as skipped, it does not run, and it communicates intent without pretending the test exists. It is the honest placeholder.

The slow-test problem. An integration test that hits a real database is too slow for the default loop. .skip marks it skipped locally, and CI runs the full set with a flag that overrides the skip. The modifier is the local default, and the override is the escape hatch.

The accident problem. A .only left in a committed file silently reduces the suite to one test, and the build still passes. The --forbidOnly flag in CI turns that into a hard failure, which is the guard against the most common filtering mistake.


a. .only โ€” run only this

.only restricts the run to the marked test or suite. Everything else in the file is reported as skipped, not as passed, and a suite-level .only runs every test inside the block.

// math.test.js
describe("math", () => {
  // Run only this test; the rest of the file is skipped
  test.only("adds two numbers", () => {
    expect(1 + 1).toBe(2);
  });

  test("subtracts two numbers", () => {
    expect(2 - 1).toBe(1); // reported as skipped, not passed
  });
});

The suite form restricts to the whole block. It is the common form when one describe block is under investigation and the sibling blocks are not.

describe.only("the block under investigation", () => {
  test("case one", () => {
    expect(true).toBe(true);
  });

  test("case two", () => {
    expect(1).toBe(1);
  });
});

.only composes by narrowing, and the most restrictive modifier wins. A .skip inside a .only block is still skipped โ€” .only decides which blocks run, and .skip decides which tests within them are excluded.

describe.only("runs, but this test does not", () => {
  test.only("runs", () => {
    expect(1).toBe(1);
  });

  // Still skipped: the skip wins inside the only block
  test.skip("skipped", () => {
    expect(2).toBe(2);
  });
});

b. .skip โ€” do not run this

.skip excludes a test or suite from the run. The excluded test is reported as skipped, it does not execute its body, and it does not run its hooks.

describe("integration", () => {
  // The body never runs, and beforeEach does not run for it
  test.skip("hits the real database", async () => {
    const rows = await db.query("SELECT 1");
    expect(rows).toHaveLength(1);
  });

  test("runs against the fake", () => {
    expect(fakeDb.query()).toBeDefined();
  });
});

There is an alias, xtest and xdescribe, that is equivalent to the .skip form. It exists for the tests that follow the test/describe convention in older codebases.

// These two are equivalent
test.skip("a", () => {});
xtest("a", () => {});

A skipped test can still be conditionally run. The test.skip.each and test.skipIf(condition) forms make the skip a function of the environment rather than a permanent annotation.

// Skip only on Windows
test.skipIf(process.platform === "win32")("posix-only", () => {
  expect(path.sep).toBe("/");
});

c. .todo โ€” not written yet

.todo records a test that is planned but not implemented. It takes only a name โ€” there is no callback โ€” and it shows in the output as a todo item, distinct from a skipped test.

describe("checkout", () => {
  // Records the intent; nothing runs
  test.todo("applies a coupon");

  test("computes a total", () => {
    expect(total([1, 2])).toBe(3);
  });
});

The distinction from .skip is semantic and visible in the reporter. A skipped test has a body that was deliberately not run. A todo has no body at all, and the reporter lists it separately. test.todo is the honest way to leave a placeholder without writing an empty assertion that passes.

// Wrong: this passes and hides the missing work
test("applies a coupon", () => {
  expect(true).toBe(true);
});

// Right: the reporter lists it as todo
test.todo("applies a coupon");

d. test.failing โ€” assert that it is broken

test.failing inverts the outcome. The test runs its assertions, and it passes when at least one assertion throws and fails when every assertion passes. It is the way to commit a test for a known bug without breaking CI.

// Known bug: formatDate drops the timezone suffix
test.failing("formats a date with a timezone", () => {
  // Fails today because the suffix is missing.
  // When the bug is fixed, this test fails and must lose .failing.
  expect(formatDate(new Date("2024-01-01T00:00:00Z"))).toBe("2024-01-01T00:00:00Z");
});

The semantics are precise: the test must throw. If the assertion passes, the test is reported as failed, which is the signal that the bug is fixed and the modifier should be removed. A test.failing that throws for an unrelated reason โ€” a typo, a missing import โ€” also counts as passing, so the test body should be reviewed when the modifier is added.

// Failing test inside a failing suite is still failing:
// the inner outcome is inverted once, at the test level
describe("known bugs", () => {
  test.failing("drops the suffix", () => {
    expect(formatDate(date)).toContain("Z"); // throws today โ†’ passes
  });
});

e. Command-line filtering on top of the modifiers

The modifiers are static, and Jest also filters at run time. A positional argument is a regex match against the test’s full name, which is the describe path plus the test name.

# Run only tests whose full name contains "checkout"
jest checkout

# Regex form
jest "checkout.*total"

# Only test files that changed since the last commit
jest --onlyChanged

# Only tests related to a given source file
jest --findRelatedTests src/cart.js

The --onlyChanged and --findRelatedTests selectors use the dependency graph, not the test names. --onlyChanged compares the working tree to the last git commit and runs the tests that touch changed files. --findRelatedTests takes one or more file paths and runs the tests that import them, which is the fast path when a single source file is edited.

# Run the tests that import the module you just edited
jest --findRelatedTests src/utils/formatDate.js

The CI guard against a committed .only is --forbidOnly. Without it, a stray .only reduces the suite and the build still passes. With it, the run fails with an explicit error.

# In CI: fail the build if any .only is present
jest --ci --forbidOnly

Complete Example Session

// ============================================
// PART 1: THE SUITE UNDER TEST
// ============================================
// checkout.test.js
describe("checkout", () => {
  test("computes a total", () => {
    expect(total([1, 2])).toBe(3);
  });

  test("applies a coupon", () => {
    expect(applyCoupon(10, "SAVE2")).toBe(8);
  });

  test("rejects an expired coupon", () => {
    expect(() => applyCoupon(10, "EXPIRED")).toThrow();
  });
});

// ============================================
// PART 2: .only DURING DEBUGGING
// ============================================
describe("checkout", () => {
  test.only("computes a total", () => {
    expect(total([1, 2])).toBe(3); // only this test runs
  });

  test("applies a coupon", () => {
    expect(applyCoupon(10, "SAVE2")).toBe(8); // skipped
  });
});

// ============================================
// PART 3: .skip FOR THE SLOW TEST
// ============================================
describe("checkout", () => {
  test.skip("hits the payment provider sandbox", async () => {
    const charge = await sandbox.charge(1000); // never runs
    expect(charge.status).toBe("ok");
  });

  test("computes a total", () => {
    expect(total([1, 2])).toBe(3);
  });
});

// ============================================
// PART 4: .todo FOR THE MISSING TEST
// ============================================
describe("checkout", () => {
  test.todo("applies a percentage coupon");

  test("computes a total", () => {
    expect(total([1, 2])).toBe(3);
  });
});

// ============================================
// PART 5: test.failing FOR THE KNOWN BUG
// ============================================
test.failing("formats a date with a timezone", () => {
  expect(formatDate(new Date("2024-01-01T00:00:00Z")))
    .toBe("2024-01-01T00:00:00Z"); // throws today
});

// ============================================
// PART 6: THE CONDITIONAL SKIP
// ============================================
test.skipIf(process.env.CI !== "true")("slow integration", async () => {
  // Runs in CI, skipped locally
  expect(await slowProbe()).toBe("ok");
});

// ============================================
// PART 7: THE .each FILTER
// ============================================
test.each([1, 2, 3])("doubles %i", (n) => {
  expect(n * 2).toBeLessThanOrEqual(6);
});

// ============================================
// PART 8: THE COMMAND-LINE FILTER
// ============================================
// jest --findRelatedTests src/utils/formatDate.js
// runs the formatDate test file and nothing else

// ============================================
// PART 9: THE CI GUARD
// ============================================
// jest --ci --forbidOnly
// fails the build if any .only remains in the tree

// ============================================
// PART 10: THE COMPOSITION RULE
// ============================================
describe.only("narrowed to this block", () => {
  test.only("runs", () => {
    expect(1).toBe(1);
  });

  test.skip("still skipped", () => {
    expect(2).toBe(2); // the skip wins inside the only block
  });
});

The parts covered the suite under test, the .only narrowing, the .skip exclusion, the .todo placeholder, the test.failing inversion, the conditional skip, the .each filter, the dependency-graph selectors, the --forbidOnly guard, and the composition rule where the most restrictive modifier wins.


Quick Reference

The Four Modifiers

ModifierRuns the bodyOutcome
.onlyYes, and skips the restNormal pass/fail
.skipNoReported skipped
.todoNo (no body)Listed as todo
.failingYesPasses when it throws

Aliases and Conditional Forms

FormEffect
xtest / xdescribeAlias for .skip
test.skipIf(cond)Skip when the condition is true
test.runIf(cond)Run only when the condition is true
test.todo(name)No callback allowed

Command-Line Selectors

FlagEffect
jest <regex>Match the full test name
--onlyChangedTests that touch changed files
--findRelatedTests <file>Tests that import the file
--forbidOnlyFail if any .only is present

Best Practices

โœ… Do This:

// Use .only during local debugging
test.only("the case under investigation", () => {});    // โœ…

// Use .todo for unwritten tests
test.todo("applies a percentage coupon");                // โœ…

// Use test.failing for a known bug
test.failing("drops the suffix", () => {});              // โœ…

// Guard CI against a committed .only
jest --ci --forbidOnly                                   // โœ…

// Use skipIf for environment-specific tests
test.skipIf(!process.env.CI)("slow probe", () => {});    // โœ…

โŒ Don’t Do This:

// Commit a .only
test.only("this disables the suite", () => {});          // โŒ

// Write an empty assertion as a placeholder
test("coupon", () => { expect(true).toBe(true); });      // โŒ

// Leave test.failing after the bug is fixed
test.failing("now works", () => { expect(fixed()).toBe(1); }); // โŒ

// Assume .only overrides .skip
test.only("x", () => {}); test.skip("y", () => {});      // โŒ (y stays skipped)

Common Pitfalls

PitfallWhy It HappensFix
Suite silently reducedA committed .only--forbidOnly in CI
Placeholder that passesEmpty assertion instead of .todoUse test.todo
Known bug never revisited.failing left in placeRemove when the test flips
Skip does not composeAssumed .only overrides it.skip wins inside .only
Hooks run on a skipped testMisread of the lifecycleHooks are skipped too
Conditional skip never runsWrong sign on the conditionskipIf vs runIf
Regex filter too broadPositional arg is a regexQuote and anchor it

Real-World Examples

1. Debug one test

test.only("reproduces the bug", () => { ... });

2. Skip a slow integration test locally

test.skipIf(!process.env.CI)("real database", async () => { ... });

3. Placeholder for unwritten work

test.todo("applies a percentage coupon");

4. Known bug under contract

test.failing("drops the suffix", () => { expect(formatDate(d)).toContain("Z"); });

5. Run only the related tests

jest --findRelatedTests src/utils/formatDate.js

6. Run only the changed tests

jest --onlyChanged

7. Match by name

jest checkout

8. CI guard

jest --ci --forbidOnly

9. Conditional run

test.runIf(process.env.CI)("only in CI", () => { ... });

10. Skip alias

xtest("the old form", () => { ... });

Visual

The Modifier Matrix

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  MODIFIER      BODY RUNS    OUTCOME                     โ”‚
โ”‚                                                         โ”‚
โ”‚  (none)        yes          pass or fail                โ”‚
โ”‚  .only         yes          pass or fail, rest skipped  โ”‚
โ”‚  .skip         no           reported skipped            โ”‚
โ”‚  .todo         no body      listed as todo              โ”‚
โ”‚  .failing      yes          passes when it throws       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Composition Rule

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  describe.only(...)        โ† decides which blocks run   โ”‚
โ”‚    test.only(...)          โ† runs                       โ”‚
โ”‚    test.skip(...)          โ† still skipped              โ”‚
โ”‚                                                         โ”‚
โ”‚  Most restrictive modifier wins at each level           โ”‚
โ”‚  (block level: only; test level: skip)                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

test.failing Inversion

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  NORMAL TEST              test.failing TEST             โ”‚
โ”‚                                                         โ”‚
โ”‚  assertion passes  โ†’ โœ…    assertion passes  โ†’ โŒ       โ”‚
โ”‚  assertion throws  โ†’ โŒ    assertion throws  โ†’ โœ…       โ”‚
โ”‚                                                         โ”‚
โ”‚  The bug fix flips the test from green to red,          โ”‚
โ”‚  which is the signal to remove .failing                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The CI Guard

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  LOCAL DEV                    CI                        โ”‚
โ”‚                                                         โ”‚
โ”‚  jest                         jest --ci --forbidOnly    โ”‚
โ”‚  .only allowed                .only โ†’ build fails       โ”‚
โ”‚                                                         โ”‚
โ”‚  The same tree passes locally and fails in CI           โ”‚
โ”‚  the moment a .only is committed                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Narrow to one test.only
Exclude a test.skip
Record unwritten work.todo
Assert a known bugtest.failing
Skip aliasxtest / xdescribe
Conditional skipskipIf / runIf
Name filterPositional regex
Change filter--onlyChanged
Related filter--findRelatedTests
CI guard--forbidOnly
CompositionMost restrictive wins

Key takeaways:

  • .only narrows the run to the marked test or block. Everything else in the file is reported as skipped, and a suite-level .only runs every test inside it. It is the debugging tool, not a permanent annotation.
  • .skip excludes a test and its hooks. The body never runs, and beforeEach does not run for it. The alias is xtest/xdescribe, and the conditional form is skipIf/runIf.
  • .todo is a name without a body. It cannot take a callback, it shows separately in the reporter, and it is the honest placeholder that an empty passing assertion is not.
  • test.failing inverts the outcome. It passes when the assertion throws and fails when it passes, which is the contract for a known bug. The bug fix flips the test to red, and the modifier is then removed.
  • The modifiers compose by restriction. A .skip inside a .only block is still skipped, because the block-level modifier decides which blocks run and the test-level modifier decides which tests within them are excluded.
  • --forbidOnly is the CI guard. Without it, a committed .only silently reduces the suite and the build still passes.

Remember: The filtering modifiers change both which tests execute and what counts as a pass, and they are not interchangeable. Use .only to narrow during debugging, .skip to exclude a test that cannot run in the current environment, .todo to record work that has not been written, and test.failing to commit a test for a known bug. Guard the CI run with --forbidOnly so a stray .only cannot silently shrink the suite.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!