|
| 1 | +(function(global) { |
| 2 | + var karma = global.__karma__; |
| 3 | + var requirejs = global.requirejs |
| 4 | + var locationPathname = global.location.pathname; |
| 5 | + var root = 'src'; |
| 6 | + karma.config.args.forEach(function(value, index) { |
| 7 | + if (value === 'aurelia-root') { |
| 8 | + root = karma.config.args[index + 1]; |
| 9 | + } |
| 10 | + }); |
| 11 | + |
| 12 | + if (!karma || !requirejs) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + function normalizePath(path) { |
| 17 | + var normalized = [] |
| 18 | + var parts = path |
| 19 | + .split('?')[0] // cut off GET params, used by noext requirejs plugin |
| 20 | + .split('/') |
| 21 | + |
| 22 | + for (var i = 0; i < parts.length; i++) { |
| 23 | + if (parts[i] === '.') { |
| 24 | + continue |
| 25 | + } |
| 26 | + |
| 27 | + if (parts[i] === '..' && normalized.length && normalized[normalized.length - 1] !== '..') { |
| 28 | + normalized.pop() |
| 29 | + continue |
| 30 | + } |
| 31 | + |
| 32 | + normalized.push(parts[i]) |
| 33 | + } |
| 34 | + |
| 35 | + // Use case of testing source code. RequireJS doesn't add .js extension to files asked via sibling selector |
| 36 | + // If normalized path doesn't include some type of extension, add the .js to it |
| 37 | + if (normalized.length > 0 && normalized[normalized.length - 1].indexOf('.') < 0) { |
| 38 | + normalized[normalized.length - 1] = normalized[normalized.length - 1] + '.js' |
| 39 | + } |
| 40 | + |
| 41 | + return normalized.join('/') |
| 42 | + } |
| 43 | + |
| 44 | + function patchRequireJS(files, originalLoadFn, locationPathname) { |
| 45 | + var IS_DEBUG = /debug\.html$/.test(locationPathname) |
| 46 | + |
| 47 | + requirejs.load = function (context, moduleName, url) { |
| 48 | + url = normalizePath(url) |
| 49 | + |
| 50 | + if (files.hasOwnProperty(url) && !IS_DEBUG) { |
| 51 | + url = url + '?' + files[url] |
| 52 | + } |
| 53 | + |
| 54 | + if (url.indexOf('/base') !== 0) { |
| 55 | + url = '/base/' + url; |
| 56 | + } |
| 57 | + |
| 58 | + return originalLoadFn.call(this, context, moduleName, url) |
| 59 | + } |
| 60 | + |
| 61 | + var originalDefine = global.define; |
| 62 | + global.define = function(name, deps, m) { |
| 63 | + if (typeof name === 'string') { |
| 64 | + originalDefine('/base/' + root + '/' + name, [name], function (result) { return result; }); |
| 65 | + } |
| 66 | + |
| 67 | + return originalDefine(name, deps, m); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + function requireTests() { |
| 72 | + var TEST_REGEXP = /(spec)\.js$/i; |
| 73 | + var allTestFiles = ['/base/test/unit/setup.js']; |
| 74 | + |
| 75 | + Object.keys(window.__karma__.files).forEach(function(file) { |
| 76 | + if (TEST_REGEXP.test(file)) { |
| 77 | + allTestFiles.push(file); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + require(allTestFiles, window.__karma__.start); |
| 82 | + } |
| 83 | + |
| 84 | + karma.loaded = function() {}; // make it async |
| 85 | + patchRequireJS(karma.files, requirejs.load, locationPathname); |
| 86 | + requireTests(); |
| 87 | +})(window); |
0 commit comments