Provide Karma with updated dependencies on change

This commit is contained in:
Tim Schaub
2017-07-03 12:26:12 -06:00
parent 32c26c2524
commit 8e91d51d7c
2 changed files with 43 additions and 9 deletions

View File

@@ -3,6 +3,39 @@ const closure = require('closure-util');
const path = require('path');
const processCliArgs = require('karma/lib/cli').process;
function insertDependencies(manager, files, previousLookup) {
previousLookup = previousLookup || {};
let firstIndex = NaN;
const original = files.filter((obj, index) => {
if (previousLookup[obj.pattern]) {
if (isNaN(firstIndex)) {
firstIndex = index;
}
return false;
} else {
return true;
}
});
if (isNaN(firstIndex)) {
firstIndex = 0;
}
const lookup = {};
const dependencies = manager.getDependencies().map(script => {
lookup[script.path] = true;
return {
pattern: script.path,
included: true,
served: true,
watched: false
};
});
original.splice.apply(original, [firstIndex, 0].concat(dependencies));
files.length = 0;
files.push.apply(files, original);
return lookup;
}
/**
* Start Karma. This prepends the Karma `files` config with all library files
* sorted in dependency order.
@@ -22,15 +55,8 @@ function serve(config, manager, callback) {
const server = new Server(config, exit);
const files = server.get('config.files');
const dependencies = manager.getDependencies().map(script => script.path);
dependencies.reverse().forEach(filePath => {
files.unshift({
pattern: filePath,
included: true,
served: true,
watched: true
});
});
let lookup = insertDependencies(manager, files);
// stop goog base.js from trying to load deps.js
files.unshift({
@@ -40,6 +66,11 @@ function serve(config, manager, callback) {
watched: false
});
manager.on('update', () => {
lookup = insertDependencies(manager, files, lookup);
server.refreshFiles();
});
server.start();
}

View File

@@ -12,6 +12,9 @@ var path = require('path');
module.exports = function(karma) {
karma.set({
frameworks: ['mocha'],
client: {
runInParent: true
},
files: [
{
pattern: path.resolve(__dirname, require.resolve('jquery/dist/jquery.js')),