Skip to content

Commit

Permalink
Use supports-color module to test if system supports colors
Browse files Browse the repository at this point in the history
This fixes #1574
  • Loading branch information
mroderick authored and fatso83 committed Oct 6, 2017
1 parent 78782b4 commit 527086a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/sinon/color.js
@@ -1,9 +1,9 @@
"use strict";

var canColor = typeof process !== "undefined";
var supportsColor = require("supports-color");

function colorize(str, color) {
if (!canColor) {
if (!supportsColor) {
return str;
}

Expand Down
77 changes: 71 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"lodash.get": "^4.4.2",
"lolex": "^2.1.3",
"nise": "^1.1.1",
"supports-color": "^4.4.0",
"type-detect": "^4.0.0"
},
"devDependencies": {
Expand Down
58 changes: 58 additions & 0 deletions test/util/core/color-test.js
@@ -0,0 +1,58 @@
"use strict";

var assert = require("referee").assert;
var proxyquire = require("proxyquire");

function getColorMethods() {
return [{
name: "red",
code: 31
}, {
name: "green",
code: 32
}];
}

describe("color", function () {
describe("when environment supports color", function () {
var color;

beforeEach(function () {
color = proxyquire("../../../lib/sinon/color", {
"supports-color": {}
});
});

getColorMethods().forEach(function (method) {
describe(method.name, function () {
it("should return a colored string", function () {
var string = "lorem ipsum";
var actual = color[method.name](string);

assert.isTrue(actual.indexOf(method.code + "m" + string) !== -1);
});
});
});
});

describe("when environment does not support color", function () {
var color;

beforeEach(function () {
color = proxyquire("../../../lib/sinon/color", {
"supports-color": false
});
});

getColorMethods().forEach(function (method) {
describe(method.name, function () {
it("should return a colored string", function () {
var string = "lorem ipsum";
var actual = color[method.name](string);

assert.equals(actual, string);
});
});
});
});
});

0 comments on commit 527086a

Please sign in to comment.