Skip to content

Instantly share code, notes, and snippets.

@alessbell
Last active August 29, 2015 14:10
Show Gist options
  • Save alessbell/cdba93dc50b847ccb008 to your computer and use it in GitHub Desktop.
Save alessbell/cdba93dc50b847ccb008 to your computer and use it in GitHub Desktop.
The Things I Learned Writing Jasmine Tests

Lessons From Writing Jasmine Tests

##Introduction

First, if it’s a Django/Flask project, use jasmine-py! It will (mostly) make your life easier. Run pip install jasmine and initialize a project using jasmine-install: a dedicated spec directory will be created for you, and a YAML configuration file will be created. This is an important difference between jasmine-py and non-py-flavouroured-jasmine: outside of django/flask projects, Jasmine runs the specs from a SpecRunner.html file, where you're expected to include links to your specs in the <head> tags. In jasmine-py, jasmine.yml handles your source files and specs.

This is important to note because, if you choose to use Jamsine extensions like jasmine-jquery which allows simpler testing of jQuery elements, be sure to include it in jasmine.yml.

Problems

We ran into a few issues working with Jasmine, specifically jasmine-py, we think. First, we wanted to use fixtures to simplify the process of testing DOM elements, but we were never able to figure out what the specific issue was. Every time we tried to use fixtures in our tests, the specs couldn't read or recognize the fixture files. We attempted to accomplish this using jasmine-fixture, but the documentation isn't the best. It's not a bulletproof arrproach, but, as a general, engagement on GitHhub is a pretty good indication of how uesful a piece of open-source code is. With only 189 stars, it's not much of a surprise that the documentation left us wanting.

After a bit of searching, it seemed as if the problem was the location of the fixtures: apparently fixtures are supposed to be situated relative to the SpecRunner.html file (which of course does not exist based on our jasmine-py configuration), but after several attempts, moving the fixtures folder accomplished precisely nothing. We chalked it up to poor documentation and moved on without the use of fixtures.

We also ran into another issue with jasmine-jquery: we were similarly having trouble getting it to function after including it in our javascripts folder and including it as a src_file in our jasmine.yml file. After reading through the complete documentation, we came upon the cross domain policy problems under Chrome section. 'Aha!' we thought, 'the problem must be that newer versions of Chrome don't allow file:// URIs to read other file:// URIs.' But, alack, how wrong we were. Jasmine-jquery remains yet another unsolved mystery; we never did determine what the problem was, but we successfully moved on without it.

More Broadly Speaking

In terms of what kind of stuff to test, we started with the methods we could test without interacting with DOM elements first, just testing relatively simple logic. Because Jasmine is DOM-agnostic, you can write scripts that access jQuery elements in Jasmine and create DOM elements before each spec runs using the beforeEach() and afterEach() functions. We would recommend this course of action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment