Jasmine with Custom Templates

For one of the sites I work on, SiteCatalyst (Adobe Analytics) is used to gather metrics. This library needs to be included inside the <body>. This was problematic when I wanted to use grunt-contrib-jasmine to run my Jasmine unit tests headlessly with PhantomJS & Grunt (aka from the command line) since by default it generates a spec runner with all referenced JS in the <head>.

That’s when I read about using Jasmine Templates. This seemed like the solution but the documentation and getting to a working solution wasn’t super straight forward.

I wanted to be able to pass in some variables to reference a minified version of the Adobe Analytics JavaScript (AppMeasurement.min-x.x.x.js) and a non-minified version (AppMeasurement-x.x.x.js) to run unit tests before and after the minification. So for the jasmine task in my Gruntfile.js I had to specify a template module (JavaScript) to execute and then the module would specify the .tmpl (HTML Template).

The trickiest part in all of this was remembering to put ./tests/ instead of tests/ as the path to load the template module so it loaded from the current working directory.

Here is what my code ended up looking like:

Grunt Configuration

Template

Runner Template

One Reply to “Jasmine with Custom Templates”

  1. First of all, thanks for this article. This was exactly what I was looking for.
    However, I am unable to understand how you are able to get references to ‘css’, ‘scripts’ in the Runner template. While trying your solution, I am even not able to get references to specs either.

    Can you please help on this?
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.