cypress.config.js settings - Build an Automation Script
/// <reference types="cypress" /> // cypress.config.js const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { baseUrl: 'https://example.com', defaultCommandTimeout: 10000, viewportWidth: 1280, viewportHeight: 720 } }) // cypress/e2e/configSettings.cy.js describe('Cypress config settings test', () => { beforeEach(() => { cy.visit('/') // uses baseUrl from config }) it('should have correct URL and viewport size', () => { cy.url().should('eq', 'https://example.com/') cy.viewport().then(viewport => { // viewport() does not return size, so use cy.window to check cy.window().then(win => { expect(win.innerWidth).to.equal(1280) expect(win.innerHeight).to.equal(720) }) }) }) })
The cypress.config.js file sets the baseUrl, defaultCommandTimeout, and viewport size. This centralizes configuration so tests do not hardcode these values.
The test visits the baseUrl by calling cy.visit('/'), which uses the configured baseUrl automatically.
We assert the URL is exactly the baseUrl with a trailing slash.
To verify viewport size, we check the window's innerWidth and innerHeight properties because cy.viewport() is a command to set viewport, not get it.
This approach follows best practices by using configuration, avoiding hardcoded URLs, and verifying settings through assertions.
Now add tests that verify the configuration works with three different baseUrls using environment variables
Practice
cypress.config.js file in a Cypress project?Solution
Step 1: Understand the role of
This file is used to configure how Cypress runs tests, including settings like base URL and test file paths.cypress.config.jsStep 2: Differentiate from other files
Test cases are written in separate test files, not in the config file. Installation and running commands happen outside this file.Final Answer:
To set configuration options like base URL and test locations for Cypress tests -> Option AQuick Check:
Config file sets options = D [OK]
- Confusing config file with test files
- Thinking config installs Cypress
- Believing config runs tests manually
defineConfig in cypress.config.js?Solution
Step 1: Recall the modern Cypress config syntax
Since Cypress 10+, the config file usesdefineConfigand exports it as default with ES module syntax.Step 2: Check the correct structure
Thee2eobject wraps settings likebaseUrl. export default defineConfig({ e2e: { baseUrl: 'http://localhost:3000' } }) matches this pattern exactly.Final Answer:
export default defineConfig({ e2e: { baseUrl: 'http://localhost:3000' } }) -> Option DQuick Check:
Use defineConfig with export default = B [OK]
- Forgetting to wrap settings inside e2e object
- Using CommonJS export instead of ES module
- Not using defineConfig function
cypress.config.js snippet, what will be the base URL used during test runs?
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
baseUrl: 'https://example.com',
specPattern: 'cypress/e2e/**/*.cy.js'
}
})Solution
Step 1: Identify the baseUrl property in config
The config setsbaseUrlto 'https://example.com' inside thee2eobject.Step 2: Understand baseUrl usage
This URL is used as the root for all relative URLs in tests, so it will be 'https://example.com'.Final Answer:
https://example.com -> Option BQuick Check:
baseUrl set to https://example.com = C [OK]
- Confusing specPattern with baseUrl
- Assuming default localhost URL
- Missing that baseUrl is inside e2e
cypress.config.js but Cypress throws an error when running tests:
import { defineConfig } from 'cypress'
export default {
e2e: {
baseUrl: 'http://localhost:8080'
}
}
What is the likely cause of the error?Solution
Step 1: Check the export syntax
The config is exported as a plain object, but Cypress expects the config wrapped indefineConfig().Step 2: Understand Cypress config requirements
WithoutdefineConfig, Cypress cannot properly parse the config, causing errors.Final Answer:
Missing call to defineConfig wrapping the config object -> Option CQuick Check:
Always wrap config in defineConfig() = A [OK]
- Placing baseUrl outside e2e
- Confusing export default with CommonJS
- Thinking baseUrl must be IP address
API_KEY in cypress.config.js so it can be accessed in your tests. Which is the correct way to add it inside the config?Solution
Step 1: Locate where environment variables go
Environment variables for e2e tests go inside theenvobject nested within thee2econfig.Step 2: Verify correct property names
The key must beenv, notenvironment, and it must be insidee2e.Final Answer:
export default defineConfig({ e2e: { env: { API_KEY: '12345' } } }) -> Option AQuick Check:
Use e2e.env for env vars = A [OK]
- Placing env outside e2e
- Using wrong key name like environment
- Adding env vars directly under e2e without env object
Ready to prove it?
Pass the test and this pattern moves unknown
