Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda