Q6 of 48 · Cypress

How do you run a single Cypress test from the command line?

CypressJuniorcypressclifundamentals

Short answer

Short answer: Use `npx cypress run --spec 'cypress/e2e/path/to/file.cy.ts'`. To run a specific test inside a spec, use `it.only(...)` in the code, or `--grep` with `@cypress/grep`. Browser and headed mode are flags: `--browser chrome --headed`.

Detail

Cypress's CLI distinguishes file selection from test selection.

Selecting one spec file uses --spec with a path or glob:

npx cypress run --spec "cypress/e2e/auth/login.cy.ts"
npx cypress run --spec "cypress/e2e/auth/**"

Selecting one test inside a spec is harder out of the box. The two common ways:

  1. .only — annotate the test with it.only('...', () => { ... }). Cypress runs only the annotated test in that spec. Don't commit .only to main; many teams add an ESLint rule to block it.
  2. @cypress/grep — a plugin that adds tag-based filtering. After installing, --env grep="login" runs tests whose names match. Combine with tags like {tags: '@smoke'} for tagged subsets.

Other useful flags: --browser chrome (or firefox, edge), --headed to see the browser, --config-file cypress.staging.config.ts for environment-specific configs. --record sends results to Cypress Cloud if configured.

For local debugging, npx cypress open launches the interactive runner — pick the spec, watch it run, and time-travel through commands.

// EXAMPLE

# Run all specs
npx cypress run

# Run one spec file
npx cypress run --spec "cypress/e2e/auth/login.cy.ts"

# Run all specs under a folder
npx cypress run --spec "cypress/e2e/checkout/**"

# Headed in Chrome (useful in CI debug)
npx cypress run --browser chrome --headed --spec "cypress/e2e/auth/login.cy.ts"

# With @cypress/grep — only tests tagged @smoke
npx cypress run --env grepTags=@smoke

# Open the interactive runner for local debugging
npx cypress open

// WHAT INTERVIEWERS LOOK FOR

Knowing `--spec` for files and `.only` or `@cypress/grep` for individual tests. Bonus for distinguishing `cypress run` (CI) from `cypress open` (interactive).

// COMMON PITFALL

Reaching for `--spec` to filter by test name — `--spec` is file-level only. Test-level filtering needs `.only` or a grep plugin.