https://cypress.io logo
Fetching async value in Cypress test
# i-need-help
f
I'm having some trouble getting an async value (an array of objects) in a cypress test, and keep playing whack-a-mole with this code. I can see what what the problem is, sort of, but I don't know how to get this global var assigned properly:

https://cdn.discordapp.com/attachments/1101589129671749744/1101589130212806726/Screenshot_2023-04-28_at_2.50.09_PM.png

e
Yeah it can be rough with the async stuff. I have been getting around this by using aliases. So for your setup I would throw that async function just in a custom command and return something like (after using
cy.wrap(object).as('alias')
)
return cy.get('@alias'
) And then call that command in a before hook in your test with the response as an alias as well
Copy code
before(() => {
  cy.yourCustomCommand().then((resp) => {
    cy.wrap(resp).as('@waiverResponse');
  });
});
g
You cannot create new tests when the tests already started. https://glebbahmutov.com/blog/dynamic-api-tests-using-cypress-each/ and https://glebbahmutov.com/blog/dynamic-tests-from-fixture/ I do it from my config / plugins process before the spec loads and then pass the data using Cypress.env object, but this is a paid lesson https://cypress.tips/courses/network-testing/lessons/bonus90