Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 101 additions & 103 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ function getBrowserWindowSize() {
return [640, 480];
}

/**
* Expose `Base`.
*/

exports = module.exports = Base;

/**
* Check if both stdio streams are associated with a tty.
*/
Expand All @@ -49,9 +43,109 @@ var isatty = isBrowser || (process.stdout.isTTY && process.stderr.isTTY);
var consoleLog = console.log;

/**
* Enable coloring by default, except in the browser interface.
* @abstract
* @description
* All other reporters generally inherit from this reporter.
*/
class Base {
/**
* Constructs a new `Base` reporter instance.
*
* @public
* @memberof Mocha.reporters
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
constructor(runner, options) {
var failures = (this.failures = []);

if (!runner) {
throw new TypeError("Missing runner argument");
}
this.options = options || {};
this.runner = runner;
this.stats = runner.stats; // assigned so Reporters keep a closer reference

var maxDiffSizeOpt =
this.options.reporterOption && this.options.reporterOption.maxDiffSize;
if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) {
exports.maxDiffSize = Number(maxDiffSizeOpt);
}

runner.on(EVENT_TEST_PASS, function (test) {
if (test.duration > test.slow()) {
test.speed = "slow";
} else if (test.duration > test.slow() / 2) {
test.speed = "medium";
} else {
test.speed = "fast";
}
});

runner.on(EVENT_TEST_FAIL, function (test, err) {
if (showDiff(err)) {
stringifyDiffObjs(err);
}
// more than one error per test
if (test.err && err instanceof Error) {
test.err.multiple = (test.err.multiple || []).concat(err);
} else {
test.err = err;
}
failures.push(test);
});
}

/**
* Outputs common epilogue used by many of the bundled reporters.
*
* @public
* @memberof Mocha.reporters
*/
epilogue() {
var stats = this.stats;
var fmt;

Base.consoleLog();

// passes
fmt =
color("bright pass", " ") +
color("green", " %d passing") +
color("light", " (%s)");

Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration));

// pending
if (stats.pending) {
fmt = color("pending", " ") + color("pending", " %d pending");

Base.consoleLog(fmt, stats.pending);
}

// failures
if (stats.failures) {
fmt = color("fail", " %d failing");

Base.consoleLog(fmt, stats.failures);

Base.list(this.failures);
Base.consoleLog();
}

Base.consoleLog();
}
}

Base.consoleLog = consoleLog;

Base.abstract = true;

exports = module.exports = Base;

/**
* Enable coloring by default, except in the browser interface.
*/
exports.useColors =
!isBrowser &&
(supportsColor.stdout || process.env.MOCHA_COLORS !== undefined);
Expand Down Expand Up @@ -347,98 +441,6 @@ exports.list = function (failures) {
});
};

/**
* Constructs a new `Base` reporter instance.
*
* @description
* All other reporters generally inherit from this reporter.
*
* @public
* @class
* @memberof Mocha.reporters
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
function Base(runner, options) {
var failures = (this.failures = []);

if (!runner) {
throw new TypeError("Missing runner argument");
}
this.options = options || {};
this.runner = runner;
this.stats = runner.stats; // assigned so Reporters keep a closer reference

var maxDiffSizeOpt =
this.options.reporterOption && this.options.reporterOption.maxDiffSize;
if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) {
exports.maxDiffSize = Number(maxDiffSizeOpt);
}

runner.on(EVENT_TEST_PASS, function (test) {
if (test.duration > test.slow()) {
test.speed = "slow";
} else if (test.duration > test.slow() / 2) {
test.speed = "medium";
} else {
test.speed = "fast";
}
});

runner.on(EVENT_TEST_FAIL, function (test, err) {
if (showDiff(err)) {
stringifyDiffObjs(err);
}
// more than one error per test
if (test.err && err instanceof Error) {
test.err.multiple = (test.err.multiple || []).concat(err);
} else {
test.err = err;
}
failures.push(test);
});
}

/**
* Outputs common epilogue used by many of the bundled reporters.
*
* @public
* @memberof Mocha.reporters
*/
Base.prototype.epilogue = function () {
var stats = this.stats;
var fmt;

Base.consoleLog();

// passes
fmt =
color("bright pass", " ") +
color("green", " %d passing") +
color("light", " (%s)");

Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration));

// pending
if (stats.pending) {
fmt = color("pending", " ") + color("pending", " %d pending");

Base.consoleLog(fmt, stats.pending);
}

// failures
if (stats.failures) {
fmt = color("fail", " %d failing");

Base.consoleLog(fmt, stats.failures);

Base.list(this.failures);
Base.consoleLog();
}

Base.consoleLog();
};

/**
* Pads the given `str` to `len`.
*
Expand Down Expand Up @@ -585,7 +587,3 @@ var objToString = Object.prototype.toString;
function sameType(a, b) {
return objToString.call(a) === objToString.call(b);
}

Base.consoleLog = consoleLog;

Base.abstract = true;
141 changes: 71 additions & 70 deletions lib/reporters/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,82 +19,83 @@ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
var EVENT_SUITE_END = constants.EVENT_SUITE_END;

/**
* Expose `Doc`.
*/

exports = module.exports = Doc;

/**
* Constructs a new `Doc` reporter instance.
*
* @public
* @class
* @memberof Mocha.reporters
* @extends Mocha.reporters.Base
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
function Doc(runner, options) {
Base.call(this, runner, options);
class Doc extends Base {
static description = "HTML documentation";

var indents = 2;
/**
* Constructs a new `Doc` reporter instance.
*
* @public
* @memberof Mocha.reporters
* @extends Mocha.reporters.Base
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
constructor(runner, options) {
super(runner, options);

function indent() {
return Array(indents).join(" ");
}
var indents = 2;

runner.on(EVENT_SUITE_BEGIN, function (suite) {
if (suite.root) {
return;
function indent() {
return Array(indents).join(" ");
}
++indents;
Base.consoleLog('%s<section class="suite">', indent());
++indents;
Base.consoleLog("%s<h1>%s</h1>", indent(), utils.escape(suite.title));
Base.consoleLog("%s<dl>", indent());
});

runner.on(EVENT_SUITE_END, function (suite) {
if (suite.root) {
return;
}
Base.consoleLog("%s</dl>", indent());
--indents;
Base.consoleLog("%s</section>", indent());
--indents;
});
runner.on(EVENT_SUITE_BEGIN, function (suite) {
if (suite.root) {
return;
}
++indents;
Base.consoleLog('%s<section class="suite">', indent());
++indents;
Base.consoleLog("%s<h1>%s</h1>", indent(), utils.escape(suite.title));
Base.consoleLog("%s<dl>", indent());
});

runner.on(EVENT_TEST_PASS, function (test) {
Base.consoleLog("%s <dt>%s</dt>", indent(), utils.escape(test.title));
Base.consoleLog("%s <dt>%s</dt>", indent(), utils.escape(test.file));
var code = utils.escape(utils.clean(test.body));
Base.consoleLog("%s <dd><pre><code>%s</code></pre></dd>", indent(), code);
});
runner.on(EVENT_SUITE_END, function (suite) {
if (suite.root) {
return;
}
Base.consoleLog("%s</dl>", indent());
--indents;
Base.consoleLog("%s</section>", indent());
--indents;
});

runner.on(EVENT_TEST_FAIL, function (test, err) {
Base.consoleLog(
'%s <dt class="error">%s</dt>',
indent(),
utils.escape(test.title),
);
Base.consoleLog(
'%s <dt class="error">%s</dt>',
indent(),
utils.escape(test.file),
);
var code = utils.escape(utils.clean(test.body));
Base.consoleLog(
'%s <dd class="error"><pre><code>%s</code></pre></dd>',
indent(),
code,
);
Base.consoleLog(
'%s <dd class="error">%s</dd>',
indent(),
utils.escape(err),
);
});
runner.on(EVENT_TEST_PASS, function (test) {
Base.consoleLog("%s <dt>%s</dt>", indent(), utils.escape(test.title));
Base.consoleLog("%s <dt>%s</dt>", indent(), utils.escape(test.file));
var code = utils.escape(utils.clean(test.body));
Base.consoleLog(
"%s <dd><pre><code>%s</code></pre></dd>",
indent(),
code,
);
});

runner.on(EVENT_TEST_FAIL, function (test, err) {
Base.consoleLog(
'%s <dt class="error">%s</dt>',
indent(),
utils.escape(test.title),
);
Base.consoleLog(
'%s <dt class="error">%s</dt>',
indent(),
utils.escape(test.file),
);
var code = utils.escape(utils.clean(test.body));
Base.consoleLog(
'%s <dd class="error"><pre><code>%s</code></pre></dd>',
indent(),
code,
);
Base.consoleLog(
'%s <dd class="error">%s</dd>',
indent(),
utils.escape(err),
);
});
}
}

Doc.description = "HTML documentation";
exports = module.exports = Doc;
Loading