Home  >  Q&A  >  body text

Setting environment variables using Vite in Cypress

I'm using VueJs 3 with Vite and Cypress.

In my application I have an environment variable that defines my URL:

const url = import.meta.env.VITE_URL

My goal is to replace this VITE_URL in Cypress. I try to create a cypress.env.json file and write in it:

{
      "VITE_URL": "https://...",
}

But it doesn't work. I also tried CYPRESS_URL or CYPRESS_VITE_URL but I got the same result. Any ideas?

P粉787934476P粉787934476304 days ago500

reply all(2)I'll reply

  • P粉846294303

    P粉8462943032023-12-21 00:59:01

    If you have declared the value in the cypress.env.json file, you can use `Cypress.env('varName'); to reference it in your code

    Cypress.env('VITE_URL');
    

    reply
    0
  • P粉946437474

    P粉9464374742023-12-21 00:25:26

    Okay, I solved it. I created a .env.testing file and used it by specifying --mode test in the npm command that started cypress.

    The properties of this env.testing are defined as follows:

    'VITE_URL="http://..."'

    reply
    0
  • Cancelreply