Skip to content

Color codes output to non-ttys #1304

@larkost

Description

@larkost

Like the writer of #1296 I am piping the output of Mocha tests to a non-tty (file in this case), and am getting control characters (specifically those for colors) in my text. This is nasty as I am trying to support unicode characters, and that makes life difficult.

The reports code appears correct, but I tracked the problem down to a combination of two places in lib/mocha.js:

  1. in Mocha.prototype.useColors the default if there is no setting is to be true, if you have selected neither -c/--colors or -C/--no-colors this gets called called by mocha.js with undefined in the line this.useColors(options.useColors), so this.options.useColors is true.
  2. In Mocha.prototype.run there is the line:
exports.reporters.Base.useColors = options.useColors;

Since this does not get run until the Mocha object is instantiated, it is run later than the code in the top-level of the the reports modules. So this one always overrides what is set in reporters modules, and always sets it to true, unless you explicitly call -C/--no-colors.

A correction would be to change that last line to:

if (exports.reporters.Base.useColors == undefined) exports.reporters.Base.useColors = options.useColors;

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: accepting prsMocha can use your help with this one!type: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions