Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
Picking up Environment Variables in Cypress with TypeScript

I'm working to add tests to a pre-existing monorepo that uses TypeScript in the main. All other tests are in TypeScript and for mine, I wish to use Cypress.

I need to run them on two separate environments and need to understand why my environment variables are not being picked up.

It's probably something very obvious but I am a TS and JS newcomer so hoping for some gentle guidance.

Directly under /config I have dev.json and prod.json for the two envs:

{
  "env": {
    "ENV": "dev",
    "BASE_URL": "http://example.com",

  }
}

My plugins/index.ts as follows:

const fs = require('fs-extra');
const path = require('path');

export default (on: Cypress.PluginEvents, _config: Cypress.PluginConfigOptions): void => {
    on('task', {
        log(message) {
            console.log(message)

            return null
        }
    });

    on('task', {
        table(message) {
            console.table(message);
            return null;
        },
    });

    /**
     * @type {Cypress.PluginConfig}
     */
    function getConfigurationByFile(file) {
        const pathToConfigFile = path.resolve('config', `${file}.json`
        );

        return fs.readJson(pathToConfigFile);
    }

    /**
     * @type {Cypress.PluginConfig}
     */
    module.exports = (on, config) => {
        const file = config.env.configFile || 'dev';

        return getConfigurationByFile(file);
    };

}

Example of use within a test spec:

beforeEach(()=> {
  cy.login(Cypress.env('BASE_URL'), 
})

Example script in package.json

"test:headless": "npm cypress run --env 'configFile=%ENV%",

Running from CLI as: ENV=dev npm run test:headless

This fails to pick up the url from the dev.json

Again, I'm sure it's something dumb but some pointers would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try the CYPRESS_ prefix Option #3: CYPRESS_*

Any OS-level environment variable on your machine that starts with either CYPRESS_ or cypress_ will automatically be added to Cypress' environment variables and made available to you.

CYPRESS_configFile=dev npm run test:headless

...

const file = config.env.configFile || 'dev';
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda