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