The Blog Ben Writes

about code

About Using Jasmine

I have been working on a Backbone.js project for a few days now and while it has been great I came accross one very annoying issue. After getting my environment set up, a quick hello world out of the way, and some Jasmine tests written I opened up my terminal and stared blankly for a few minutes before it hit methat I didn’t know how to run them Jasmine tests. I had the proper source files and dependencies but just didn’t know how to actually run my tests.

After looking on google for a while I decided to refactor my test suite use the Jasmine Standalone release which I would have done originally had I not expected the the testing suite to just work, like it does in rails.

I download the standalone bundle and was able to pull the necessary source files into my project’s directory, set up an index.html in my spec directory, and began to require all the relevant scripts. At that point I decided I wanted a better way. I talked to some friends and was told that I should just use the Jasmine Gem. Since I had only used this gem with with Rails and gems-in-general with Ruby I didn’t think to use it for a strictly JavaScript project.

Using the Jasmine gem is extremely simple if you have a Ruby environment set up on your computer. Once you install the gem just navigate to the root of your project and type these two commands:

1
2
jasmine init
jasmine examples

If this is done properly you will get a notice saying “Jasmine has been installed” and instructions to run the server with the command rake jasmine and that you can run the automated CI tasks with PhantomJS by calling rake jasmine::ci.

More specifically jasmine init will add the following files and directories to your project.

  • spec will contain all of your spec files and spec specific requirements
  • spec/javascripts which holds
    • helpers an empty directory you will later use for helper scripts (jasmine examples will add one)
    • support holding jasmine.yml which will handle your requirements and jasmine.helper.rb where you can override and default configuration options like the port numbers
  • rakefile which by default will require the jasmine rake tasks for starting the testing server and more but can be used to write more tasks

jasmine examples will add more directories and files to your project that you can use as a boilerplate for building your own tests. It will install:

  • spec/helpers/jasmine_examples/spec_helper.js a file you can use for setting up callbacks for your tests
  • spec/jasmine_examples/PlayerSpec.js an example spec file for Player
  • publica directory contianing two JavaScript objects that are tested by PlayerSpec.js

Now, if you type rake jasmine and visit http://localhost:8888/ (the default port for the python server you launch with rake jasmine) you will see the passing tests for PlayerSpec.js.

Now that we have the dummy tests running we can set up the requirements for dependencies in jasmine.yml. This file contains comments telling you where to require what and it already has everything you need to run the tests generated by jasmine examples and any tests that you write in the spec directory the gem builds however, if if you are using anything other than vanilla JavaScript like I was, you will need to manually require some files. You can use wildecards and regex to require specific groups of files in directories (e.g. js/**/*.js will require in all javascript files in all child directories directly nested under the js directory) however this may not load scripts in the order that you need so it will still be necessary to require certain files manually. For example, if you are using backbone lib/underscore.js will need to be required before lib/backbone.js manually even if you have lib/*.js the src_files section of jasmine.yml.

If some of your tests aren’t working and you think that some files aren’t being required properly you can manually check the page by running the server (rake jasmine) and opening the console to see what isn’t being loaded properly. If there are errors in your files they will show up in the console. Since it is JavaScript it runs in the browser and all of the files you need for testing should be there.

This is not the most difficult thing in the world, and frankly, if you are using anything like yeoman or are adept at bower you will probably not need this, but if you want to bring some jasmine tests into a small application without much overhead I found this gem to be a great solution.