Skip to content

Commit adac051

Browse files
committed
fix(systemjs-loader): systemjs config for karma test runner
An import of a unit test _helper_ module from within a spec was failing (e.g. a module that defines a mock). This was due to the import statement normalization result not having a .js extension. This change splits aurelia-karma into two loader specific implementations, with the SystemJS version adding a package configuration for the base/test path that sets `defaultExtension` to js. `au new` adds an implementation based on the selected loader. closes #648
1 parent cc65ec9 commit adac051

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

lib/commands/new/unit-test-runners/karma.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function(project) {
1717
ProjectItem.resource('setup.ext', 'test/setup.js', project.model.transpiler),
1818
ProjectItem.resource('app.spec.ext', 'test/app.spec.js', project.model.transpiler)
1919
),
20-
ProjectItem.resource('aurelia-karma.js', 'test/aurelia-karma.js')
20+
ProjectItem.resource('aurelia-karma.js', `test/${project.loader}.aurelia-karma.js`)
2121
)
2222
).addToDevDependencies(
2323
'jasmine-core',

lib/dependencies.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
"karma-typescript-preprocessor": "^0.2.1",
4646
"minimatch": "^3.0.2",
4747
"requirejs": "^2.3.2",
48+
"systemjs":"0.20.13",
49+
"systemjs-plugin-text":"0.0.9",
4850
"text": "github:requirejs/text#latest",
4951
"through2": "^2.0.1",
5052
"tslint": "^3.11.0",
File renamed without changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(function(global) {
2+
var karma = global.__karma__;
3+
var root = 'src';
4+
karma.config.args.forEach(function(value, index) {
5+
if (value === 'aurelia-root') {
6+
root = karma.config.args[index + 1];
7+
}
8+
});
9+
10+
if (!karma) {
11+
return;
12+
}
13+
14+
function patchSystemJS() {
15+
SystemJS.config({
16+
"packages": {
17+
"base/test": {
18+
"defaultExtension": "js"
19+
}
20+
}
21+
});
22+
23+
var originalDefine = global.define;
24+
global.define = function(name, deps, m) {
25+
if (typeof name === 'string') {
26+
originalDefine('/base/' + root + '/' + name, [name], function (result) { return result; });
27+
}
28+
29+
return originalDefine(name, deps, m);
30+
}
31+
}
32+
33+
function requireTests() {
34+
var TEST_REGEXP = /(spec)\.js$/i;
35+
var allTestFiles = ['/base/test/unit/setup.js'];
36+
37+
Object.keys(window.__karma__.files).forEach(function(file) {
38+
if (TEST_REGEXP.test(file)) {
39+
allTestFiles.push(file);
40+
}
41+
});
42+
43+
require(allTestFiles, window.__karma__.start);
44+
}
45+
46+
karma.loaded = function() {}; // make it async
47+
patchSystemJS();
48+
requireTests();
49+
})(window);

lib/workflow/activities/project-create.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ module.exports = class {
4040
let configurePlatform = require(`../../commands/new/platforms/${model.platform.id}`);
4141
configurePlatform(project, this.options);
4242

43+
let configureLoader = require(`../../commands/new/loaders/${model.loader.id}`);
44+
configureLoader(project, this.options);
45+
4346
let configureTranspiler = require(`../../commands/new/transpilers/${model.transpiler.id}`);
4447
configureTranspiler(project, this.options);
4548

@@ -55,9 +58,6 @@ module.exports = class {
5558
let configureEditor = require(`../../commands/new/editors/${model.editor.id}`);
5659
configureEditor(project, this.options);
5760

58-
let configureLoader = require(`../../commands/new/loaders/${model.loader.id}`);
59-
configureLoader(project, this.options);
60-
6161
return project.create(this.ui, this.options.hasFlag('here') ? undefined : process.cwd())
6262
.then(() => this.ui.clearScreen())
6363
.then(() => this.ui.log('Project structure created and configured.'))

0 commit comments

Comments
 (0)