Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!