0
0
Cypresstesting~20 mins

Test configuration per environment in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of environment-specific baseUrl in Cypress
Given the following Cypress configuration files for two environments, what will be the baseUrl used when running tests with the command cypress run --env configFile=staging?
Cypress
/* cypress.config.js */
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      const envConfig = require(`./cypress.env.${config.env.configFile}.json`)
      return {...config, ...envConfig}
    },
    baseUrl: 'http://localhost:3000'
  }
})

/* cypress.env.staging.json */
{
  "baseUrl": "https://staging.example.com"
}

/* cypress.env.production.json */
{
  "baseUrl": "https://www.example.com"
}
Ahttps://staging.example.com
Bhttp://localhost:3000
Chttps://www.example.com
Dundefined
Attempts:
2 left
💡 Hint
Check how the configFile environment variable affects the loaded JSON file.
assertion
intermediate
1:30remaining
Correct assertion for environment-specific API endpoint
You want to assert that the API endpoint URL used in your Cypress test matches the environment-specific value. Which assertion correctly verifies that the API URL is set to the production endpoint when running with --env configFile=production?
Cypress
const apiUrl = Cypress.env('apiUrl')

// Production endpoint is https://api.example.com
Aassert.equal(apiUrl, 'http://localhost:3000')
Bexpect(apiUrl).to.equal('https://api.example.com')
Cexpect(apiUrl).to.be.undefined
Dassert.isTrue(apiUrl === 'https://staging.example.com')
Attempts:
2 left
💡 Hint
Use the correct assertion style and check the expected URL for production.
🔧 Debug
advanced
2:30remaining
Debug failing environment variable in Cypress test
A test fails because the environment variable apiKey is undefined when running tests with --env configFile=qa. The cypress.env.qa.json file contains { "apiKey": "12345" }. What is the most likely cause?
Cypress
/* cypress.config.js */
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      const envConfig = require(`./cypress.env.${config.env.configFile}.json`)
      return {...config, ...envConfig}
    }
  }
})
AThe apiKey variable is not accessed via Cypress.env('apiKey')
BThe cypress.env.qa.json file is missing the apiKey property
CThe test command did not include --env configFile=qa
DThe setupNodeEvents function does not merge envConfig correctly
Attempts:
2 left
💡 Hint
Check the test run command and environment variable usage.
🧠 Conceptual
advanced
2:00remaining
Best practice for managing sensitive environment variables in Cypress
Which approach is the safest and most recommended way to manage sensitive environment variables like API keys across different environments in Cypress?
AUse OS environment variables and load them in cypress.config.js without committing keys to version control
BStore sensitive keys directly in cypress.env.{env}.json files committed to version control
CHardcode sensitive keys in test files for easy access
DInclude sensitive keys in the baseUrl property in cypress.config.js
Attempts:
2 left
💡 Hint
Think about security and version control best practices.
framework
expert
3:00remaining
Configuring Cypress to run tests with multiple environment configurations sequentially
You want to run your Cypress tests sequentially for 'development', 'staging', and 'production' environments, each with its own config file (cypress.env.development.json, etc.). Which approach correctly sets this up in a Node.js script?
Cypress
const { exec } = require('child_process')

// Fill in the missing code to run tests sequentially for each environment
A['development', 'staging', 'production'].map(env => exec(`cypress run --env configFile=${env}`))
B
async function run() {
  for (const env of ['development', 'staging', 'production']) {
    await exec(`cypress run --env configFile=${env}`)
  }
}
run()
C['development', 'staging', 'production'].forEach(env => exec(`cypress run --env configFile=${env}`))
D
const runEnv = env => new Promise((res, rej) => {
  exec(`cypress run --env configFile=${env}`, (err) => err ? rej(err) : res())
})

async function run() {
  for (const env of ['development', 'staging', 'production']) {
    await runEnv(env)
  }
}
run()
Attempts:
2 left
💡 Hint
Consider how to wait for each exec command to finish before starting the next.