handsome-cartoon-58565
01/22/2022, 7:41 PMhandsome-cartoon-58565
01/22/2022, 7:41 PMres.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.
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:
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?handsome-cartoon-58565
01/23/2022, 5:55 PMcy.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.wonderful-match-15836
01/25/2022, 1:01 PMreturn 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.handsome-cartoon-58565
01/25/2022, 1:37 PMif 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.