Overcoming Karma server case sensitivity
This post explains how to setup Karma + RequireJs. However it doesn't mention one important limitation of Karma. And that is case-sensitivity. Basically if your module name doesn't match actual folder/filename Karma will return 404 and test will fail. In order to overcome this issue I hooked up into require.load function and normalized module paths there to make sure they match those stored on the disk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
//This file is used to load ES6 unit-tests transpiled into AMD modules. | |
var __karma__; | |
var tests = []; | |
var files = {}; | |
for (var file in __karma__.files) { | |
if (__karma__.files.hasOwnProperty(file)) { | |
if (/spec\.js$/.test(file)) { | |
tests.push(file); | |
} | |
//For case-insensitive search | |
var key = file.toLowerCase(); | |
files[key] = file; | |
} | |
} | |
//Delay loading of Karma | |
__karma__.loaded = function () { }; | |
requirejs.config({ | |
// Karma serves files from '/base' | |
baseUrl: '.', | |
paths: {}, | |
shim: {}, | |
// ask Require.js to load these files (all our tests) | |
deps: tests, | |
// start test run, once Require.js is done | |
callback: function () { | |
__karma__.start(); | |
} | |
}); | |
//Normalize urls because Karma server is case-sensitive | |
var _load = require.load; | |
require.load = function (context, moduleName, url) { | |
var normalizedUrl = files[url.toLowerCase()]; | |
if (!normalizedUrl) { | |
console.error("File " + url + " is not served by Karma. Add missing files to Karma configuration in config.json. Consider mocking external module dependencies in main.ts."); | |
} | |
_load(context, moduleName, normalizedUrl); | |
}; |
Comments
Post a Comment