Relative URL issue when using html2js

298 views
Skip to first unread message

ka...@bocoup.com

unread,
Apr 30, 2013, 2:10:01 PM4/30/13
to karma...@googlegroups.com
Apologies if this has been posted before, I couldn't find anything related while searching.

I've got a couple directives that use templateUrl to load their templates from a partial. In order for these directives to work, I've just changed the templateUrl to be a root-relative path:

templateUrl: '/static/app/partials/grid-directive.html'

Previously, I'd been pre-processing these partials within my Karma config so that they'd be available to test, a la https://github.com/vojtajina/ng-directive-testing:

basePath = '../src/';
files = [
    // ...
    'static/app/**/*.html'
];
preprocessors = {
  '**/*.html': 'html2js'
};

Unfortunately, now that I've made the paths root-relative, it seems that Karma no longer understands to match the requested templateUrl against the pre-processed module, which is loaded with the non-root-relative string:

beforeEach( module('static/app/partials/grid-directive.html') );

Adding the / at the start of the module path causes an error because the module cannot be found, and removing it causes the test to error out with Error: Unexpected request: GET /static/app/partials/grid-directive.html

How can I allow Karma to be aware of how to match these requests against the loaded, pre-processed partial's module?

Vojta Jína

unread,
May 4, 2013, 12:02:28 AM5/4/13
to karma...@googlegroups.com
Hi, this is a missing feature in html2js preprocessor. We need to make this configurable:

V.


--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at http://groups.google.com/group/karma-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

ka...@bocoup.com

unread,
May 6, 2013, 4:01:42 PM5/6/13
to karma...@googlegroups.com
Thanks Vojta for coding a fix for custom $templateCache values.

If anybody's stuck on earlier versions that won't include the fix in https://github.com/karma-runner/karma-ng-html2js-preprocessor/commit/5b59ac681014340578049f7087eafa0efb124ec4 , here's how we worked around this issue last week: Before each test, wrap the $templateCache.get function in another function that will transform the template keys. In our case, templates were being requested with a slash appended to the existing $templateCache key, so we just added this beforeEach statement:

    beforeEach(inject(function($templateCache) {
        var getTemplate = $templateCache.get;

        // Wrap $templateCache.get to strip leading slashes off templateUrls
        // (Karma's html2js pre-processor registers templates without the leading /)
        $templateCache.get = function(key) {
            if( key.substr(0, 1) === '/' ) {
                key = key.substr(1);
            }
            return getTemplate(key);
        };
    }));

Not recommended if you can just use a more recent version of Karma, but this approach can serve the same function in the short-term.

Vojta Jína

unread,
May 11, 2013, 2:09:41 AM5/11/13
to karma...@googlegroups.com
Yep, that's a clever hack ;-)
Reply all
Reply to author
Forward
0 new messages