1

I run Windows10, I have a project made on Cypress and I want to log the test results both on a file AND on console: I tried just printing on a file, using in my package.json this script:

"scripts": {

        "open": "./node_modules/.bin/cypress open",
        "runheadless": "./node_modules/.bin/cypress run --headless --browser chrome
         --spec 'cypress/integration/webpages.spec.js' > cypresstest.log"
      }

And this runs smoothly; my issue is that there are more than 100 tests and it takes very long time (like 20 minutes); so I can't check if something got frozen or is working fine, because nothing is printed on console.

So I tried with

"runheadless": "./node_modules/.bin/cypress run --headless --browser chrome
         --spec 'cypress/integration/webpages.spec.js' | tee cypresstest.log"

But since I'm on windows, it says

tee is not recognized as internal or external program

Is there a way, or a plugin, or something I can do to simply print both on console AND on a file log?

Don Diego
  • 1,309
  • 3
  • 23
  • 46

1 Answers1

1

Cypress-terminal-report has such a feature, or you can use a custom command including cy. task instead of cy.log - for example:

cypress plugin file

module.exports = (on, config) => {
   on('task', {
        log (message) {
            console.log(message)

            return null
        }
    })
}

custom command:

Cypress.Commands.add("logInAndOut", (message) => {
    cy.log(message)
    cy.task('log', message)
});

test file

cy.logInAndOut('My log')

Edit: I found another solution - Commands. overwrite(), and I`ll list an example, but I do not recommend it, since if other people try to use the code after you - won't know the change:

Cypress.Commands.overwrite('log', (originalFn, message, args...) => {
  console.log(message, args...)

  // originalFn is the existing `log` command that you need to call
  // and it will receive whatever you pass in here.
  //
  // make sure to add a return here!
  return originalFn(message, args...)
})
Rosen Mihaylov
  • 1,363
  • 2
  • 10
  • Thanks, I'll try the plugin! – Don Diego May 14 '21 at 14:08
  • I improved my suggestion since I tried it and it started to show only on the console – Rosen Mihaylov May 14 '21 at 18:41
  • Added a third option on how to do it. – Rosen Mihaylov May 19 '21 at 17:45
  • well, I've finally tried the plugin but sadly does not do what I meant to do: it prints a json (or similar) file with a resume of errors, but sadly it doesn't print exactly what it prints on terminal; moreover for some weird reasons, if I try to use it on my project using cypress interface instead of headless, it loops forever. I will try your custom suggestion.. – Don Diego Jun 16 '21 at 14:09
  • According to other users this should solve your problem: custom command: `Cypress.Commands.overwrite('log', (subject, message) => cy.task('log', message));` => event in plugins/index.js: `on('task', { log (message) {console.log(message); return null; }})` https://stackoverflow.com/questions/52070262/cypress-pipe-console-log-and-command-log-to-output – Rosen Mihaylov Jun 17 '21 at 08:38