Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda