Setting environment variables using Vite in Cypress
P粉787934476
P粉787934476 2023-12-20 08:40:07
0
2
535

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粉787934476
P粉787934476

reply all(2)
P粉846294303

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');
P粉946437474

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://..."'
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template