Using Mocha .only and .skip for Selective Test Execution
How to run or skip specific Mocha test blocks during development and CI, with practical commands and pipeline safeguards.
07 Oct 2025, 03:51 UTC

Problem: Lingering .only blocks CI test suites silently
When developing with Mocha, it’s common to want running a single test or a small group of tests without the noise of the full suite. The .only modifier solves this in the moment, but if it’s left behind after a debugging session, the continuous integration pipeline executes zero tests—masking failures that should have been caught. Conversely, .skip lets you deliberately disable a test block without deleting it, which is useful for known-failing tests or experiments you’re not ready to remove.
Use .only to narrow execution to the immediate describe or it block. It accepts an optional regular expression, so you can filter by name pattern across nested suites. Use .skip to explicitly skip a block; the test code remains in the file but is not executed. Both modifiers are well-established Mocha behavior and work across supported versions, including Mocha 10 and later. They do not require additional dependencies or configuration beyond a standard Mocha setup.
Prerequisites
- A Mocha project initialized with npm init mocha or equivalent
- Test files written using describe and it blocks, as Mocha’s selective execution modifiers annotate those constructs
- Node.js and npm available on your workstation or CI runner, with Mocha installed as a development dependency
- Familiarity with running Mocha via npx mocha or a local ./node_modules/.bin/mocha executable
Focused procedure
Running a single test block with .only:
describe('math operations', () => { describe('addition', () => { .only it('adds two numbers', () => { assert.strictEqual(1 + 1, 2) }) }) describe('subtraction', () => { it('subtracts two numbers', () => { assert.strictEqual(5 - 3, 2) }) }) })When you run npx mocha from the project root, only the it('adds two numbers') inside the nested describe('addition') block executes. The subtraction block and any other suites are suppressed for that run. If you place .only on a top-level describe, every it inside that parent runs exclusively; nested .only does not propagate outward, only the nearest ancestor block containing .only is honored.
Skipping a test block with .skip:
describe('edge cases', () => { .skip it('handles null input', () => { // omitted for now }) it('handles undefined input', () => { assert.deepStrictEqual(handleUndefined(), undefined) }) })The it('handles null input') block is skipped entirely; the second it runs as part of the normal suite. This is useful when a test fails for a known reason you’re not ready to fix, or when you want to keep the test skeleton while working on other assertions.
Using regular expressions with both modifiers:
// Run only it blocks whose description contains api npx mocha --grep api describe.only('api requests', () => { /* only blocks inside, regardless of .only placement */ })The --grep flag is the runtime equivalent; it filters tests by pattern at execution time without modifying source files. Within source, describe.only and it.only apply the modifier to the enclosed block, while npx mocha --grep pattern applies the same kind of filtering externally. Both approaches can be combined: use source-level modifiers for development workflow and --grep for CI pipeline configuration.
Expected checks
- After running npx mocha, examine the output summary. If .only is active, the report should show a single test suite and a single (or few) test pass(es), with all other blocks listed as skipped or simply absent from the run.
- Run npx mocha --grep addition to verify that the filter pattern matches the blocks you expect. For example npx mocha --grep addition should run only tests whose names or parent describes match 'addition'. Compare the passing count against your intention.
- Inspect the runner’s passing, failing, and skipped counts at the bottom of the output. If using .only in CI, the skipped count should be non-zero (reflecting the suppressed blocks) and the passing count should reflect only the intended block. If the output shows 0 tests run, suspect a lingering .only modifier at the suite level.
Comparison: .only vs .skip
| Modifier | Behavior | Typical use case |
|---|---|---|
| .only | Runs only the marked block; all other blocks in the suite are suppressed for that execution. | Focused debugging of a single test or small group during active development. |
| .skip | Explicitly skips the marked block; the code remains in the suite and is not executed. | Known-failing tests, experiments, or temporary disabling without removing code. |
Use .only when you need to isolate execution for quick feedback. Use .skip when you want to keep a test in the suite but prevent it from running, such as while tracking down a flaky failure or maintaining a test for future re-enablement.Recovery and CI safety
Because a lingering .only silently disables the entire suite, make it a habit to remove or replace .only after debugging. A lingering modifier is the most common cause of zero tests executed CI failures that are actually caused by developer workflow artifacts left in the codebase.
Add a quick audit step to your CI configuration to catch lingering .only flags.
grep -rn '.only' test/ && echo 'WARNING: .only detected in test files' || echo 'No lingering .only flags'Alternatively, integrate a lint rule or pre-commit hook that warns when .only or .skip appear in committed test files.
When using nested .only, remember that only the nearest ancestor block containing .only is honored.
Summary
- .only restricts Mocha execution to the marked block; remove it before committing to avoid silent suite-wide skips in CI.
- .skip disables a block without removing it; useful for known-failing tests or temporary experiments.
- Both accept optional regex patterns for broader filtering, both within source code and via the npx mocha --grep pattern flag.
- Use npx mocha --grep pattern to verify filter behavior before committing changes.
- Add a CI audit step to detect lingering .only and prevent silent suite-wide test skips.
- Remember that nested .only does not propagate; only the nearest ancestor block containing .only is honored.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.