0
votes

I am trying my hands on the karma preprocessor to get the template loaded in the unit tests.

My angularjs unit tests for directive is throwing 'Failed to instantiate module error' while running the tests. I am using the karma html2js preprocessor in karma.conf.js for getting the template in the directive unit test.

karma.conf.js

config.set({
        preprocessors: {
            'Components/navBar/Templates/navBar.htm': ['ng-html2js']
        },

    files: [
    'Scripts/angular-mocks.js',
    ...
    ...
    'Client/Components/navBar/Templates/navBar.htm'
    ],
    ngHtml2JsPreprocessor: {
        moduleName: 'templates'
    }

navBar_testcase.js

describe("navbartesting", 
function () {
    var template;

beforeEach(module('templates'));

beforeEach(function () {
    module("navBar");
    inject(function ($rootScope, $compile, $httpBackend, $injector, $localStorage, $state, $templateCache, userSession, appConfig) {

        http = $httpBackend;
        $httpBackend = $injector.get('$httpBackend');
        ...
        ....
    });
});
});

While running the above unit test the error 'Failed to instantiate module 'templates'' is thrown. What might be the problem in this? Are we having any physical path to be defined to have the 'templates' module in the karma config file?

1

1 Answers

0
votes

With this config, is possible to make a template. The downside is that it will create only the view.tpl.html and not the test.tpl.html

 config.set({
        autoWatch: true,
        basePath: '../',
        browsers: ['Chrome'],
        frameworks: ['jasmine', 'requirejs'],

        reporters: ['progress'],

        plugins: [
            'karma-chrome-launcher',
            'karma-phantomjs-launcher',
            'karma-jasmine',
            'karma-requirejs',
            'karma-ng-html2js-preprocessor-requirejs'
        ],

        files: [
            // App-specific Code
            {pattern: 'src/**/*.js', included: false},

            // 3rd party code
            {pattern: 'lib/**/*.js', included: false},

            //test files
            {pattern: 'test/unit/**/*.js', included: false},

            'test/test-main.js',
            '**/test.tpl.html',
            '**/view.tpl.html'
        ],
        ngHtml2JsPreprocessor: {
            enableRequireJs: true,
            moduleName: 'templates',
            cacheIdFromPath: function (filepath) {
                console.log('path=' + filepath.replace(/^.*[\\\/]/, ''));
                return filepath.replace(/^.*[\\\/]/, '');
            },
        },

        exclude: [
            'src/main.js'
        ],
        preprocessors: {
            '**/*.html': ['ng-html2js']
    }