Quick Hack To Work Around Jasmine-jQuery Fixture Limitations

I use Jasmine-jQuery to help out my Jasmine tests. It’s a great little plugin and generally makes my life easier. I ran into an issue today, though, and rather than trying to fix the real problem (which I think is going to be difficult), I created a quick workround in my own code.

The Problem: setFixtures Wipes Out Previous Fixtures

That pretty much sums it up… when you call `setFixtures(“….”)` to add some HTML fixture to your app, this call will wipe out all of the previous fixtures that you had set up. This means you can’t have call calls to setFixtures, and means you have to set up all of your fixtures at the same time in the same place.

This doesn’t work well with the way I wrote my tests. I use helper methods to set up various bits of context, including fixtures that I need. So, I need a way to add more fixtures over time.

The Hack: `addFixture` And `clearMyFixtures` Methods

Here’s the gist of my hack to work around it. Add this code to a .js file in `spec/javascript/helpers/`

function addFixture(html){
  var fixtures = jasmine.getFixtures();  
  if (!fixtures.fixtureList){
    fixtures.fixtureList = [];

function clearMyFixtures(){
  delete jasmine.getFixtures().fixtureList;

Then you can call `addFixture` in your `beforeEach` method. Be sure to call `clearMyFixtures` in an `afterEach` block as well.

describe("something", function(){
    addFixture("<div id='something'></div>");


  it("does that thing", function{
    // ... your test that uses a fixture, here

The `beforeEach` method keeps a running tally of all the fixtures that you want to add, in a simple array. Every time you call this method it appends the new fixture to the existing list, joins them all together and calls the real setFixture method. This works around the limitation of how setFixtures will wipe out your existing fixtures.

In order to prevent every fixture from appending over and over and over forever, though, I needed a method to clean it all up. The `clearMyFixtures` method does exactly this. It deletes the array of fixtures that I’m storing so the next time you call `addFixture`, it starts over with an empty list.

No Really, It’s Just A Hack

I don’t plan on using this forever. At some point I want to see if I can help out the Jasmine-jQuery plugin and fix the underlying problem (as I see it, anways). For now, this hack will do.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Jasmine, Javascript. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Here’s a much less “hacky” alternative, complete with tests: https://github.com/searls/jasmine-fixture