Nesting commands - promise error
# help
h
Nesting commands - promise error
Hey, i am trying to debug why
res.body.payload
is sometimes empty (as my comment says). So i added
logToCLI
method in a plugin and i want to call it by
cy.task
.
Copy code
js
Cypress.Commands.add('confirmTestUserRegistration', (email) => {
    return cy.getLinkFromEmail(email, 'Account activation', '/pages/confirm-registration').then((url) => {
      cy.intercept('/api/users/confirm-registration', (req) => {
        req.continue((res) => {
          console.log('[confirmTestUserRegistration()] /res:', res);
          if (!res.body.payload) {
            cy.task('logToCLI', {
              message: `confirmTestUserRegistration() no 'res.body.payload' found!`,
              value: res,
            });
          }

          // TODO: [issue] sometimes `res.body.payload` is undefined due to 401 response code
          return expect(res.body.payload.isUserConfirmed).to.be.true;
        });
      }).as('confirmRegistration');

      cy.visit(url);
      cy.wait('@confirmRegistration');
      cy.contains(
        '[data-cy="message:registration-confirmation-succeeded-hint"]',
        'You can now log in to your new account.'
      );
    });
  });
The plugin code is fairly simple:
Copy code
js
module.exports = (on) => {
    on('task', {
        logToCLI ({ message, value }) {
            console.log('[Cypress CLI]', message, value);
            
            return null;
        }
    })
}
, but Cypress throws error: > **CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise. > ** > The command that returned the promise was: > > >
cy.wait()
> > The cy command you invoked inside the promise was: > > >
cy.task()
Why do i receive the error if i don't use any promise? Is it about nesting?
Update: i prepended
cy.task(..)
call with
return
and error no longer appears (i don't exactly know why though - i'd be grateful for some explanation regarding how Cypress behaves here). However, i don't see a log inside CLI, like the task itself didn't run 🤔 I didn't notice other errors.
w
I haven't been able to look at this example in detail, but this video from Gleb might be helpful, especially around 5:45 where he shows an example of getting this error without explicitly using a promise

https://www.youtube.com/watch?v=9AF-lcvh1dkâ–¾

It might be that Cypress is not sure whether to
return expect(res.body.payload.isUserConfirmed).to.be.true
or wait for `cy.task`to complete. Do you definitely need to return the
expect
stuff? This is not a part of Cy I'm super familiar with but hopefully Gleb's video gets you on the right track. Happy to come back to this in the next few days when I have more time too.
h
Thanks for reply. I watched the YT film, but AFAIK in my case Cypress should rather not be confused what to do, because i have
if
statement. But maybe the issue is that unless the
cy.task
is prepended by
return
then Cypress still has dillema like "Ok, condition met, so do
cy.task
, but there is also
expect
outside the condition - so what to do? Oh, i'll throw error, because i don't know.". If i add
return
before
cy.task
the error is gone, because i think Cypress should know at this moment that
cy.task
returns a promise-alike, which is also returned, so no
expect
is going to happen. What's more confusing for me (as i at least has some clue about what's reason for
cy.task
error) - why the
logToCLI
task doesn't log anything to Node's console? I return
null
from it, because i don't do any async stuff in there.
4 Views