Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor statistics #3458

Merged
merged 2 commits into from
Dec 4, 2018
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
45 changes: 3 additions & 42 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,7 @@ exports.list = function(failures) {
* Initialize a new `Base` reporter.
*
* All other reporters generally
* inherit from this reporter, providing
* stats such as test duration, number
* of tests passed / failed etc.
* inherit from this reporter.
*
* @memberof Mocha.reporters
* @public
Expand All @@ -283,68 +281,31 @@ exports.list = function(failures) {
*/

function Base(runner) {
var stats = (this.stats = {
suites: 0,
tests: 0,
passes: 0,
pending: 0,
failures: 0
});
var failures = (this.failures = []);

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

runner.stats = stats;

runner.on('start', function() {
stats.start = new Date();
});

runner.on('suite', function(suite) {
stats.suites = stats.suites || 0;
suite.root || stats.suites++;
});

runner.on('test end', function() {
stats.tests = stats.tests || 0;
stats.tests++;
});

runner.on('pass', function(test) {
stats.passes = stats.passes || 0;

if (test.duration > test.slow()) {
test.speed = 'slow';
} else if (test.duration > test.slow() / 2) {
test.speed = 'medium';
} else {
test.speed = 'fast';
}

stats.passes++;
});

runner.on('fail', function(test, err) {
stats.failures = stats.failures || 0;
stats.failures++;
if (showDiff(err)) {
stringifyDiffObjs(err);
}
test.err = err;
failures.push(test);
});

runner.once('end', function() {
stats.end = new Date();
stats.duration = stats.end - stats.start;
});

runner.on('pending', function() {
stats.pending++;
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var utils = require('./utils');
var inherits = utils.inherits;
var debug = require('debug')('mocha:runner');
var Runnable = require('./runnable');
var createStatsCollector = require('./stats-collector');
var stackFilter = utils.stackTraceFilter();
var stringify = utils.stringify;
var type = utils.type;
Expand Down Expand Up @@ -81,6 +82,7 @@ function Runner(suite, delay) {
this._defaultGrep = /.*/;
this.grep(this._defaultGrep);
this.globals(this.globalProps().concat(extraGlobals()));
createStatsCollector(this);
}

/**
Expand Down
70 changes: 70 additions & 0 deletions lib/stats-collector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

plroebuck marked this conversation as resolved.
Show resolved Hide resolved
/**
* Test statistics collector.
*
* @typedef {Object} StatsCollector
* @property {number} suites - integer count of suites run.
* @property {number} tests - integer count of tests run.
* @property {number} passes - integer count of passing tests.
* @property {number} pending - integer count of pending tests.
* @property {number} failures - integer count of failed tests.
* @property {Date} start - time when testing began.
* @property {Date} end - time when testing concluded.
* @property {number} duration - number of msecs that testing took.
*/

/**
* Provides stats such as test duration,
* number of tests passed / failed etc.
*
* @public
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
* @memberof Mocha
* @param {Runner} runner
*/
function createStatsCollector(runner) {
var stats = {
suites: 0,
tests: 0,
passes: 0,
pending: 0,
failures: 0
};

if (!runner) {
throw new TypeError('Missing runner argument');
}

runner.stats = stats;

runner.once('start', function() {
stats.start = new Date();
});

runner.on('suite', function(suite) {
suite.root || stats.suites++;
});

runner.on('pass', function() {
stats.passes++;
});

runner.on('fail', function() {
stats.failures++;
});

runner.on('pending', function() {
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
stats.pending++;
});

runner.on('test end', function() {
stats.tests++;
});

runner.once('end', function() {
stats.end = new Date();
stats.duration = stats.end - stats.start;
});
}

module.exports = createStatsCollector;
6 changes: 5 additions & 1 deletion test/reporters/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
scope of this function for the tests to run properly.
*/

var createStatsCollector = require('../../lib/stats-collector');

function createMockRunner(runStr, ifStr1, ifStr2, ifStr3, arg1, arg2) {
var runnerFunction = createRunnerFunction(
runStr,
Expand All @@ -17,10 +19,12 @@ function createMockRunner(runStr, ifStr1, ifStr2, ifStr3, arg1, arg2) {
arg1,
arg2
);
return {
var mockRunner = {
on: runnerFunction,
once: runnerFunction
};
createStatsCollector(mockRunner);
return mockRunner;
}

function createRunnerFunction(runStr, ifStr1, ifStr2, ifStr3, arg1, arg2) {
Expand Down
7 changes: 6 additions & 1 deletion test/reporters/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('List reporter', function() {
duration: expectedDuration,
slow: function() {}
};

beforeEach(function() {
useColors = Base.useColors;
Base.useColors = false;
Expand Down Expand Up @@ -126,7 +127,11 @@ describe('List reporter', function() {
test = {};
runner = createMockRunner('fail', 'fail', null, null, test);
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
runner.on = runner.once = function(event, callback) {
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
if (!checked && event === 'fail') {
if (
!checked &&
event === 'fail' &&
callback.toString().includes('stringifyDiffObjs') // target correct fail event callback
) {
err = new Error('fake failure object with actual/expected');
err.actual = actual;
err.expected = expected;
Expand Down
14 changes: 6 additions & 8 deletions test/reporters/xunit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var assert = require('assert');
var createStatsCollector = require('../../lib/stats-collector');
var reporters = require('../../').reporters;
var XUnit = reporters.XUnit;

describe('XUnit reporter', function() {
var stdout;
var stdoutWrite;
// the runner parameter of the reporter
var runner;

var callbackArgument = null;
Expand All @@ -26,6 +28,7 @@ describe('XUnit reporter', function() {
beforeEach(function() {
stdout = [];
runner = {on: function() {}, once: function() {}};
createStatsCollector(runner);
});

describe('if reporter options output is given', function() {
Expand Down Expand Up @@ -322,8 +325,6 @@ describe('XUnit reporter', function() {
describe('custom suite name', function() {
// capture the events that the reporter subscribes to
var events;
// the runner parameter of the reporter
var runner;
// capture output lines (will contain the resulting XML of the xunit reporter)
var lines;
// the file stream into which the xunit reporter will write into
Expand All @@ -332,13 +333,10 @@ describe('XUnit reporter', function() {
beforeEach(function() {
events = {};

runner = {
on: function(eventName, eventHandler) {
// capture the event handler
events[eventName] = eventHandler;
}
runner.on = runner.once = function(eventName, eventHandler) {
// capture the event handler
events[eventName] = eventHandler;
};
runner.once = runner.on;

lines = [];
fileStream = {
Expand Down