Adding relative template path for angular unit testing with ng-html2js-preprocessor

660 Views Asked by At

I've a angular directive with a template url. I'm trying to test the directive with karma. So get the html of the directive with ng-html2js-preprocessor .

My project folder structure is

var
   www
      myproject
           assets
               templetes
                    directive.html
               js
                    directive.js
           karma
               karma.config.js
               directive.test.js

Here myproject is the project folder for me. So var/www can be different in some other environment.

In the directive.js I've:

  restrict: 'E',
  scope: true,
  replace: false,
  templateUrl: '/templates/directive.html',

Now in the karma.config.js file I've written

 preprocessors: {
  '../assets/templates/**/*.html': ['ng-html2js']
 },
 files: [
 ..., (Some library files like angular, jquery etc...)
 '../assets/js/directive.js',
  './*.test.js',
  '../assets/templates/**/*.html'
 ]

After running the config file in terminal in the log I saw:

DEBUG [preprocessor.html2js]: Processing "/var/www/myproject/assets/templates/directive.html".

DEBUG [watcher]: Resolved files:
/var/www/myproject/assets/templates/directive.html.js

In the test file I've written

beforeEach(module('/var/www/myproject/assets/templates/directive.html'));

beforeEach(inject(function($rootScope, $compile, defaultJSON, $templateCache) {
var template = $templateCache.get('/var/www/myproject/assets/templates/directive.html');
$templateCache.put('/templates/directive.html', template);
 }));

And this code is working properly, but my problem is the path /var/www is not same in every environment may be some other user is keeping the project in some other path then it will not work. So I need to add some relative paths. Can anybody tell me how I can add relative path here?

I tried with

ngHtml2JsPreprocessor: {
  stripPrefix: '*/myproject/assets'
},

in karma.config.js file and in the test file:

 beforeEach(module('/templates/directive.html'));
 var template = $templateCache.get('/templates/directive.html');

But it didn't worked, showing error failed to instantiate module '/templates/directive.html'

I also tried with

ngHtml2JsPreprocessor: {
  moduleName: "my.templates"
},

in karma.config.js file and in the test file:

 beforeEach(module('my.templates'));
 var template = $templateCache.get('/templates/directive.html');

But it also failed. Please help me how to fix this issue and add relative urls.

0

There are 0 best solutions below