vue-cli: E2E tests with Cypress do not use .env.test for env variables
Version
3.0.0-beta.9
Reproduction link
https://github.com/lowski/fakturownia/tree/managing-clients - it’s an app that I use for testing different techniques for Vue.js app but setup is the default one, created by Vue-cli.
Steps to reproduce
- Create .env.test file with some env variable, i.e:
VUE_APP_API_URL=http://localhost:8081
- Try to reference that variable inside any of your client-side JavaScript files, i.e.:
console.log(process.env.VUE_APP_API_URL)
What is expected?
It is expected to print http://localhost:8081
when tests are run with Cypress
What is actually happening?
It’s printing undefined
I tried to pass --mode test
to e2e
command like this:
vue-cli-service test:e2e --mode test
But it throws error that HMR is disabled and test does not run.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (14 by maintainers)
It’s still a issue, .env variables are not shared to Cypress.env nor process.env inside e2e tests. Duplicating .env content to cypress.json does not seem to be a good solution IMHO
from the docs of that plugin:
From https://github.com/vuejs/vue-cli/blob/dev/docs/env.md#modes
So you can either provide a
.env.production
file, or, if you rather want to use a different mode, do this:The important part is to set NODE_ENV to production so the backend config is setup like a production build, while the environment variables can be different.
I don’t get why this issue got closed. @lowski The solution you propose does not work.
process.env
is simply not modified. @yyx990803 What about the application’s code that does not useCypress.env()
? I spent a few hours yesterday trying to figure out how to alterprocess.env
for Cypress’ tests but nothing seemed to work. While searching for a solution in search engines, I found very few relevant answers like what I was trying to do was very exotic.@LinusBorg @yyx990803 thanks for your help. I decided to add the variable to
cypress.json
file and created small test helper that at first line in my test:This basically expose Cypress envs to
process.env
so I can easily refer it inside my code. If I need to add more specific setup in the future I can throw it here as well.