mocha icon indicating copy to clipboard operation
mocha copied to clipboard

🚀 Feature: Add a `--clearscreen` option to go with `--watch`

Open ScottFreeCode opened this issue 8 years ago • 19 comments

We should add a --clearscreen flag that clears the console before a test run, for use with --watch. This would be handy for reporters other than "min" (which has this effect built in and can be used as an example for implementation). It can currently be done with a global before hook, but that's clumsier than a flag would be (you have to go out of your way to set it up so that you can choose to include it or not as easily as if it were a flag) and could work incorrectly in the unlikely edge case that you have other global before hooks printing stuff.

Inspired by @AshCoolman on Gitter.

ScottFreeCode avatar Jun 15 '16 20:06 ScottFreeCode

+1 for this feature

dNitro avatar Jun 18 '16 21:06 dNitro

A neat idea. I'm loathe to add anything to the "watch" functionality at this point though...

boneskull avatar Jun 27 '16 06:06 boneskull

This would be a really great flag because I want to use --reporter min with --clearscreen false (or something similar). Right now using --reporter min always clears the screen and that does not work well for my needs.

erikmellum avatar Apr 07 '17 19:04 erikmellum

I want this feature too. Since I guess it has been decided that this won't be added, I just hacked it in by adding console.log('\x1Bc'); to the reporters directly.

Just edit node_modules/mocha/lib/reporters/ and add it manually where you see runner.on('start'....

Obviously it's not permanent, and has to be done for each repo, but it works well enough for me.

brennancheung avatar Jul 29 '17 22:07 brennancheung

@brennancheung I didn't decide it wasn't to be added :wink: I'd just rather avoid piling features ontop of an already problematic feature.

boneskull avatar Jul 30 '17 19:07 boneskull

+1

HoltMansfield avatar Aug 20 '17 00:08 HoltMansfield

Or another option would be, if adding a flag is too complicated, to make a new reporter, call it even-more-min, which does the exact same thing as min except for clearing the screen and setting the cursor.

justinmchase avatar Dec 05 '18 15:12 justinmchase

I am running yarn mocha --recursive --watch --reporter min

This did not clear the screen when I saved a file I upgraded mocha from 3.2.0 to [email protected]

this had no effect. Screen output is not cleared at all.

Coming from Rails where I run Guard all the time with clearing :on - I don't know if I can live without it.

brentgreeff avatar Apr 29 '19 10:04 brentgreeff

This did not clear the screen when I saved a file

Actually it seems to add a full page of black space which is not the clear command

brentgreeff avatar Apr 29 '19 10:04 brentgreeff

Maybe yarn blocks the clearing or translates it? I don't think this feature was ever implemented in mocha so the min reporter does still clear last I looked.

Try:

npx mocha --recursive --watch --reporter min

justinmchase avatar Apr 29 '19 14:04 justinmchase

npx mocha --recursive --watch --reporter min Same result unfortunately. - Must be something project specific - thx anyway.

brentgreeff avatar Apr 29 '19 14:04 brentgreeff

This did not clear the screen when I saved a file

Actually it seems to add a full page of black space which is not the clear command

@brentgreeff , the "min" reporter does this when run begins:

process.stdout.write('\u001b[2J');

From this documentation on ANSI-X3.64-1979 terminal control sequences, the code above should erase the entire display.

But I don't know the affect of --watch off top of my head...

plroebuck avatar Apr 30 '19 20:04 plroebuck

@plroebuck That just watches for changes to files under test. When a file changes a new test run is kicked off and if you're using the min reporter the screen will be cleared before each test new run.

justinmchase avatar Apr 30 '19 21:04 justinmchase

For posterity...

If you are still looking for a solution to the original problem in this thread, which was to use the min reporter but have it not clear the console, here's the solution I finally came up with:

I created the custom reporter called even-more-min which is a clone of min, sans clearing.

Install

npm install even-more-min --save-dev

Use

npx mocha --reporter even-more-min

justinmchase avatar Apr 30 '19 21:04 justinmchase

This works for me: https://www.npmjs.com/package/mocha-clearscreen-reporter

mocha --watch  --reporter mocha-clearscreen-reporter

mprinc avatar Jun 09 '20 09:06 mprinc

yslpn avatar Jun 15 '23 11:06 yslpn

Seems reasonable to me. Maybe, --clear-screen since other Mocha CLI flags use kebab-case (--allow-uncaught, --async-only, etc.)?

Tangentially related: TypeScript had the opposite behavior at first (always clear by default), but then added in a --preserveWatchOutput flag: https://github.com/microsoft/TypeScript/issues/21295.

JoshuaKGoldberg avatar Feb 06 '24 03:02 JoshuaKGoldberg

I cant believe its 8 years and this is still a (wrong) thing!

anodynos avatar Mar 04 '24 11:03 anodynos

I cant believe its 8 years and this is still a (wrong) thing!

Believe it, @anodynos 😉. Mocha is a community-run open source project. There's no company employing developers working on it. Until recently there wasn't a consistently active maintenance team for several years.

If you want this issue resolved, send a pull request to implement the feature yourself. Comments expressing unhappiness at the situation don't help the conversation and just spam everyone subscribed to the thread.

In the meantime, https://github.com/mochajs/mocha/issues/2312#issuecomment-488121898 and https://github.com/mochajs/mocha/issues/2312#issuecomment-641172258 include links to community-authored workarounds.

JoshuaKGoldberg avatar Mar 04 '24 21:03 JoshuaKGoldberg