dmaloney.calu dmaloney.calu - 4 days ago 4x
AngularJS Question

Karma 'Unexpected Request' when testing angular directive, even with ng-html2js

After spending the last day trying to make this work, I've found that I've circled back around to the same error I was having at the beginning:

Error: Unexpected request: GET test-directive.html

I'm using Karma and Jasmine to test directives in Angular. I've looked through similar questions on StackOverflow, but find that everything that has been tried in the other examples is to no avail.

Code structure
















Karma Config

'use strict';
module.exports = function(config){
basePath: '../../',
frameworks: ['jasmine'],
files: [
// Angular
// Mocks
// Libraries
// App
// Tests
// Templates
autoWatch: false,
singleRun: true,
reporters: ['progress'],
browsers: ['PhantomJS'],

preprocessors: {
'src/modules/**/*.html': 'ng-html2js'
ngHtml2JsPreprocessor: {
moduleName: 'dir-templates'
plugins: [


'use strict';
angular.module('modules.test', []).
directive('testDirective', [function() {
return {
restrict: 'E',
templateUrl: 'test-directive.html',
link: function($scope, $elem, $attrs) {
$scope.someFn = function() {


<span>Hello World</span>


'use strict';
describe('test module', function() {
/* -- DIRECTIVES------------------ */
describe('directives', function() {
var $compile, $scope, elm;
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope.$new();
elm = angular.element('<test-directive></test-directive>');
it('should have one span tag', function(){
//Jasmine test here to check for one span tag.

Have shortened a couple of files to stick to just where the issue is. In calling
, it should be loading all of the matched .html files into the $templateCache and preventing the GET request that is throwing the error.

Any help would be appreciated as it's really been driving me nuts. Please comment if you have any additional questions.


So, a painstaking headache for what seems to be a two line fix. After opening Karma in Chrome (instead of PhantomJS) and looking at the source files, I noticed that when ng-html2js attaches the directive to the $templateCache it uses the entire path, not the one provided in the directive definition.

In short, 'src/modules/test/test-directive.html.js' !== 'test-directive.html.js'.

To achieve this, modify the karma.conf.js file ngHtml2JsProcessor to read like:

ngHtml2JsPreprocessor: {
    stripPrefix: 'src/',
    moduleName: 'dir-templates'

And the directive declaration's templateUrl to look like:

templateUrl: 'modules/test/test-directive.html'