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.

returning a promise in a custom command should throw

See original GitHub issue

You cannot return a 3rd party promise from a custom command because this breaks the chaining between cypress commands. This is because the .then methods are not the same.

It is an anti-pattern anyway because all cypress commands are promises.

For instance doing this:

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

…can simply be rewritten as:

Cypress.addParentCommand('login', function (email, password) {
  cy.request({
    method: 'POST',
    url: '/login',
    body: {email, password}
  })
  .its("body")
})

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
brian-manncommented, Jan 3, 2018

Browsers have no notion of require. Browsers must have a build process to parse the dependency tree and generate JS files. Cypress does this with browserify or you can swap this out with webpack.

You cannot import Meteor that way because it would create a whole new instance which has nothing to do with the Meteor instance your app is using.

In order to access the Meteor in your application is the same way you’d access it from the dev tools - you just set it on window and then access it with cy.window().then((win) => win.Meteor )

0reactions
jennifer-shehanecommented, Feb 7, 2019

@panneerselvamc Can you include the test code you’ve written where you call the function?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to return a promise from a Cypress custom command in ...
My custom command returns a Promise , which Cypress automatically handles and converts to a Cypress.Chainable , but typescript doesn't know ...
Read more >
Cypress.Promise
Cypress is promise aware so if you return a promise from inside of commands like .then() , Cypress will not continue until those...
Read more >
Promise.prototype.then() - JavaScript - MDN Web Docs
Returns a new Promise immediately. This new promise is always pending when returned, regardless of the current promise's status. One of the ...
Read more >
Error handling with promises - The Modern JavaScript Tutorial
In the task an error is thrown form inside the promise and promise is never settled neither with a resolve nor reject. The...
Read more >
Custom Commands - WebdriverIO
If the promise gets rejected, the command will throw an error. import got from 'got' browser.addCommand('makeRequest', async (url) => { return ...
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