https://github.com/boneskull/karma-ng-server-side-template2js-preprocessor
Scans HTML-based server side template files for AngularJS templates and converts them to JS for use in unit tests
https://github.com/boneskull/karma-ng-server-side-template2js-preprocessor
Last synced: 11 months ago
JSON representation
Scans HTML-based server side template files for AngularJS templates and converts them to JS for use in unit tests
- Host: GitHub
- URL: https://github.com/boneskull/karma-ng-server-side-template2js-preprocessor
- Owner: boneskull
- License: mit
- Archived: true
- Created: 2014-02-23T23:32:51.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2015-09-02T20:07:46.000Z (over 10 years ago)
- Last Synced: 2025-03-20T13:04:11.692Z (11 months ago)
- Language: JavaScript
- Size: 195 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# karma-ng-server-side-template2js-preprocessor
Karma preprocessor to scan HTML-based server side templates for embedded AngularJS templates, and convert the AngularJS templates to JS for use in unit tests.
This is useful for *[testing directives](https://github.com/vojtajina/ng-directive-testing).*
Based on [karma-ng-html2js-preprocessor](https://github.com/karma-runner/karma-ng-html2js-preprocessor).
Module name left long intentionally.
## Installation
`npm install karma-ng-server-side-template2js-preprocessor`
## What This Module Does Not Do
* **It does not compile your server-side templates.** Thus, if you have template code within any of your AngularJS
templates, it will be rendered raw. Try [Protractor](https://github.com/angular/protractor)?
* It does not understand anything that doesn't at least vaguely look like HTML. Karma runs in browsers and browsers need HTML. Templating languages like Jade do not run in browsers unless compiled. If you want to use a Jade template, compile it, and hand the resulting HTML to Karma.
## What This Module Actually Does
* It takes some sort of HTML-like server-side template and extracts the [AngularJS templates](http://docs.angularjs.org/guide/templates) from it. Given an EJS template (for example), `foo.ejs`:
```html
<div ng-repeat="herp in derp">
<!-- ... -->
</div>
<ul>
<li ng-repeat="apples in oranges">
<!-- ... -->
</li>
</ul>
<%- // EJS garbage in here, but we ignore it %>
```
and a Karma configuration containing:
```js
preprocessors: {
'**/*.ejs': ['ng-sst2js']
},
ngSst2JsPreprocessor: {
moduleName: 'myTemplates'
}
```
It will produce a module named `myTemplates` which can then be loaded by your unit tests. Templates `bar` and `baz` will live in your [`$templateCache`](http://docs.angularjs.org/api/ng/service/$templateCache) service.
They can then be used like so:
```js
'use strict';
describe('myDirective', function () {
beforeEach(module(
'myTemplates',
'myModuleContainingDirective'));
it('has templates', inject(function ($compile, $rootScope) {
// myDirective leverages the "bar" template
var markup = '',
compiled,
scope = $rootScope.$new();
compiled = $compile(markup)(scope);
scope.$apply();
expect(compiled.find('div').attr('ng-repeat')).to.equal('herp in derp');
}));
});
```
*(Note: example has not been tested; something similar has, so something similar should work, at least.)*
## TODO
See [issues](https://github.com/boneksull/karma-ng-server-side-template2js-preprocessor).
## Author
[Christopher Hiller](https://boneskull.com)