Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cypress-env

Package Overview
Dependencies
Maintainers
0
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cypress-env

Cypress Plugin | Easily manage and streamline your Cypress test scripts across multiple environments with the cypress-env npm library. This lightweight utility simplifies the process of handling different environments (such as test, staging, and productio

  • 1.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
837
increased by7.58%
Maintainers
0
Weekly downloads
 
Created
Source

Cypress plugin to handle multi environment

Easily manage and streamline your Cypress test scripts across multiple environments with the cypress-env npm library. This lightweight utility simplifies the process of handling different environments (such as test, staging, and production) by providing a environment-specific settings in your Cypress tests.

Install

$ npm install cypress-env --save-dev

or as a global module

$ npm install -g cypress-env

Configuration

Code in cypress.config.js:

In your cypress.config.js file:

module.exports = defineConfig({
 e2e: {
  setupNodeEvents(on, config) {
   require('cypress-env')(on, config, __dirname)
  },
 },
})

Create the env.config folder:

Then configure a folder named `env.config' with your environments.json files:

├── cypress
│   ├── e2e
│   ├── fixtures
│   └── support
├── cypress.config.js
├── env.config
│   ├── test.json
│   ├── stage.json
│   └── prod.json
├── node_modules
├── README.md
├── package-lock.json
├── package.json
└── .gitignore

Add your environment.json files:

JSON files must respect this syntax:

//environment.json
{
  "baseUrl": "https://www.google.com",
  "specPattern": "cypress/e2e/**/**.js",
  "excludeSpecPattern": "cypress/e2e/**/toExclude.js",
  "supportFile": "cypress/support/customName.js",
  "env": {
    "var1": "value1",
    "var2": "value2",
    "var3": "value3",
    "envName": "environment"
  }
}
ParameterMandatoryOverwrites value in cypress.config.jsNotes
baseUrlFALSETRUEOverwrites value in cypress.config.js
specPatternFALSETRUEOverwrites value in cypress.config.js
excludeSpecPatternFALSETRUEOverwrites value in cypress.config.js
supportFileFALSETRUENeeds the parameters "supportFile" in the main e2e or component object set to false
envFALSEFALSEOBJ added to values in cypress.config.js

Open or run cypress with the correct environment variables:

Open cypress and inject the envName variables:

npx cypress open -e envName=test

or run cypress and inject the envName variables:

npx cypress run -e envName=test

Results example

Correct configuration

====================================================================================================

Starting plugin: cypress-env

Extracting local configurations from: "path/to/environment.json"

 - baseUrl: "https://www.google.com"
 - specPattern: "cypress/e2e/**/**.js"
 - excludeSpecPattern: "cypress/e2e/**/toExclude.js",
 - supportFile: "cypress/support/customName.js",
 - env: "{
    "var1": "value1",
    "var2": "value2",
    "var3": "value3",
    "envName": "test"
}"

√ Configurations loaded correctly for the environment: < TEST >

====================================================================================================

No configuration specified

====================================================================================================

Starting plugin: cypress-env

√ No environment configuration specified, using basic configuration!

====================================================================================================

Wrong configuration (missing __dirname)

====================================================================================================

Starting plugin: cypress-env

ConfigurationError!
You must specify the "__dirname" element in the config, change the require to:
require("cypress-env")(on, config, __dirname)

====================================================================================================

Little tip for you

In your package.json file create a script like this:

//package.json
{
  "scripts": {
    "cy:test": "npx cypress open -e envName=test",
    "cy:stage": "npx cypress open -e envName=stage",
    "cy:prod": "npx cypress open -e envName=prod"
  }
}

So you'll only have to type this command to open cypress in the correct environment:

npm run cy:test

Compatibility with cypress-aws-secrets-manager

"cypress-aws-secrets-manager" is a plugin that allows a secret stored in the AWS secret manager to be loaded into cypress as an environment variable. To make life easier I added a new key inside ENV: AWS_SECRET_MANAGER_CONFIG.

ParameterMandatoryOverwrites value in cypress.config.jsNotes
AWS_SECRET_MANAGER_CONFIGFALSETRUEOBJ used by cypress-aws-secrets-manager

The secret manager plugin will automatically handle this obj to recover the secret archived on AWS secret manager.

//environment.json
{
  "baseUrl": "https://www.google.com",
  "specPattern": "cypress/e2e/**/**.js",
  "excludeSpecPattern": "cypress/e2e/**/toExclude.js",
  "supportFile": "cypress/support/customName.js",
  "env": {
    "var1": "value1",
    "var2": "value2",
    "var3": "value3",
    "envName": "test",
    "AWS_SECRET_MANAGER_CONFIG": {
      "secretName": "...",
      "profile": "...",
      "region": "..."
    }
  }
}

THE JOB IS DONE!

Happy testing to everyone!

ALEC-JS

🙌 Donate to support my work & further development! 🙌

Keywords

FAQs

Package last updated on 11 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc