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

Cypress env vars not persisting values outside 'then' block #27856

Closed
rsde21 opened this issue Sep 20, 2023 · 3 comments
Closed

Cypress env vars not persisting values outside 'then' block #27856

rsde21 opened this issue Sep 20, 2023 · 3 comments
Labels
stale no activity on this issue for a long period

Comments

@rsde21
Copy link

rsde21 commented Sep 20, 2023

Current behavior

I have my cypress.env.json file at the root directory. And the initial value of this env var is blank. let's say the name of the env var is TOKEN

  cy.log('pre');
  cy.log(Cypress.env('TOKEN'));            // "" logged

  cy.request({
    method: 'GET',
    url: `/xsrf-token`,
  })
    .its('body')
    .then((data) => {
      Cypress.env('TOKEN', data.token);
      cy.log('mid');
      cy.log(Cypress.env('TOKEN'));     // the correct token value is logged
    });
  cy.log('post');
  cy.log(Cypress.env('TOKEN'));   // "" logged

Desired behavior

I was expecting the cypress env value to persist even outside of the "then" block.

Test code to reproduce

  1. Run any cy.request function
  2. In the then block, set the value of an env var
  3. log the env var after the cy.request

Cypress Version

13.2.0

Node version

18

Operating System

MacOS

Debug Logs

No response

Other

No response

@Crustum7
Copy link
Contributor

Crustum7 commented Sep 26, 2023

Hello! I would recommend using before if you want to set the environment variable before running your tests in your spec file like so

before(() => {
   cy.request({
	   method: 'GET',
	   url: `/xsrf-token`,
	 })
   .its('body')
   .then((data) => {
     Cypress.env('TOKEN', data.token);
     cy.log('mid');
     cy.log(Cypress.env('TOKEN'));     // the correct token value is logged
   });
 })

 it('test', function () {
   cy.log(Cypress.env('TOKEN'))
 })

Do you have a special use case where this does not work? Please let me know

@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 Apr 2, 2024
@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 Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

3 participants