AleG94 / mocha-suppress-logs

Suppress console output of successful mocha tests
MIT License
13 stars 3 forks source link

Log output for failed tests does NOT print the state of objects at the time of the log call 🙀 #8

Closed vassudanagunta closed 11 months ago

vassudanagunta commented 2 years ago

When mocha-suppress-logs prints the log messages for failed tests, instead of printing the state of objects at the time of the log call, it prints their state after the test fails and exits. While this is ok for some tests, as the final state is enough the identify the bug, for many if not most cases the output is completely misleading and confusing. It violates the critical principle that all programmers assume when using log message to debug their code, that log messages reflect program state at the point of the log call.

This is happening because the current code intercepts the log calls and saves the arguments for those calls:

https://github.com/AleG94/mocha-suppress-logs/blob/ec776ce9da881e8c1a1008f5c96cc1222deec961/lib/log-capture.js#L30-L32

On test failure, it passes those args to the originally intended console methods:

https://github.com/AleG94/mocha-suppress-logs/blob/ec776ce9da881e8c1a1008f5c96cc1222deec961/index.js#L15-L17

The result of course is that the end state of the objects are printed. Even that may be misleading, as the args may represent discarded program state, objects that would otherwise have been garbage collected.

The fix is, instead of intercepting the arguments to log calls, execute those calls and intercept their output. This can be done by intercepting stdout, for example.

When I get some free time I will submit a PR with a fix :)

AleG94 commented 2 years ago

Thanks for reporting the issue, I already tested a solution for this and will be releasing the fix later this week!

vassudanagunta commented 2 years ago

Any chance you can push this out soonish, even as an "dev" release?

If you're having some doubts about your solution, you can take a look at log-interceptor (what I was using before i switched to your library. It has some nice functionality as I mentioned on a previous issue here.

I don't know if you noticed, but usage has been going up!

saramcicchi commented 11 months ago

I would greatly appreciate this being fixed as well! I just spent way too long debugging something based off of incorrect log messages.

AleG94 commented 11 months ago

I just released version 0.4.0 and it should be fixed now, please let me know!

vassudanagunta commented 11 months ago

Confirmed. Thank you! I had to use workarounds this past year and a half, and had on my todo list to publish my own solution. Now no need!

It's good because usage is going through the roof! Congrats.