Angular + Testacular + Grunt and directive testing with templateUrl (external templates)

1,021 views
Skip to first unread message

Jaymes Bearden

unread,
Jan 17, 2013, 12:00:11 PM1/17/13
to ang...@googlegroups.com
Following Vojtajina's github example of Directive testing (https://github.com/vojtajina/ng-directive-testing/), I have finally been able to unit test my directives that have templateUrl set by preloading the external templates in to the $templateCache.

The one problem I now face is that I have to always have Grunt launched and running with (windows) grunt.cmd watch:templates so that my templates get compiled/updated and my tests can execute.

I noticed that Vojtajina had set the following in his Testacular config file:

preprocessors = {
  '**/*.html': 'html2js'
};

This doesn't appear to have any interaction with Grunt though as I set the same in my Testacular config and it doesn't execute the Grunt task. The documentation is pretty sparse on the preprocessor section of Testacular as well...

What am I doing wrong?

Peter Bacon Darwin

unread,
Jan 17, 2013, 3:23:24 PM1/17/13
to ang...@googlegroups.com
Testacular can run preprocessors before running the tests.  Have a look inside https://github.com/vojtajina/testacular/blob/master/lib/preprocessors/Html2js.js


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Jonathan Perl

unread,
Mar 8, 2013, 11:44:40 AM3/8/13
to ang...@googlegroups.com
How does this work exactly if I want to test a directive with a template.

I added this to my testacular.conf.js:

preprocessors = {
    'app/templates/**/*.html': 'html2js'
};

But whenever I try to compile a directive I get 

Error: Unexpected request: GET dTempl.html
No more request expected
   at Error (<anonymous>)
   at $httpBackend 

And if I try to include 'app/templates/**/*.html' in files I get Uncaught SyntaxError: Unexpected token <

Peter Bacon Darwin

unread,
Mar 8, 2013, 11:50:37 AM3/8/13
to ang...@googlegroups.com
Try including:

 'app/templates/**/*.html.js

Then add the modules as a dependency or just load them in your unit tests with:

module('app/templates/some/path/to/a/template/html');

mp


To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Perl

unread,
Mar 8, 2013, 12:13:04 PM3/8/13
to ang...@googlegroups.com
I added beforeEach(module('app/templates/dTemp.html.js')) but I get: Error: No module: app/templates/dTemp.html.js

Also when testacular first runs I get: Pattern "/Users/me/src/mySite/app/templates/**/*.html.js" does not match any file.

Jonathan Perl

unread,
Mar 8, 2013, 12:30:42 PM3/8/13
to ang...@googlegroups.com
Honestly, if there was just one example using the latest preprocessor I could figure it out. Do you know of any?

Peter Bacon Darwin

unread,
Mar 8, 2013, 5:31:24 PM3/8/13
to ang...@googlegroups.com
OK, so try

beforeEach(module('app/templates/dTemp.html'))

without the js


Jonathan Perl

unread,
Mar 8, 2013, 5:36:41 PM3/8/13
to ang...@googlegroups.com
Thanks for your help. I decided to just include a very similar preprocessing step in my grunt flow to ng-directive-testing and it works.

Peter Bacon Darwin

unread,
Mar 8, 2013, 5:59:43 PM3/8/13
to ang...@googlegroups.com
Actually, the reason I wasn't much help is that we do the pre-processing in grunt too!

Jonathan Perl

unread,
Mar 18, 2013, 5:53:05 PM3/18/13
to ang...@googlegroups.com
For future visitors here is an example of using the grunt preprocessing https://github.com/jperl/angular-jquery-mobile/blob/master/Gruntfile.js#L25
Reply all
Reply to author
Forward
0 new messages