qunitjs / js-reporters

📋 Common Reporter Interface (CRI) for JavaScript testing frameworks.
MIT License
60 stars 18 forks source link

Defend against console mocks in TAP reporter #125

Closed Krinkle closed 3 years ago

Krinkle commented 4 years ago

Ref https://github.com/qunitjs/qunit/issues/1340.

If a project is mocking console.log because the project in question is, like js-reporters, also related to producing output, then at times it can make the test output broken and very hard to debug.

Krinkle commented 4 years ago

We could capture and cache a reference to console.log early on and use that, although that makes testing our own output harder since then we can't mock our own console anymore when testing TapReporter whilst using TapReporter. We could workaround that by exposing our cached reference in a pseudu-private property that we can still mock manually.

Alternatively, a bit more elaborate but perhaps cleaner method, is to give TapReporter a constructor parameter with a Writable stream. When used normally, that would default to process.stdout (the Writable stream that console.log uses), and under test we can inject our own instance that we can easily mock.