From 8e91d51d7c99b60923f988f73ea75b8812f990a0 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 3 Jul 2017 12:26:12 -0600 Subject: [PATCH] Provide Karma with updated dependencies on change --- tasks/test-all.js | 49 ++++++++++++++++++++++++++++++++++++-------- test/karma.config.js | 3 +++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/tasks/test-all.js b/tasks/test-all.js index 27afe7b81b..5a5ac8223e 100644 --- a/tasks/test-all.js +++ b/tasks/test-all.js @@ -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(); } diff --git a/test/karma.config.js b/test/karma.config.js index dbd1943ccc..d0ac0c557f 100644 --- a/test/karma.config.js +++ b/test/karma.config.js @@ -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')),