thelounge/test/src/command-line/utilsTest.js
Jérémie Astori b90c224a99
Define a raw logger to avoid using console.log, use it in extra help for environment variables, and add a test for this
This has multiple benefits:

- Respects the "Do not mock what you do not own" principle, instead we mock `log.raw` when necessary
- Lets us not re-assign `console.log`, which breaks as Mocha uses `console.log` as well
- Save and restore initial `log.raw` in test hooks (before/after), otherwise this would break Mocha/Chai
2017-12-11 23:48:51 -05:00

47 lines
1.2 KiB
JavaScript

"use strict";
const expect = require("chai").expect;
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;
});
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));
Utils.extraHelp();
// Starts with 2 empty lines
expect(stdout[0]).to.equal("\n");
expect(stdout[1]).to.equal("\n");
expect(stdout[2]).to.not.equal("\n");
// Ends with 1 empty line
expect(stdout[stdout.length - 2]).to.not.equal("\n");
expect(stdout[stdout.length - 1]).to.equal("\n");
});
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);
Utils.extraHelp();
expect(stdout).to.include("THELOUNGE_HOME");
});
});
});