question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolve data in custom command

See original GitHub issue

Description I cannot find a way to resolve data in a custom command, using a Cypress.Promise.

Code In support/commands

Cypress.addParentCommand('signIn', function (email, password) {
  return new Cypress.Promise((resolve, reject) => {
    cy.request({
      method: 'POST',
      url: '/auth',
      body: {email, password}
    })
    .then(result => {
      resolve(result.body);
    }, reject)
  });
});

In my test:

describe('any test', function () {
  it('should work', function() {
    cy.signIn('user@email.com', 'password')
     .then(res => console.log(res))
  });
});

When I return the promise like above, cy.signIn times out. It does not fail when I do not return the promise, but I still can’t access the request data. Am I doing anything wrong?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
ChristophPcommented, Nov 29, 2016

We also had problems with returning values from custom commands. It took us a while to find this thread about cy.chain(). Our commands are working now. @brian-mann could you please update the docs for the chain method because I couldn’t find it in the docs. I would like to understand how it works specifically.

1reaction
brian-manncommented, Aug 22, 2016

Ah okay the reason this is happening is due to the way Cypress chaining works - there are rules about parent > child commands which ultimately resets the subject between new ‘chains’. It makes sense in the general API but when writing custom commands this is basically a hidden landmine.

You need to do this…

Cypress.addParentCommand('signIn', function (email, password) {
  // chain onto the existing sets so that the subject
  // is not reset when attempting to consume the subject
  // from this custom command
  return cy.chain()
  .request({
    method: 'POST',
    url: '/auth',
    body: {email, password}
  })
  // this changes the subject to the body property from the cy.request object
  .its("body")
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Commands in Cypress - Tools QA
These commands can be created to give a different behavior to the existing in-build Cypress command or can be used to create a...
Read more >
Custom Command Which invokes an action that returns row ...
Hi, in one of my usecases, I have a custom command. When clicked it does some server side calculations and returns an updated...
Read more >
Writing a Custom Cypress Command - Gleb Bahmutov
This blog post teaches you how to write a reusable Cypress Custom Command. Simple custom command Simple command limitation Custom command ...
Read more >
How to return a promise from a Cypress custom command in ...
I'm not going to accept this answer because there has to be a better way closer to the original question. function getData(successFn: (data: ......
Read more >
How to create custom Commands in Cypress? - YouTube
cypress #cypressAutomation #cypressCommandsCypress comes with its own API for creating custom commands and overwriting existing commands.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found