Skip to content

Commit

Permalink
fix(systemjs-loader): systemjs config for karma test runner
Browse files Browse the repository at this point in the history
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
  • Loading branch information
simonfox committed Jun 15, 2017
1 parent cc65ec9 commit adac051
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/commands/new/unit-test-runners/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(project) {
ProjectItem.resource('setup.ext', 'test/setup.js', project.model.transpiler),
ProjectItem.resource('app.spec.ext', 'test/app.spec.js', project.model.transpiler)
),
ProjectItem.resource('aurelia-karma.js', 'test/aurelia-karma.js')
ProjectItem.resource('aurelia-karma.js', `test/${project.loader}.aurelia-karma.js`)
)
).addToDevDependencies(
'jasmine-core',
Expand Down
2 changes: 2 additions & 0 deletions lib/dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"karma-typescript-preprocessor": "^0.2.1",
"minimatch": "^3.0.2",
"requirejs": "^2.3.2",
"systemjs":"0.20.13",
"systemjs-plugin-text":"0.0.9",
"text": "github:requirejs/text#latest",
"through2": "^2.0.1",
"tslint": "^3.11.0",
Expand Down
File renamed without changes.
49 changes: 49 additions & 0 deletions lib/resources/test/system.aurelia-karma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(function(global) {
var karma = global.__karma__;
var root = 'src';
karma.config.args.forEach(function(value, index) {
if (value === 'aurelia-root') {
root = karma.config.args[index + 1];
}
});

if (!karma) {
return;
}

function patchSystemJS() {
SystemJS.config({
"packages": {
"base/test": {
"defaultExtension": "js"
}
}
});

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
patchSystemJS();
requireTests();
})(window);
6 changes: 3 additions & 3 deletions lib/workflow/activities/project-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ module.exports = class {
let configurePlatform = require(`../../commands/new/platforms/${model.platform.id}`);
configurePlatform(project, this.options);

let configureLoader = require(`../../commands/new/loaders/${model.loader.id}`);
configureLoader(project, this.options);

let configureTranspiler = require(`../../commands/new/transpilers/${model.transpiler.id}`);
configureTranspiler(project, this.options);

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

let configureLoader = require(`../../commands/new/loaders/${model.loader.id}`);
configureLoader(project, this.options);

return project.create(this.ui, this.options.hasFlag('here') ? undefined : process.cwd())
.then(() => this.ui.clearScreen())
.then(() => this.ui.log('Project structure created and configured.'))
Expand Down

0 comments on commit adac051

Please sign in to comment.