I'm using Mocha for testing my apps. Currently, I'm using Makefiles, but I want to switch to Cakefiles. When I run my test through Cake, the colors from Mocha are not displayed on console.
Here's an example:
task 'test', 'test project', (options) ->
fetchTestFiles './test', (err,files) ->
mocha = spawn 'mocha', files
mocha.stdout.pipe(process.stdout, end: false)
Now, I tried doing this:
task 'test', 'test project', (options) ->
fetchTestFiles './test', (err,files) ->
mocha = spawn 'mocha', files, customFds: [0..2]
This works, however the Node.js docs claim that customFds
is deprecated. Is there a way to pass through the colors without using customFds
?
The problem is that mocha, like many command line programs, will disable color output if it sees that the thing it is outputting to is not a terminal. It does this because otherwise if you piped to a file, the file would be full of color control sequences along with the displayed text.
So the issue isn't really with node, and more with mocha. The solution in this case is that mocha has a --colors
argument to force color output. So this should work.
task 'test', 'test project', (options) ->
fetchTestFiles './test', (err,files) ->
files.unshift '--colors'
mocha = spawn 'mocha', files
mocha.stdout.pipe(process.stdout, end: false)
Normally when you spawn a process, new data streams are allocated for the child's stdin, stdout, and stderr. That means that mocha will check these streams, see that they are not terminals, and disable colors. The reason that customFds
works is that it passes the exact streams that node gets at process.stdin
, process.stdout
, and process.stderr
to the child process, so when mocha checks if the streams are terminals, they will be because the streams are the actual terminal.
I know you're looking for another solution, but I don't think you'll find one. You either explicitly tell mocha to use colors, or mocha needs to think that it's streams are terminals, and the only real way to do that is via customFds
, which is deprecated as you said.
Node.js docs claim that customFds
is deprecated. Is there a way to pass through the colors without using customFds
?
Newer versions of Node specify file descriptors with the stdio
attribute of spawn's options
object.
http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
stdio
even includes a handy 'inherit' value for passing the parent's file descriptors for stdout, stdin, and stderr to the spawned process. Using 'stdio', the original call...
mocha = spawn 'mocha', files, customFds: [0..2]
...would become...
mocha = spawn 'mocha', files, stdio: 'inherit'
As loganfsmyth explained, this will pass the exact streams to the child process, negating the need for any additional code to redirect mocha's output.