top of page

NPM Scripts for Cypress

Updated: Mar 21


Typing full Cypress CLI commands every time gets old fast.


Instead of this:

npx cypress run --spec "cypress/e2e/firstTest.cy.js"npx cypress run --spec "cypress/e2e/firstTest.cy.js" --browser firefox

Create reusable NPM scripts once — then just run:

npm run cypress:smoke-firefox

Cleaner. Faster. Better for CI.


Why Use NPM Scripts?


  • Avoid repeating long terminal commands

  • Standardize execution across the team

  • Simplify CI/CD pipelines

  • Centralize configuration in one place (package.json)


Instead of remembering flags and paths, everything lives under scripts.


Where to Add Them


Inside package.json → scripts section.


Example:

How to Run Them


Single Script

npm run cypress:smoke-firefox

or

npm run cypress:smoke-chrome

Sequential Execution (One After Another)

npm run cypress:smoke-firefox && npm run cypress:smoke-chrome

Or use the combined script:

npm run cypress:smoke-all

&& ensures the second script runs only after the first finishes successfully.


Parallel Execution


If you want both browsers running at the same time:


Linux / Mac

Use single &

npm run cypress:smoke-firefox & npm run cypress:smoke-chrome

Windows

Install:

npm install npm-run-all --save-dev

Then run:

npx npm-run-all cypress:smoke-firefox cypress:smoke-chrome

Syntax Notes


  • Use single quotes ' ' inside package.json commands to avoid escaping issues.

  • Scripts must be separated by commas.

  • Script names are completely custom — choose clear names like:

    • cypress:smoke

    • cypress:regression

    • cypress:chrome

    • cypress:ci


Advanced Use Cases


NPM scripts are not limited to Cypress.


They can run:

  • Application builds

  • API calls (curl)

  • Linting

  • Docker commands

  • Database migrations

  • Full test pipelines


Example CI-friendly script:

"test:ci": "npm run build && npm run cypress:smoke-chrome"

Now your CI pipeline just runs:

npm run test:ci

Instead of copying huge command strings into pipeline YAML.


Why This Matters in Real Projects


In larger teams:

  • DevOps runs one simple command

  • QA doesn’t remember flags

  • Build servers use consistent commands

  • Terminal mistakes are reduced


It’s a small change with big productivity impact.

 
 
bottom of page