TechBuzzPro

Cypress Configuration File: Step-by-Step Setup

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through configuring Cypress using its configuration file (cypress.config.js or cypress.config.ts). You'll learn how to set base URLs, timeouts, viewport sizes, environment variables, and more. By the end, you'll have a tailored Cypress setup that matches your project's needs.

What You'll Need

  • A project with Cypress installed (npm install cypress --save-dev)
  • Node.js (version 18 or higher recommended)
  • A code editor (VS Code, WebStorm, etc.)
  • Basic familiarity with JavaScript or TypeScript

Step-by-Step

  1. Locate or create the configuration file. After installing Cypress, a default cypress.config.js is created in your project root. If it's missing, create it manually. For TypeScript projects, use cypress.config.ts.
  2. Open the file in your editor. The default content looks like this:
    const { defineConfig } = require('cypress');
    
    module.exports = defineConfig({
      e2e: {
        setupNodeEvents(on, config) {
          // implement node event listeners here
        },
      },
    });
    If you're using TypeScript, you'll see import { defineConfig } from 'cypress'; and export default defineConfig({...}).
  3. Set your base URL. Add baseUrl inside the e2e block. This saves you from typing the full URL in every cy.visit() call.
    e2e: {
      baseUrl: 'https://example.com',
      setupNodeEvents(on, config) { ... }
    }
  4. Configure timeouts. Add defaultCommandTimeout (default 4000 ms) and requestTimeout (default 5000 ms) at the top level of the config object. These control how long Cypress waits for commands and HTTP requests.
    module.exports = defineConfig({
      defaultCommandTimeout: 10000,
      requestTimeout: 10000,
      e2e: { ... }
    });
  5. Set the viewport size. Add viewportWidth and viewportHeight (default 1000x660). This is useful for testing specific screen resolutions.
    viewportWidth: 1280,
    viewportHeight: 720,
  6. Define environment variables. Use the env key to store values like API endpoints or test credentials. Access them in tests via Cypress.env('key').
    env: {
      apiUrl: 'https://api.example.com',
      adminUser: 'admin@example.com',
    }
  7. Configure the test file pattern. By default, Cypress looks for spec files in cypress/e2e. You can change this with specPattern inside the e2e block.
    e2e: {
      specPattern: 'cypress/tests/**/*.cy.{js,jsx,ts,tsx}',
    }
  8. Add support for Node events (optional). The setupNodeEvents function lets you tap into Cypress's lifecycle events, such as on('before:browser:launch') or on('task') for custom commands that run in Node.
    setupNodeEvents(on, config) {
      on('task', {
        log(message) {
          console.log(message);
          return null;
        },
      });
    }
  9. Save and restart Cypress. After editing the config, close Cypress (if running) and start it again with npx cypress open or npx cypress run. The new settings take effect immediately.

Common Mistakes

  • Placing options in the wrong block. Top-level options like viewportWidth go at the root, while baseUrl and specPattern go inside e2e (or component for component testing).
  • Forgetting to export the config. If you use module.exports without defineConfig, Cypress may still work, but you lose type checking and autocomplete. Always wrap with defineConfig.
  • Using single quotes inside the config when the file uses double quotes. This is a syntax error. Stick to one style consistently.
  • Overwriting the default setupNodeEvents function. If you add a second one, the first is ignored. Keep only one.
  • Using environment variables without the env key. Putting them at the root level won't make them accessible via Cypress.env().

Tips

  • Use comments to document your config. Especially for timeouts and viewport sizes, future you will appreciate the context.
  • Leverage environment-specific config files. For example, create cypress.config.dev.js and cypress.config.prod.js and switch using the --config-file flag.
  • Use the --config CLI flag for quick overrides. For example, npx cypress run --config viewportWidth=800 overrides the config file without editing it.
  • Take advantage of TypeScript types. If you use a .ts config file, you get autocomplete for all options, reducing typos.
  • Keep sensitive data out of the config file. Use environment variables (like process.env) or Cypress's env option with a cypress.env.json file that is gitignored.

Troubleshooting

  • Cypress doesn't pick up my changes. Make sure you restart Cypress after editing the config. The config is read only when Cypress launches.
  • Getting a syntax error. Check for missing commas, mismatched brackets, or a stray semicolon. Use a linter or run node cypress.config.js to see the exact error.
  • baseUrl not working. Verify you placed it inside the e2e block. Also check that your URL doesn't have a trailing slash unless you intend it.
  • Environment variables are undefined. Ensure they are under the env key, not at the root. Also check that you're using Cypress.env('key') correctly in your tests.
  • Spec files not found. Double-check the specPattern path. Use forward slashes and ensure the glob pattern matches your file extensions.
  • TypeScript config not recognized. Make sure your project has TypeScript installed and that the file is named cypress.config.ts. Also check your tsconfig.json includes Cypress types.

Frequently Asked Questions

Where is the Cypress configuration file located?

By default, it's cypress.config.js (or .ts) in the root of your project. You can specify a different file using the --config-file flag.

Can I use environment variables in the config file?

Yes, you can access system environment variables via process.env inside the config file, and you can define Cypress-specific environment variables under the env key.

What's the difference between baseUrl and env?

baseUrl is used as the prefix for cy.visit() and cy.request() when you pass a relative path. The env object stores arbitrary key-value pairs that you access with Cypress.env().

How do I override the config for a single run?

Use the --config CLI option, e.g., npx cypress run --config defaultCommandTimeout=8000. This merges with the config file values.

Can I have multiple config files for different environments?

Yes, create separate files like cypress.config.staging.js and run with --config-file cypress.config.staging.js. You can also use a single file and conditionally set values based on process.env.NODE_ENV.

Why is my setupNodeEvents not being called?

Make sure you have only one setupNodeEvents function and that it's defined inside the e2e (or component) block. Also verify that you're not accidentally overwriting it in a plugin file (the old plugins/index.js approach is deprecated).