Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

Test-App
-src
--bower
--lib
--js
--modules
---testDir
----test.js
----test-directive.html
----test
-----test.spec.js
-test
--config
---karma.conf.js
--e2e

Karma Config

'use strict'; module.exports = function(config){     config.set({     basePath: '../../',     frameworks: ['jasmine'],     files: [         // Angular         'src/bower/angular/angular.js',         // Mocks         'src/bower/angular-mocks/angular-mocks.js',         // Libraries         'src/lib/**/*.js',         // App         'src/js/*.js',         'src/modules/*/*.js',         // Tests         'src/modules/**/test/*spec.js',         // Templates         'src/modules/**/*.html'     ],     autoWatch: false,     singleRun: true,     reporters: ['progress'],     browsers: ['PhantomJS'],      preprocessors: {         'src/modules/**/*.html': 'ng-html2js'     },     ngHtml2JsPreprocessor: {         moduleName: 'dir-templates'     },     plugins: [         'karma-jasmine',         'karma-ng-html2js-preprocessor',         'karma-phantomjs-launcher',         'karma-chrome-launcher',         'karma-junit-reporter'     ]     }); }; 

test.js

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

test-direct.html

<span>Hello World</span> 

test.spec.js

'use strict'; describe('test module', function() {     beforeEach(module('modules.test'));     /* -- DIRECTIVES------------------ */     describe('directives', function() {         var $compile, $scope, elm;         beforeEach(module('dir-templates');         beforeEach(inject(function($compile, $rootScope) {             $scope = $rootScope.$new();             elm = angular.element('<test-directive></test-directive>');             $compile(elm)($scope);             $scope.$digest();         }));         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 beforeEach(module('dir-templates')), 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.

like image 464
dmaloney.calu Avatar asked Apr 04 '14 17:04

dmaloney.calu


People also ask

How to run angular tests in karma?

An environment to run angular tests is being created using all the imports at the beginning of the file. TestBed is a powerful unit testing tool provided by angular, and it is initialized in this file. Finally, karma loads all the test files of the application matching their names against a regular expression.

How to run a test in angular-CLI?

To run the test you only need to run the command “ng test”. This command is going to execute the tests, open the browser, show a console and a browser report and, not less important, leave the execution of the test in watch mode. Let’s take a look at the karma configuration file created by angular-cli.

What is the default browser for Karma testing?

By default it is chrome but you can install and use other browser launchers. The angular-cli configuration of karma uses the file “test.ts” as the entry point of the tests for the application. Let’s take a look to that file; We have a lot of things going on here.

What is the use of karma in testing?

karma. Karma is a task runner for our tests. It uses a configuration file in order to set the startup file, the reporters, the testing framework, the browser among other things. The rest of the dependencies are mainly reporters for our tests, tools to use karma and jasmine and browser launchers.


1 Answers

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' 
like image 172
dmaloney.calu Avatar answered Oct 05 '22 22:10

dmaloney.calu