morficus / cypress-dotenv

Cypress plugin that enables compatability with dotenv
53 stars 13 forks source link

How to turn off "prefixed with CYPRESS_" ? #8

Closed Drozerah closed 4 years ago

Drozerah commented 4 years ago

How to turn off "prefixed with CYPRESS_" ?

/**
 * Cypress-dotenv Plugin 
 */
const dotenvPlugin = require('cypress-dotenv');
module.exports = (on, config) => {
  config = dotenvPlugin(config)
  return config
}
morficus commented 4 years ago

Hey @Drozerah - starting with v1.2.0 you can pass in a 3rd option to this plugin

config = dotenvPlugin(config, {}, true)

The third is an optional [all] boolean parameter, which is set to false by default. If set to true, it returns all available environmental variables, not limited to those prefixed with CYPRESS_.

Drozerah commented 4 years ago

Hey @Drozerah - starting with v1.2.0 you can pass in a 3rd option to this plugin

config = dotenvPlugin(config, {}, true)

The third is an optional [all] boolean parameter, which is set to false by default. If set to true, it returns all available environmental variables, not limited to those prefixed with CYPRESS_.

@morficus - Hi, I had already tried your solution but it's not working... If you take a look at the plugin code, it seems the second parameter is not defined as an optional parameter, I'am wrong ?

spec.js

/**
 * Cypress-dotenv Plugin 
 */
const dotenvPlugin = require('cypress-dotenv');
module.exports = (on, config) => {
  config = dotenvPlugin(config, {}, true)
  return config
}

__node_modules\cypress-dotenv\index.js__

/**
 * Cypress dotenv plugin
 *
 * @param {object} cypressConfig - The cypress config object
 * @param {object} dotEnvConfig - (optional) The dotenv config object, ref: https://www.npmjs.com/package/dotenv#config
 * @param {boolean} all - (optional) Whether to return all env variables. If set to false (default), only env variables prefixed with CYPRESS_ are returned.
 * @returns {object} The cypress config with an augmented `env` property
 */
module.exports = (cypressConfig, dotEnvConfig, all = false) => {
  // load the content of the .env file, then parse each variable to the correct type (string, number, boolean, etc.)
  let envVars = require('dotenv').config(dotEnvConfig)
  const dotenvParseVariables = require('dotenv-parse-variables')
  envVars = dotenvParseVariables(envVars.parsed || {})

  let enhancedConfig = clonedeep(cypressConfig)
  enhancedConfig.env = enhancedConfig.env || {}

  // get the name of all env vars that relate to cypress
  const cypressEnvVarKeys = all
    ? Object.keys(envVars)
    : Object.keys(envVars).filter(envName => envName.startsWith('CYPRESS_'))

  cypressEnvVarKeys.forEach(originalName => {
    const cleanName = originalName.replace('CYPRESS_', '')
    const camelCaseName = camelcase(cleanName)
    enhancedConfig.env[cleanName] = envVars[originalName]

    if (enhancedConfig.hasOwnProperty(camelCaseName) && camelCaseName !== 'env') {
      enhancedConfig[camelCaseName] = envVars[originalName]
    }
  })

  return enhancedConfig
}
morficus commented 4 years ago

@Drozerah you are correct that in order to pass in the 3rd parameter you must provide a value for the 2nd parameter. a value of {} should be good enough.

I am using using this plugin with Cypress 4.3.0 as well and I haven't had any issues

This is what my cypress/plugins/index.js file looks like.

const dotenvPlugin = require('cypress-dotenv')
module.exports = (on, config) => {
  config = dotenvPlugin(config, {}, true)
  return config
}

you said

browser can not be launch as consequence What error are you getting when you trying the above? Could you post a screenshot or track trace of the error please?