debug-js/debug

Colour lost when using Node native test running (Node 18+)

adam-nielsen opened this issue · 2 comments

When using the new native test runner, Debug does not colour the output, but it would be nice if it did by default:

import Debug from 'debug';
const debug = Debug('example');

debug('Test');

When this is run normally (DEBUG='*' node test.js) the output is coloured as normal, but when run in --test mode, it is not:

$ DEBUG='*' node --test test.js
2024-01-20T15:36:44.768Z example Test

A workaround is to force the colour (DEBUG_COLORS=1 DEBUG='*' node --test test.js) and this works fine, it's just it would be nice if the colours were on by default in the test runner, as that's where they are most useful.

Qix- commented

We can't detect this reliably, unfortunately. The way that TTYs are modelled makes the "is this a TTY whereby I can emit colors" check very fragile to begin with, and the last thing you want is to pipe the output into a program that is doing something with the output in some meaningful way, as the color codes will almost certainly break it.

Unfortunately your problem is exactly why that environment variable exists. However if you're using npm or otherwise have a package.json typically put that sort of thing in the scripts there. If you're not, and this is for something in production, you'll have to set up a pseudoterminal, which is anything but straightforward.

If you do end up putting it in a script, I'd suggest prefixing it with env, as in env DEBUG_COLORS=1 DEBUG='*' ... for a bit more robustness.

Sorry I couldn't give you a better answer :/

No problem at all, thanks for the quick response! A shame there's no way to detect it reliably but as you say, perhaps it is better to err on the side of caution than to provide output filled with escape codes when they are not supported.

As you suggested, I ended up putting the workaround in package.json as:

"scripts": {
   "test": "DEBUG='*' DEBUG_COLORS=1 tsx --test"
}

And so far so good, so I'll continue with that workaround.

Thanks again!