Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component testing: process.env is always empty within Vue component #17058

Open
azerkail opened this issue Jun 22, 2021 · 11 comments
Open

Component testing: process.env is always empty within Vue component #17058

azerkail opened this issue Jun 22, 2021 · 11 comments
Labels
CT Issue related to component testing stale no activity on this issue for a long period

Comments

@azerkail
Copy link

azerkail commented Jun 22, 2021

Current behavior

This happens only when running Cypress for component testing with cypress run-ct.
The process.env of node is never populated within the Vue component.
I've tried doing all the different options available in the documentation here.

Notice that in the example repository there is an environment variable set in cypress.json for NODE_ENV which is correctly available in the test code when doing cy.log(Cypress.env('NODE_ENV'));

image

However, in the component Button.vue the console log that exposes process.env on line 25 always produces an output of {}.
I think what this means is that the Cypress test environment is being populated with the environment variables I am giving it, but not the environment that the components are running in.

I have also tried overriding the webpack env variables directly within /plugins/index.ts with the following changes:

// Set all of the process.env to the config.env
config.env = process.env;

// Set only NODE_ENV directly
config.env.NODE_ENV = "test";

None of this worked either.

Desired behavior

I think the right solution here would be to have the environment variables that are given to Cypress also be propagated into the process.env of the components as well.
If that's not right, then I would at least like to have a solution where I can provide environment variables to the component.

This is useful for testing cases where we want to override the component behaviour for testing, for example in my application I have a loader that waits one second before displaying content to the user for better UI usability, this is completely useless and slows down testing within a test environment, so I would like to tell the component to wait for one second only if process.env.NODE_ENV is not test.

Test code to reproduce

You can find a reproducible repo here: https://github.com/St1fle/cypress-ct-env
Just run with npm run test and run the single case in there, you should see the cy.log mentioned above, as well as a single console log with an empty object in the dev tools.
In the example repo, the test is doing a simple check that the component should have disabled classes if the process.env.NODE_ENV is set to `test.

Versions

Cypress version: 7.4.0
Operating System: Windows
Browser: Microsoft Edge

@jennifer-shehane
Copy link
Member

@St1fle You've misspelled NODE_ENV in your component as NOVE_ENV. Can you try running again after fixing that?

Screen Shot 2021-06-22 at 12 35 43 PM

@jennifer-shehane jennifer-shehane added the stage: awaiting response Potential fix was proposed; awaiting response label Jun 22, 2021
@azerkail
Copy link
Author

azerkail commented Jun 22, 2021

Thanks for spotting that out, I must've copied out the value incorrectly when making the example repo sorry about that.

Unfortunately this does not fix the issue I am seeing. I added a few more console logs to the example repo and I am seeing some really strange results. The component looks like this:

image

Those console logs produce the following output:

image

So the process.env looks like an empty object, but when accessing process.env.NODE_ENV it is set with the value development, even though my cypress.json looks like this:

{
  "pluginsFile": "src/plugins/index.ts",
  "supportFile": "src/support/index.ts",
  "componentFolder": "src/specs",
  "testFiles": "**/*.spec.{ts,js}",
  "fixturesFolder": "src/fixtures",
  "screenshotsFolder": "src/screenshots",
  "videosFolder": "src/videos",
  "ignoreTestFiles": "**/*.{json}",
  "userAgent": "E2E",
  "video": true,
  "videoUploadOnPasses": false,
  "defaultCommandTimeout": 12000,
  "watchForFileChanges": false,
  "retries": {
    "runMode": 2,
    "openMode": 0
  },
  "reporter": "cypress-multi-reporters",
  "reporterOptions": {
    "configFile": "reporter-config.json"
  },
  "component": {
    "watchForFileChanges": true
  },
  "env": {
    "NODE_ENV": "test"
  }
}

I have also tried uncommenting the lines in plugins/index.ts that set the env variable via the config directly to test and that didn't seem to work either, the test console logs always outputs development.
So it looks like it is being set somewhere, but completely ignoring my attempts at configuring it.

I've update the example repo to reflect this.

@jose-aktiun
Copy link

I have the same issue with React. Passing env variables to open-ct is not working to me in any form or way mentioned in the documentation.

@frost555
Copy link

Same issue

@moshie
Copy link

moshie commented Jan 18, 2022

same issue (Bump)

Turns out my issue was that I was importing application code into my test and it was killing the cypress webpack build.

@lukkyjoe
Copy link

same issue

@binginsin
Copy link

Same issue here in react

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Jul 19, 2023
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
@guska8
Copy link

guska8 commented Apr 3, 2024

same issue

@jennifer-shehane jennifer-shehane added CT Issue related to component testing and removed stage: awaiting response Potential fix was proposed; awaiting response stale no activity on this issue for a long period labels Apr 3, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CT Issue related to component testing stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

9 participants