Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] no console logging output if using custom reporter #18945

Closed
mstepin-edt opened this issue Nov 21, 2022 · 9 comments · Fixed by #19118
Closed

[BUG] no console logging output if using custom reporter #18945

mstepin-edt opened this issue Nov 21, 2022 · 9 comments · Fixed by #19118

Comments

@mstepin-edt
Copy link

Context:

  • Playwright Version: 1.27.1
  • Operating System: Ubuntu (WSL)
  • Node.js version: 19.0.0
  • Browser: Chrome

When adding a custom reporter, the console.log output is no longer displayed in the console.

NOTE: The console stdOut attachment to the html reporter still shows the desired output.

Consider the following setup:

// playwright.config.js

            reporter: [
              ['html', {open: 'never'}],
              ['./lib/utils/customReporters/myCustomReporter.js']
            ],

Custom reporter:

//  ./lib/utils/customReporters/myCustomReporter.js
// @ts-nocheck

/** @implements {import('@playwright/test/reporter').Reporter} */
class MyReporter {
    onBegin(config, suite) {
        console.log('this text IS shown in the console')
        someOtherFunction()
    }
    onStepEnd(test, result, step) {
        console.log('this text IS ALSO shown in console')
        someOtherFunctionTwo()
    }
}

module.exports = MyReporter

Now consider this test:

// ./tests/myTest.spec.js
const { test } = require('@playwright/test')

test('Some interesting test title', async ( {}, testInfo ) => {
    console.log('this text is NOT shown in the console')
    await someReallyCoolFunction()
})

// ./func/someFunctionFile.js
async function someReallyCoolFunction() {
     console.log('this text is NOT shown in the console')
}

As per code snippets above, only the console logging from the custom reporter is output to the actual console.

When commenting out the code in playwright.config.js for the custom reporter, all lines are output in the console as expected.

@dgozman
Copy link
Contributor

dgozman commented Nov 21, 2022

@mstepin-edt There are two options to make console.log visible with custom reporter:

  1. Handle onStdOut/onStdErr and print to the terminal in your custom reporter. This is what default reporters do for you behind the scenes.
  2. Return false from printsToStdio(). In this case, Playwright will add some terminal output, including console.log output.

Let me know if this helps.

@mstepin-edt
Copy link
Author

@dgozman I did spot those before I logged the bug report, and added them to my reporter. That said, it seems extremely counter-intuitive for a standalone reporter class to influence terminal output and have to explicitly proxy the output stream, else risk affecting existing functionality. There probably aren't all too many use cases where one might want to completely suppress terminal output, and should there be a need to suppress terminal output, it makes more sense for that to be a config option rather than this convoluted reporter path. The reporter needs to be a standalone listener -> ie. "you'll receive the stdOut and stdErr events, do with it what you will, but we'll continue doing what we've always done until now".
As you said, the default reporters under the covers continue to output to terminal, so why should they stop outputting just because there is a new reporter in town?

@dgozman
Copy link
Contributor

dgozman commented Nov 22, 2022

@mstepin-edt The reporters API is designed so that you can replace default stdio reporter with anything. For example, you can use a json reporter that outputs a json dump to the stdout. I think we can improve documentation though.

@mstepin-edt
Copy link
Author

@dgozman agreed, if at the very least some improved documentation comes out of this discussion, that would be great. I still stand by my above comment in that a new standalone module such as a reporter should be a consumer and not a proxy for existing behaviour.

@luixo
Copy link

luixo commented Sep 21, 2023

Hello,
I found out that all the debug data I may want to look through is discarded when using a custom reporter which includes console.logs from tests itself as well as output from --list cli option.

This does not make sense if I want to keep the tests progress while printing some custom reporter extra info to console in the end of all tests.

I believe the quick fix (though it will break the compatibility) might be to force existence of printsToStdio function.
The proper one would be to let custom reporters use some kind of logger manager that will let them log whatever they report in the end / while tests are running without (or with, if needed) conflicting with default reporter.

@didu9898
Copy link

didu9898 commented Jul 2, 2024

In my case, only the console.log from custom test reporter was working. Even after printsToStdio to true, the console.log from tests where not logged.
I used onStdOut in my custom test reporter.

onStdOut(chunk: string | Buffer, test: void | TestCase, result: void | TestResult): void {
        console.log(chunk);
    }

@samevans5
Copy link

Is there a way to prevent console.log(chunk); from adding a new line in each log, I would like to have the default be 0 space and handle this myself

Current output:
Message 1

Message 2

New section

Message 1

The output I want:
Message 1
Message 2

New section
Message 1

@luixo
Copy link

luixo commented Aug 14, 2024

@dgozman

Could we get back to this issue?
Doc changes do not seem to fix the problem described earlier :(

@dgozman
Copy link
Contributor

dgozman commented Aug 14, 2024

@luixo Could you please file a new issue by filling in either the "Bug Report" or the "Feature Request" template, so that we can handle it separately?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants