DEV Community

Cover image for How to Configure Environment Variables in Cypress in 2025?
olasperu
olasperu

Posted on

1

How to Configure Environment Variables in Cypress in 2025?

In the ever-evolving world of software testing, Cypress continues to be a popular choice for end-to-end testing in 2025. One powerful feature that enhances Cypress's flexibility and robustness is the use of environment variables. Environment variables allow you to configure and control aspects of your testing environment without hardcoding values. This guide will help you understand how to configure and use environment variables in Cypress effectively.

Understanding Environment Variables

Environment variables are essential for customizing the behavior of your test environments without modifying your codebase. They can be set up for different purposes, like toggling features, controlling test environments, sensitive data management, and more. These variables make tests more manageable and reusable across different scenarios.

Setting Up Environment Variables in Cypress

Step 1: Using cypress.json or cypress.config.js

The primary method of defining environment variables in Cypress is through your configuration files. Depending on your Cypress version, this may either be cypress.json or the more modern cypress.config.js.

In cypress.json, you can set environment variables like this:

{
  "env": {
    "baseUrl": "http://localhost:3000",
    "apiKey": "your-api-key-here"
  }
}
Enter fullscreen mode Exit fullscreen mode

In cypress.config.js, the setup might look like this:

module.exports = {
  env: {
    baseUrl: "http://localhost:3000",
    apiKey: "your-api-key-here"
  }
};
Enter fullscreen mode Exit fullscreen mode

Step 2: Using Environment-Specific Configurations

For different environments like development, staging, and production, it's efficient to create multiple configuration files. This helps in managing environment-specific settings easily. For instance, you could have:

  • cypress.dev.json
  • cypress.staging.json
  • cypress.prod.json

You can then switch between them using the --config-file option:

cypress open --config-file cypress.staging.json
Enter fullscreen mode Exit fullscreen mode

Step 3: Configuring via CLI

Cypress allows setting environment variables directly via the command line, which overrides settings in config files:

cypress open --env baseUrl=http://test-url.com,apiKey=dynamic-api-key
Enter fullscreen mode Exit fullscreen mode

This method is handy for CI/CD pipelines where variables might only be known at runtime.

Step 4: Accessing Environment Variables in Tests

Within your tests, use Cypress.env() to access these variables:

describe('My Test Suite', () => {
  it('should login using API key', () => {
    const apiKey = Cypress.env('apiKey');
    cy.visit(Cypress.env('baseUrl'));
    // Utilize apiKey and baseUrl as needed in your tests
  });
});
Enter fullscreen mode Exit fullscreen mode

Benefits of Using Environment Variables in Cypress

  1. Flexibility: Easily switch between environments without code changes.
  2. Security: Keep sensitive information out of your source code.
  3. Scalability: Manage tests across different deployment pipelines efficiently.

Additional Resources

By properly configuring environment variables in Cypress, you can ensure your tests are robust, adaptable, and secure, aligning perfectly with the latest Norms and practices of testing paradigms in 2025. Happy testing!

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹ī¸

👋 Kindness is contagious

We would appreciate it if you could show your support by leaving a ❤ī¸ or sharing a thoughtful comment on this post if you found it helpful!

Thank you!