Configuring Cypress for Effective Web Testing
After installing Cypress, you need to set it up properly. Good configuration helps your tests run smoothly across different environments. Let’s explore how to set up Cypress for the best results.

Understanding Cypress Configuration Files
Where to Find Configuration Files
Starting with Cypress 10, you’ll store your settings in cypress.config.js
or cypress.config.ts
in your project’s main folder. Here’s what it looks like:
<code>const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
baseUrl: 'https://example.com',
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
setupNodeEvents(on, config) {
<em>// add event listeners here</em>
},
},
})
Moving from Older Versions
Were you using Cypress 9 or earlier? If so, your settings were probably in cypress.json
. Fortunately, upgrading is easy. First, run this command:
<code>npx cypress open
After that, Cypress will find your old settings and offer to move them to the new format automatically.
Essential Cypress Configuration Options
Setting Your Test Website Address
The most important setting is probably the baseUrl
. This tells Cypress which website to test:
<code>module.exports = defineConfig({
e2e: {
baseUrl: 'https://example.com',
},
})
Once you set this up, you can use shorter paths in your tests:
<code><em>// Instead of cy.visit('https://example.com/login')</em>
cy.visit('/login')
As a result, your tests become easier to maintain and you can switch between testing environments quickly.
Setting Screen Size for Tests
Cypress lets you choose default screen dimensions for your tests:
<code>module.exports = defineConfig({
e2e: {
viewportWidth: 1280,
viewportHeight: 720,
},
})
Moreover, you can change screen size within specific tests:
<code><em>// Test for mobile viewport</em>
it('displays mobile menu on small screens', () => {
cy.viewport(375, 667)
cy.visit('/')
cy.get('.mobile-menu-button').should('be.visible')
})
Adjusting Wait Times
Cypress commands wait for elements by default, but sometimes you need more time. Therefore, you can adjust these waiting periods:
<code>module.exports = defineConfig({
defaultCommandTimeout: 5000, <em>// milliseconds</em>
pageLoadTimeout: 30000,
requestTimeout: 10000,
})
For slower websites or networks, higher values help prevent false failures.
Setting Up Different Testing Environments
Creating Multiple Environment Settings
Most projects need testing in different places (development, staging, production). Cypress makes this simple:
<code>module.exports = defineConfig({
e2e: {
env: {
development: {
baseUrl: 'http://localhost:3000',
},
staging: {
baseUrl: 'https://staging.example.com',
},
production: {
baseUrl: 'https://example.com',
}
}
},
})
Afterwards, use these environments in your tests:
<code>const environment = Cypress.env('environment') || 'development'
const config = Cypress.env(environment)
cy.visit(config.baseUrl)
Using Environment Variables in Cypress
Cypress can access environment variables from your system:
<code><em>// Set variable when running Cypress</em>
<em>// CYPRESS_API_URL=https://api.example.com npx cypress run</em>
<em>// Access in tests</em>
cy.request(Cypress.env('API_URL') + '/users')
This keeps sensitive information out of your code repository.
Advanced Cypress Configuration Techniques
Adding Custom Plugins
Plugins add extra features to Cypress. Set them up in your configuration:
<code>module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', {
log(message) {
console.log(message)
return null
},
})
return config
},
},
})
Managing Screenshots and Videos
By default, Cypress takes screenshots when tests fail and records videos during headless runs. You can control these features:
<code>module.exports = defineConfig({
screenshotOnRunFailure: true,
video: true,
videoCompression: 32,
videosFolder: 'cypress/videos',
screenshotsFolder: 'cypress/screenshots',
})
In addition, you might want to turn off videos in some cases:
<code>module.exports = defineConfig({
video: process.env.CI ? false : true,
})
Setting Test File Patterns
You can control which files Cypress sees as tests:
<code>module.exports = defineConfig({
e2e: {
specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}',
excludeSpecPattern: ['**/ignored-tests/**', '**/node_modules/**'],
},
})
Best Practices for Cypress Configuration
Sharing Configuration Between Team Members
Keep settings consistent across your team by:
- First, add
cypress.config.js
to version control - Next, use environment variables for changing values
- Finally, document any special local settings
Settings for Better Performance
Speed up your tests with these options:
<code>module.exports = defineConfig({
numTestsKeptInMemory: 50, <em>// Default is 50</em>
experimentalMemoryManagement: true,
retries: {
runMode: 2, <em>// Retry failed tests in CI</em>
openMode: 0, <em>// Don't retry in dev mode</em>
},
})
Settings for Different Testing Types
Different testing needs require different settings:
<code>module.exports = defineConfig({
e2e: {
<em>// For component testing</em>
includeShadowDom: true,
<em>// For API testing</em>
experimentalSessionAndOrigin: true,
<em>// For accessibility testing</em>
experimentalStudio: true,
},
})
Fixing Configuration Problems
Common Setup Issues
Watch for these frequent problems:
- First, check file paths match your project structure
- Second, use TypeScript to catch errors early
- Lastly, make sure plugins are installed correctly
Finding Configuration Bugs
When you have setup problems:
- First, look at Cypress logs in the console
- Then, print settings with
console.log(Cypress.config())
- Alternatively, run with
DEBUG=cypress:* npx cypress open
for detailed output
Next Steps After Configuration
After setting up:
- Share your config with your team
- Document any special settings
- Consider adding scripts to switch environments easily
Conclusion
Good Cypress setup creates the foundation for solid, reliable tests. By understanding your options and following best practices, you can build a testing system that works well for everyone on your team.
In our next article, we’ll use these settings to create your first real Cypress test using Google.com as an example.
Another Steps to use Cypress:
Check out the other posts in our Cypress series on the blog:
- Introduction to Cypress: Modern JavaScript Testing Framework
- Getting Started with Cypress: Installation and Setup Guide
- Configuring Your Cypress Environment for Optimal Testing
- Creating Your First Cypress Test: Step-by-Step Google Search Example
- Mastering Cypress Test Execution and Debugging Techniques
- Cypress Best Practices: Building Reliable and Maintainable Tests