5

When running Cypress headlessly, I can see console.log output from the frontend code under test by using the DEBUG environment variable, like:

DEBUG='cypress:launcher' npx cypress run --browser chrome

However, I haven't found any similar way to see the output of cy.log from the Cypress test code when running headlessly. Even with DEBUG='cypress:*' I don't see them - they only seem to be visible in the interactive interface. It feels like there must be some way to see the cy.log output headlessly - can someone help with that?

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    Does this answer your question? [Cypress pipe console.log and command log to output](https://stackoverflow.com/questions/52070262/cypress-pipe-console-log-and-command-log-to-output) – jonrsharpe Sep 23 '22 at 10:38

2 Answers2

10

The first step is to add a new task in your Cypress config file so that you can run console.log from Node:

import { defineConfig } from "cypress";

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on("task", {
        log(args) {
          console.log(...args);
          return null;
        }
      });
    },
  },
});

Then, you can override cy.log so that it calls this task whenever you run the command in headless mode, and console.log when you're running in headed mode. You can do this by adding the following to your commands file:

Cypress.Commands.overwrite("log", function(log, ...args) {
  if (Cypress.browser.isHeadless) {
    return cy.task("log", args, { log: false }).then(() => {
      return log(...args);
    });
  } else {
    console.log(...args);
    return log(...args);
  }
});
hughsk
  • 3,822
  • 1
  • 21
  • 16
  • I've added this to my code, but what value should I assigned to the DEBUG variable to see the cy.log output? – mgpmul Jul 07 '23 at 10:15
0

I can see the output of cy.log by using DEBUG=cypress.server.task. I used it to get a log of the headers of a sent request when running in headless mode.

Although I first tried the solution of @hughsk, I noticed that I could to without it; just using DEBUG suffices for me. Because we also have code coverage running as a task, this will still generate a lot of logging, after which it is a bit tedious to find the cy.log task that your are interested in. In that case, you can redirect the output to a file and then use a text editor to search for it.

In Linux/Unix, the DEBUG output is going to the error output, so you'll have to use '2>' to redirect it to a file.

Like this:

DEBUG='cypress:server:task' npx cypress run --browser=chrome 2> DEBUG_cypress-server-task.log
mgpmul
  • 145
  • 2
  • 9