Skip to content

Commit

Permalink
Merge pull request #2 from jbubsk/entry-files
Browse files Browse the repository at this point in the history
feat(entry files): allows to redefine sets of the specs for distribut…
  • Loading branch information
rschuft authored Jan 26, 2018
2 parents 7c89353 + b17ea48 commit f6c9058
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ You can pass configuration to override these defaults:
{
sharding: {
specMatcher: /(spec|test)s?\.js/i,
base: '/base'
base: '/base',
getSets: function(config, basePath, files) {
// splitForBrowsers - some util function
return splitForBrowsers(files.served)
.map(oneBrowserSet => [someInitScript].concat(oneBrowserSet));
}
}
}
```
`getSets` might be overridden when files from `karma.files` config should be organized in certain view, for example:

`karma.files` has set - `[setup.js, one.unit.js, two.unit.js]` - and it must be combined for each browser as `[setup.js, one.unit.js]` and `[setup.js, two.unit.js]`

## Installation

Expand Down
23 changes: 8 additions & 15 deletions lib/framework.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const _ = require('lodash');

const { isSpecFile } = require('./matchers');
const { isSpecFile, splitArray } = require('./utils');

function getConfig(fullConfig) {
// ensure we can manipulate config settings
Expand All @@ -10,6 +9,7 @@ function getConfig(fullConfig) {
config.base = config.base ? config.base : '/base';
config.sets = [];
config.indexMap = {};
config.getSets = config.getSets ? config.getSets : getSets;
return config;
}

Expand All @@ -27,23 +27,16 @@ function setupCoverageReporting(fullConfig) {
fullConfig.coverageReporter.browserId = 'name';
}

// modified from example at:
// https://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays
function splitArray(flatArray, numCols){
const maxColLength = Math.ceil(flatArray.length/numCols);
const nestedArray = _.chunk(flatArray, maxColLength);
for (var i = nestedArray.length; i < numCols; i++) {
nestedArray.push([]);
}
return nestedArray;
function setupSets(config, basePath, files) {
config.sets = config.getSets(config, basePath, files);
config.performSharding = _.last(config.sets).length;
}

function setupSets(config, basePath, files) {
// can be redefined in sharding config
function getSets(config, basePath, files) {
var specs = files.served.map(f => config.base + f.path.replace(basePath, ''))
.filter(p => isSpecFile(p, config.specMatcher));
config.sets = splitArray(specs, config.browserCount);
var lastSet = config.sets && config.sets.length && config.sets[config.sets.length - 1];
config.performSharding = (lastSet && lastSet.length);
return splitArray(specs, config.browserCount);
}

function setupSharding(config, fullConfig, log) {
Expand Down
9 changes: 0 additions & 9 deletions lib/matchers.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { isSpecFile } = require('./matchers');
const { isSpecFile } = require('./utils');

const idParamExtractor = /\/\?id=(\d+)/;
const idCookieExtractor = /browser.id=(\d+)/;
Expand Down
22 changes: 22 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const _ = require('lodash');

// modified from example at:
// https://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays
function splitArray(flatArray, numCols){
var maxColLength = Math.ceil(flatArray.length/numCols);
var nestedArray = _.chunk(flatArray, maxColLength);
for (var i = nestedArray.length; i < numCols; i++) {
nestedArray.push([]);
}
return nestedArray;
}

function isSpecFile(url, matcher) {
return (
url.indexOf('/bower_components/') === -1 &&
url.indexOf('/node_modules/') === -1 &&
matcher.test(url)
);
}

module.exports = { isSpecFile, splitArray };
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"name": "Ryan Schuft",
"email": "[email protected]",
"url": "https://github.com/rschuft"
},
{
"name": "Mikhail Zheludev",
"email": "[email protected]",
"url": "https://github.com/jbubsk"
}
],
"maintainers": [
Expand Down

0 comments on commit f6c9058

Please sign in to comment.