Skip to content

Commit

Permalink
Runner: improve efficiency of select method
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Jan 21, 2016
1 parent 68068be commit ea551c7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function Runner(opts) {
this.options = opts || {};
this.results = [];
this.tests = [];
this.testsByType = {};
}

util.inherits(Runner, EventEmitter);
Expand All @@ -58,9 +59,16 @@ optionChain(chainableMethods, function (opts, title, fn) {
var Constructor = (opts && /Each/.test(opts.type)) ? Hook : Test;
var test = new Constructor(title, fn);
test.metadata = objectAssign({}, opts);
this.tests.push(test);
this._addTest(test);
}, Runner.prototype);

Runner.prototype._addTest = function (test) {
this.tests.push(test);
var type = test.metadata.type;
var tests = this.testsByType[type] || (this.testsByType[type] = []);
tests.push(test);
};

Runner.prototype._runTestWithHooks = function (test) {
if (test.metadata.skipped) {
return this._addTestResult(test);
Expand All @@ -70,9 +78,9 @@ Runner.prototype._runTestWithHooks = function (test) {
return hook.test(test.title);
}

var tests = this.select({type: 'beforeEach'}).map(hookToTest);
var tests = (this.testsByType.beforeEach || []).map(hookToTest);
tests.push(test);
tests.push.apply(tests, this.select({type: 'afterEach'}).map(hookToTest));
tests.push.apply(tests, (this.testsByType.afterEach || []).map(hookToTest));

var context = {};

Expand Down Expand Up @@ -188,7 +196,9 @@ Runner.prototype.run = function () {
};

Runner.prototype.select = function (filter) {
return this.tests.filter(function (test) {
var tests = filter.type ? this.testsByType[filter.type] || [] : this.tests;

return tests.filter(function (test) {
return Object.keys(filter).every(function (key) {
return filter[key] === test.metadata[key];
});
Expand Down

0 comments on commit ea551c7

Please sign in to comment.