Browse Source

clean up stat collection in runner (#728)

Follow-up to #654. No longer collecting stats for every test that completes.
Explicitly return `null` when no exclusive tests could be run.
browser-support
Mark Wubben 9 years ago
committed by James Talmage
parent
commit
e33ce7a458
  1. 34
      lib/runner.js
  2. 18
      test/hooks.js
  3. 55
      test/runner.js

34
lib/runner.js

@ -47,6 +47,7 @@ function Runner(options) {
this._serial = options.serial; this._serial = options.serial;
this._match = options.match || []; this._match = options.match || [];
this._addTestResult = this._addTestResult.bind(this); this._addTestResult = this._addTestResult.bind(this);
this._buildStats = this._buildStats.bind(this);
} }
util.inherits(Runner, EventEmitter); util.inherits(Runner, EventEmitter);
@ -89,21 +90,6 @@ optionChain(chainableMethods, function (opts, title, fn) {
Runner.prototype._addTestResult = function (result) { Runner.prototype._addTestResult = function (result) {
var test = result.result; var test = result.result;
if (test.metadata.type === 'test') {
this.stats.testCount++;
if (test.metadata.todo) {
this.stats.todoCount++;
} else if (test.metadata.skipped) {
this.stats.skipCount++;
}
}
if (!result.passed) {
this.stats.failCount++;
}
var props = { var props = {
duration: test.duration, duration: test.duration,
title: test.title, title: test.title,
@ -155,25 +141,11 @@ Runner.prototype._buildStats = function () {
}; };
Runner.prototype.run = function (options) { Runner.prototype.run = function (options) {
var self = this;
this.stats = {
failCount: 0,
passCount: 0,
skipCount: 0,
todoCount: 0,
testCount: 0
};
if (options.runOnlyExclusive && !this.tests.hasExclusive) { if (options.runOnlyExclusive && !this.tests.hasExclusive) {
return Promise.resolve(); return Promise.resolve(null);
} }
this.tests.on('test', this._addTestResult); this.tests.on('test', this._addTestResult);
return Promise.resolve(this.tests.build(this._bail).run()).then(function () { return Promise.resolve(this.tests.build(this._bail).run()).then(this._buildStats);
self.stats = self._buildStats();
return self.stats;
});
}; };

18
test/hooks.js

@ -51,9 +51,9 @@ test('after', function (t) {
arr.push('a'); arr.push('a');
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.passCount, 1); t.is(stats.passCount, 1);
t.is(runner.stats.failCount, 0); t.is(stats.failCount, 0);
t.same(arr, ['a', 'b']); t.same(arr, ['a', 'b']);
t.end(); t.end();
}); });
@ -158,8 +158,8 @@ test('fail if beforeEach hook fails', function (t) {
a.pass(); a.pass();
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.failCount, 1); t.is(stats.failCount, 1);
t.same(arr, ['a']); t.same(arr, ['a']);
t.end(); t.end();
}); });
@ -282,8 +282,8 @@ test('shared context', function (t) {
a.deepEqual(a.context.arr, ['a', 'b', 'c']); a.deepEqual(a.context.arr, ['a', 'b', 'c']);
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.failCount, 0); t.is(stats.failCount, 0);
t.end(); t.end();
}); });
}); });
@ -301,8 +301,8 @@ test('shared context of any type', function (t) {
a.is(a.context, 'foo'); a.is(a.context, 'foo');
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.failCount, 0); t.is(stats.failCount, 0);
t.end(); t.end();
}); });
}); });

55
test/runner.js

@ -194,10 +194,10 @@ test('skip test', function (t) {
runner.skip('should be a todo'); runner.skip('should be a todo');
}, {message: 'Expected an implementation. Use `test.todo()` for tests without an implementation.'}); }, {message: 'Expected an implementation. Use `test.todo()` for tests without an implementation.'});
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.testCount, 2); t.is(stats.testCount, 2);
t.is(runner.stats.passCount, 1); t.is(stats.passCount, 1);
t.is(runner.stats.skipCount, 1); t.is(stats.skipCount, 1);
t.same(arr, ['a']); t.same(arr, ['a']);
t.end(); t.end();
}); });
@ -233,10 +233,10 @@ test('todo test', function (t) {
runner.todo(); runner.todo();
}, {message: '`todo` tests require a title'}); }, {message: '`todo` tests require a title'});
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.testCount, 2); t.is(stats.testCount, 2);
t.is(runner.stats.passCount, 1); t.is(stats.passCount, 1);
t.is(runner.stats.todoCount, 1); t.is(stats.todoCount, 1);
t.same(arr, ['a']); t.same(arr, ['a']);
t.end(); t.end();
}); });
@ -256,16 +256,16 @@ test('only test', function (t) {
arr.push('b'); arr.push('b');
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.testCount, 1); t.is(stats.testCount, 1);
t.is(runner.stats.passCount, 1); t.is(stats.passCount, 1);
t.same(arr, ['b']); t.same(arr, ['b']);
t.end(); t.end();
}); });
}); });
test('runOnlyExclusive option test', function (t) { test('runOnlyExclusive option test', function (t) {
t.plan(4); t.plan(1);
var runner = new Runner(); var runner = new Runner();
var options = {runOnlyExclusive: true}; var options = {runOnlyExclusive: true};
@ -275,11 +275,8 @@ test('runOnlyExclusive option test', function (t) {
arr.push('a'); arr.push('a');
}); });
runner.run(options).then(function () { runner.run(options).then(function (stats) {
t.is(runner.stats.failCount, 0); t.is(stats, null);
t.is(runner.stats.passCount, 0);
t.is(runner.stats.skipCount, 0);
t.is(runner.stats.testCount, 0);
t.end(); t.end();
}); });
}); });
@ -416,10 +413,10 @@ test('options.match will not run tests with non-matching titles', function (t) {
t.fail(); t.fail();
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.skipCount, 0); t.is(stats.skipCount, 0);
t.is(runner.stats.passCount, 2); t.is(stats.passCount, 2);
t.is(runner.stats.testCount, 2); t.is(stats.testCount, 2);
t.end(); t.end();
}); });
}); });
@ -441,10 +438,10 @@ test('options.match hold no effect on hooks with titles', function (t) {
t.is(actual, 'foo'); t.is(actual, 'foo');
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.skipCount, 0); t.is(stats.skipCount, 0);
t.is(runner.stats.passCount, 1); t.is(stats.passCount, 1);
t.is(runner.stats.testCount, 1); t.is(stats.testCount, 1);
t.end(); t.end();
}); });
}); });
@ -464,10 +461,10 @@ test('options.match overrides .only', function (t) {
t.pass(); t.pass();
}); });
runner.run({}).then(function () { runner.run({}).then(function (stats) {
t.is(runner.stats.skipCount, 0); t.is(stats.skipCount, 0);
t.is(runner.stats.passCount, 2); t.is(stats.passCount, 2);
t.is(runner.stats.testCount, 2); t.is(stats.testCount, 2);
t.end(); t.end();
}); });
}); });

Loading…
Cancel
Save