Use Sinon to stub the logger instead of manual stubbing

This commit is contained in:
Jérémie Astori 2018-03-20 01:54:04 -04:00
parent d1548572d4
commit c86ea9463d
No known key found for this signature in database
GPG key ID: B9A4F245CD67BDE8
4 changed files with 36 additions and 52 deletions

View file

@ -13,30 +13,27 @@ function timestamp() {
return colors.dim(time);
}
module.exports = {
/* eslint-disable no-console */
exports.error = function(...args) {
error(...args) {
console.error(timestamp(), colors.red("[ERROR]"), ...args);
};
exports.warn = function(...args) {
},
warn(...args) {
console.error(timestamp(), colors.yellow("[WARN]"), ...args);
};
exports.info = function(...args) {
},
info(...args) {
console.log(timestamp(), colors.blue("[INFO]"), ...args);
};
exports.debug = function(...args) {
},
debug(...args) {
console.log(timestamp(), colors.green("[DEBUG]"), ...args);
};
exports.raw = function(...args) {
},
raw(...args) {
console.log(...args);
};
},
/* eslint-enable no-console */
exports.prompt = (options, callback) => {
prompt(options, callback) {
options.prompt = [timestamp(), colors.cyan("[PROMPT]"), options.text].join(" ");
read(options, callback);
},
};

View file

@ -1,24 +1,21 @@
"use strict";
const expect = require("chai").expect;
const stub = require("sinon").stub;
const TestUtil = require("../../util");
let packages;
describe("packages", function() {
let originalLogInfo;
beforeEach(function() {
originalLogInfo = log.info;
log.info = () => {};
stub(log, "info");
delete require.cache[require.resolve("../../../src/plugins/packages")];
packages = require("../../../src/plugins/packages");
});
afterEach(function() {
log.info = originalLogInfo;
log.info.restore();
});
describe(".getStylesheets", function() {
@ -51,8 +48,9 @@ describe("packages", function() {
describe(".loadPackages", function() {
it("should display report about loading packages", function() {
// Mock `log.info` to extract its effect into a string
log.info.restore();
let stdout = "";
log.info = TestUtil.mockLogger((str) => stdout += str);
stub(log, "info").callsFake(TestUtil.sanitizeLog((str) => stdout += str));
packages.loadPackages();

View file

@ -1,25 +1,20 @@
"use strict";
const expect = require("chai").expect;
const stub = require("sinon").stub;
const TestUtil = require("../../util");
const Utils = require("../../../src/command-line/utils");
describe("Utils", function() {
describe(".extraHelp", function() {
let originalRaw;
beforeEach(function() {
originalRaw = log.raw;
});
afterEach(function() {
log.raw = originalRaw;
log.raw.restore();
});
it("should start and end with empty lines to display correctly with --help", function() {
// Mock `log.raw` to extract its effect into an array
const stdout = [];
log.raw = TestUtil.mockLogger((str) => stdout.push(str));
stub(log, "raw").callsFake(TestUtil.sanitizeLog((str) => stdout.push(str)));
Utils.extraHelp();
@ -36,7 +31,7 @@ describe("Utils", function() {
it("should contain information about THELOUNGE_HOME env var", function() {
// Mock `log.raw` to extract its effect into a concatenated string
let stdout = "";
log.raw = TestUtil.mockLogger((str) => stdout += str);
stub(log, "raw").callsFake(TestUtil.sanitizeLog((str) => stdout += str));
Utils.extraHelp();
@ -119,18 +114,12 @@ describe("Utils", function() {
});
describe("when given the same key multiple times", function() {
let originalWarn;
beforeEach(function() {
originalWarn = log.warn;
});
afterEach(function() {
log.warn = originalWarn;
log.warn.restore();
});
it("should not override options", function() {
log.warn = () => {};
stub(log, "warn");
expect(Utils.parseConfigOptions("foo=baz", {foo: "bar"}))
.to.deep.equal({foo: "bar"});
@ -138,7 +127,7 @@ describe("Utils", function() {
it("should display a warning", function() {
let warning = "";
log.warn = TestUtil.mockLogger((str) => warning += str);
stub(log, "warn").callsFake(TestUtil.sanitizeLog((str) => warning += str));
Utils.parseConfigOptions("foo=bar", {foo: "baz"});

View file

@ -24,7 +24,7 @@ MockClient.prototype.createMessage = function(opts) {
return message;
};
function mockLogger(callback) {
function sanitizeLog(callback) {
return function(...args) {
// Concats and removes ANSI colors. See https://stackoverflow.com/a/29497680
const stdout = args.join(" ").replace(
@ -51,5 +51,5 @@ module.exports = {
createWebserver() {
return express();
},
mockLogger,
sanitizeLog,
};