Skip to content

Commit

Permalink
Clean up strategies and make sure to comply with mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
joeljeske committed Jan 16, 2018
1 parent 8972e61 commit 602c93c
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions lib/karma-parallelizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ function initKarmaParallelizer(root, shardIndexInfo) {
return;
}

const overrideSpecSuite = getSpecSuiteOverrider(shardIndexInfo);
['describe', 'ddescribe', 'fdescribe'].forEach(function(methodName) {
replaceMethod(root, methodName, overrideSpecSuite);
const strategy = getSpecSuiteStrategy(shardIndexInfo);
const overrideSpecSuite = createSpecSuiteOverrider(strategy);

// Mocha uses describe.only|skip
// Jasmine uses fdescribe|ddescribe|xdescribe

replaceMethod(root, 'describe', function(method) {
const overriden = overrideSpecSuite(method);
replaceMethod(overriden, 'only', overrideSpecSuite);
replaceMethod(overriden, 'skip', overrideSpecSuite);
return overriden;
});
replaceMethod(root, 'xdescribe', overrideSpecSuite);
replaceMethod(root, 'fdescribe', overrideSpecSuite);
replaceMethod(root, 'ddescribe', overrideSpecSuite);
}

function replaceMethod(root, methodName, overrider) {
Expand All @@ -35,23 +46,23 @@ function replaceMethod(root, methodName, overrider) {
}
}

function getSpecSuiteOverrider(shardIndexInfo) {
function getSpecSuiteStrategy(shardIndexInfo) {
switch (shardIndexInfo.shardStrategy) {
case 'description-length':
return createDescriptionLengthSpecSuiteOverrider(shardIndexInfo);
return createDescriptionLengthStragegy(shardIndexInfo);
case 'round-robin':
/* falls through */
default:
return createRoundRobinBasedSpecSuiteOverrider(shardIndexInfo);
return createRoundRobinStrategy(shardIndexInfo);
}
}

function createDescriptionLengthSpecSuiteOverrider({shardIndex, executors}) {
function createSpecSuiteOverrider(strategy) {
let depth = 0;
return function overrideSpecSuite(origDescribe) {
return function(description, specDefinitions) {
// If we are a top-level describe but our description doesn't match our responsibility, skip it
if (depth === 0 && description.length % executors !== shardIndex) {
// If we are a top-level, ask our strategy if we should be interested in this suite
if (depth === 0 && !strategy(description, specDefinitions)) {
// console.log('[skipping]', description);
} else {
origDescribe(description, function() {
Expand All @@ -64,23 +75,18 @@ function createDescriptionLengthSpecSuiteOverrider({shardIndex, executors}) {
};
}

function createRoundRobinBasedSpecSuiteOverrider({shardIndex, executors}) {
let depth = 0;
let count = 0;
function createDescriptionLengthStragegy({shardIndex, executors}) {
return function overrideSpecSuite(description/*, specDefinitions*/) {
return description.length % executors === shardIndex;
};
}

return function overrideSpecSuite(origDescribe) {
return function(description, specDefinitions) {
// If we are a top-level describe but our description doesn't match our responsibility, skip it
if (depth === 0 && count++ % executors !== shardIndex) {
// console.log('[skipping]', description);
} else {
origDescribe(description, function() {
depth++;
specDefinitions();
depth--;
});
}
};
function createRoundRobinStrategy({shardIndex, executors}) {
// Increment the count on each top level describe to determine
// round-robin responsibility
let count = 0;
return function(/*description, specDefinitions*/) {
return count++ % executors === shardIndex;
};
}

Expand Down

0 comments on commit 602c93c

Please sign in to comment.