Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!