How to set environment variables in cypress.json file through command line? (Angular CLI - nrwl nx)
Asked Answered
L

1

7

I am new to cypress and learning day by day, we are trying to implement e2e tests using cypress in angular project.

I am facing issues when I want to set env variables in cypress.json file through the command line

This is my cypress.json file

{
  "defaultCommandTimeout": 10000,
  "viewportWidth": 1440,
  "viewportHeight": 900,
  "env": {
    "environment": "Stagging"
  },
  "fileServerFolder": ".",
  "fixturesFolder": "./src/fixtures",
  "integrationFolder": "./src/integration",
  "pluginsFile": "./src/plugins/index",
  "supportFile": "./src/support/index.ts",
  "video": true,
  "videosFolder": "../../../dist/cypress/apps/web/e2e/videos",
  "screenshotsFolder": "../../../dist/cypress/apps/web/e2e/screenshots",
  "chromeWebSecurity": false
}

I wanted to dynamically relace the Environment value, in my case from Stagging to dev. I am trying to use the following ng command

ng e2e --env environment=Dev

it's throwing me this error enter image description here

when I worked on POC, this option worked fine as they mentioned in cypress docs (https://docs.cypress.io/guides/guides/environment-variables.html#Setting)

can someone help me to solve this?

Lamarlamarck answered 17/10, 2019 at 20:18 Comment(2)
The error comes from 'ng' command not the cypress. If you need to set up cypress environment wouldn't it be running cypress command?Poirier
It looks like your error is connected with nx framework usage, as it's known issue in their infrastructure. Take a look at this discussion: github.com/nrwl/nx/issues/1970Sweeny
U
4

You need to run cypress open --env environment=Dev instead of the ng CLI command. If you want to run the tests directly, instead of opening the Test Runner, then use cypress run --env environment=Dev

Uncork answered 18/10, 2019 at 12:39 Comment(2)
I tried that option as well, the problem here is according to the framework .nodemodules is sitting in projects root folder 'dyno\node_modules' , whereas my cypress framework is sitting in 'dyno\apps\admin-web\ui-e2e' so, when I am trying to execute 'cypress open --env environment=Dev', a sample cypress framework and cypress.json file is getting created in the root folder ('dyno\cypress.json') and executing cypress example integration tests provided by cypress guys, instead of my project testsLamarlamarck
Where is your cypress.json file? Is it under dyno\apps\admin-web\ui-e2e? If so firstly go to that folder where cypress json file has. Then run the command ''cypress open --env environment=Dev'. If it cannot find cypress executable add it to environment variable or use the absolute path.Poirier

© 2022 - 2024 — McMap. All rights reserved.