Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
JS + Cypress : unable to get the output from a function

I recently started playing with JS and looking into Cypress to write some simple test automation.

my code writes the following:

    Cypress.Commands.add("setup", (email, password) => {
    getAccessToken(email, password).then(console.log)
   
  })

  function getAccessToken (email, password) {
    cy.request('POST', 'testurl',{       
        "email": email,
       "password": password
       }).then((response) => {
           return response.body.access_token
       })
  }

console.log prints access_token just fine if I place it within getAccessToken at where the return statement is.. but the console.log prints unidentified if I call it in the command setup even after using .then, (my goal is to get the access_token and use it as an input for another function within "setup")

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your return only returns the response.body.access_token in the Cypress chain. If you added a .then() after the one in your function, you would have correctly yielded the value.

Instead, you can return the entire cy.request() chain in your function, and see the response.

  function getAccessToken (email, password) {
    return cy.request('POST', 'testurl',{       
        "email": email,
       "password": password
       }).then((response) => {
           return response.body.access_token
       })
  }

Tested with this code:

    const getTest = () => {
      return cy.request('http://www.google.com').then((res) => {
        return res.body;
      });
    };

    getTest().then(console.log);
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda