问题
Attempting to use the karma-ng-html2js-preprocessor. Karma has been finding all of my other stuff (javascript) fine, but when it comes to this html preprocessor it can't seem to find and generate a module from it.
Check out my options object below. As you can see, I've added a pattern to the files attribute and I'm using the generic name 'foo' for the module. In the test, karma throws an error whenever I try to call module('foo');
I really want to be able to use this so I don't have to hardcode templates into my unit tests or some other wacky solution.
var options = {
files: [].concat(
bowerFiles,
config.specHelpers,
clientApp + '**/*.module.js',
clientApp + '**/*.js',
clientApp + '**/*.html',
'*.html',
temp + config.templateCache.file,
config.serverIntegrationSpecs
),
exclude: [],
coverage: {
dir: report + 'coverage',
reporters: [
// reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' },
{ type: 'text-summary' } //, subdir: '.', file: 'text-summary.txt'}
]
},
ngHtml2JsPreprocessor: {
// strip this from the file path
// stripPrefix: clientApp,
moduleName: 'foo'
},
preprocessors: {}
};
options.preprocessors[clientApp + '**/*.html'] = ['ng-html2js'];
options.preprocessors[clientApp + '**/!(*.spec)+(.js)'] = ['coverage'];
return options;
}
回答1:
I know this doesn't necessarily answer your question and if more information comes to light, I'll add to this answer but I've found this strategy works well...
I find that the HTML template of a directive, route or state (if using ui-router) shouldn't come into play when unit testing. Unit testing is about testing the interface to your component; its publicly available methods and properties. Testing the interactions with the HTML document is really in the realm of end-to-end testing with something like Protractor.
With that in mind, here's how I prevent Karma from attempting to load template files during testing.
beforeEach(function() {
module('module.name.of.the.testable.component');
inject(function($templateCache) {
$templateCache.put('path/to/template/as/it/appears/in/your/component.html',
'<div></div>');
});
});
来源:https://stackoverflow.com/questions/34301801/karma-ng-html2js-preprocessor-not-creating-modules