0

I have five different cypress projects in the same repo.

The Cypress.json file of each project has reporterOptions :

{
  "fixturesFolder": "./src/fixtures",
  "integrationFolder": "./src/integration",
   ……..
  "reporter": "../../node_modules/mocha-testrail-reporter",
  "reporterOptions": {
    "username": "my-user-name”,
    "password": "my-password",
    "host": "https://abc.testrail.io",
    "domain": "abc.testrail.io",
    "projectId": 1,
    "suiteId": 3,
    "includeAllInTestRun": true,
    "runName": "test"
  }
}

The Username, host, password and domain value are same for all five cypress projects. Thus, I want to put them in the .env file like this, and access these variables and use them in the Cypress.json files

USERNAME= my-user-name
PASSWORD= my-password
HOST= https://abc.testrail.io
DOMAIN= abc.testrail.io

How do I access these variables? Any help will be appreciated. Thank you,

sofina
  • 341
  • 1
  • 2
  • 9
  • Does this answer your question? [How to use .env variables inside cypress.json file?](https://stackoverflow.com/questions/70333682/how-to-use-env-variables-inside-cypress-json-file) – nathan liang Mar 25 '22 at 23:48

1 Answers1

0

Take a look at Extending the Cypress Config File

Cypress does not support extends syntax in its configuration file

But in plugins it can be done

module.exports = (on, config) => {

  const reporterParams = require('.env')  // not quite sure of the format
                                          // may need to fiddle it

  const reportOptions = {
    ...config.reporterOptions,  // spread existing options
    "username": reporterParams.username,
    "password": reporterParams.password,
    "host": reporterParams.host,
    "domain": reporterParams.domain,
  }

  const merged = {
    ...config,
    reportOptions 
  }

  return merged
}