https://cypress.io logo
No way to log to stdout from within a command
# i-need-help
g
Hi, I feel like I reached a bit of a deadend here. I am trying to get some debug output that I can evaluate to figure out some page load timeout issues I'm hitting. The way I'm trying to do this is by outputting a set of resources that have not been "loaded" by the time a page load event is reached, or in 100s after a
visit
command is started (whichever comes first). The issue is that, in order to output to the stdout, I am using
cy.task('log', ...)
, a pretty familiar pattern, I'm sure, since it's outlined in Cypress' own docs for
cy.task
. When this command is run within the context of a
cy.visit
command, it gives me this error:
Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
(outlined here: https://docs.cypress.io/guides/references/error-messages#Cypress-detected-that-you-returned-a-promise-from-a-command-while-also-invoking-one-or-more-cy-commands-in-that-promise). There are no promises being used anywhere, so I figure the mere usage of
cy.task
is a violation. To me, this would indicate that there's actually no safe way to output to stdout from within the context of a command, which seems like a major shortcoming of the interface. But I'm sure I'm missing something. Example code:
Copy code
// plugins code
...
on('task', {
  log(message) {
    // eslint-disable-next-line no-console
    console.log(message);

    return null;
  },
...

// func that logs to cy.task
function someFunc() {
  cy.task('log', 'log to stdout');
}

// func that overwrites visit
Cypress.Commands.overwrite(
  'visit',
  (originalFn, url, options) => {
    someFunc();

    return originalFn(url, options);
  }
);
g
it is hard to give any advice without seeing any test code
g
You're right; one second...
Updated question with general form of the code structure
g
what happens if you do not return the result and just do
Copy code
js
Cypress.Commands.overwrite(
  'visit',
  (originalFn, url, options) => {
    someFunc();

    originalFn(url, options);
  }
);
g
Let me check
Same result
g
I would open an issue in the Cypress repo, I believe I have overwritten the
cy.visit
command before, but cannot find an example right now
g
Will do -- thanks for the help. Also, thanks for all the amazing contributions you make to Cypress (and the documentation too!)