I’ve demonstrated how to run Jasmine using JavaScript in the browser, but you don’t have to do it that way. Jasmine works in a variety of other environments and with other tools.
CoffeeScript is a language that compiles to JavaScript, and it’s beautiful. It makes coding in JavaScript much easier, and it also makes your Jasmine code look better. Using CoffeeScript with Jasmine is a fairly straightforward process, and your specs will look pretty.
If you don’t already use CoffeeScript, I’d strongly recommend it. It’s easy to learn if you know JavaScript, and it reduces a lot of the headaches that come with JavaScript. There are some people who don’t like CoffeeScript, though—it’s up to you whether to use it or not.
If you want to give CoffeeScript a try, you can test it out at CoffeesScript.org, where you’ll also find installation instructions and documentation. If you have npm installed, you can install CoffeeScript like so:
sudo npm install -g coffee-script
If you don’t have npm or want to use CoffeeScript in a different way, check the CoffeeScript website for more usage instructions.
One of the things that makes Jasmine specs look nice in CoffeeScript is the language’s optional parentheses. The following two lines are equivalent in CoffeeScript:
alert("Hello world!")
alert "Hello world!"Because Jasmine’s describe and it are just functions, you can write nice-looking specs that don’t have as many brackets and parentheses. You can have specs like this:
describe "CoffeeScript Jasmine specs", ->
it "is beautiful!", ->
expect("your code is so beautiful").toBeTruthy()Aren’t those nice?
Unfortunately, you can’t remove the parentheses from the expect calls, but you can easily remove them from everything else.
For more about CoffeeScript, take a look at Alex MacCaw’s The Little Book on CoffeeScript (O’Reilly).
If you want to use Jasmine to test your Node.js projects, you can! You can use it to test your browser-based projects, too.
First, you need to install the jasmine-node package. Type the following into your terminal:
sudo npm install -g jasmine-node
The -g flag installs jasmine-node on your system globally. If you’d prefer to keep it in a project directory, leave the flag off. This also (probably) means you don’t need sudo at the front.
First, download a ZIP of jasmine-node.
Unpack it and rename the folder as jasmine-node. Once you do this, move it into the same directory that you installed node.exe into. You should be up and running!
Now you have jasmine-node installed! Use it as follows:
jasmine-node /path/to/project/directory
Jasmine-node requires you to put your specs in a directory called spec and for the specs in that directory to end with .spec.js. You can also put specs in subdirectories of the spec directory.
For example, if you have a function like this in src/test.js:
global.hello = function() {
return 'world';
};A test spec for that might look like this:
// Include what we need to include: this is specific to jasmine-node
require("../src/test.js");
describe("hello", function() {
it('returns "world"', function() {
expect(hello()).toEqual("world");
});
});Other than the require calls that you need to make, the specs are just like browser-based Jasmine specs—except for one asynchronous component, that is.
Asynchronous tests work the same way as they do in “regular” Jasmine, but there’s another syntax that you can use: the done function. This signals to jasmine-node that your spec is, well, done.
Here’s an example of how you’d use it:
it("does an asynchronous call", function() {
exampleAsyncCall(function(response) {
expect(response).toContain("something expected");
done();
});
});The done function says to jasmine-node: “Hey, this is a function that deals with asynchronous stuff. Don’t call it quits until done is called…unless it takes too long and things time out (after 5 seconds).” You can adjust this timeout value by changing jasmine.DEFAULT_TIMEOUT_INTERVAL, like so:
// Change default timeout interval to 2 seconds jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;
If you want to use jasmine-node with CoffeeScript, you can. You’ll need to end your filenames with .spec.coffee and then run jasmine-node with the --coffee flag, like so:
jasmine-node /path/to/project/directory --coffee
That’s all you have to do! Jasmine-node will start picking up .spec.coffee files and test them properly.
Jasmine works well with Ruby on Rails, allowing you to test your JavaScript without constantly editing a spec runner HTML file.
The Jasmine documentation recommends one of Ryan Bates’s RailsCasts as a good way to get set up with Jasmine and Rails 3+. I think it is a fantastic resource, so give it a look if you’re interested. Or you can follow along here.
First, you’ll need to add Jasmine to your Gemfile, like so:
gem "jasmine"
Next, let’s install it:
bundle install rails generate jasmine:install
Now you’re all set up! This process will create a directory called spec if you don’t already have one. Inside of that, there’s a folder called javascripts. For this example, let’s make a test spec in spec/javascripts that just contains this:
describe("Jasmine + Rails test", function() {
it("works", function() {
expect(true).toBeTruthy();
});
});Save this as test.spec.js.
Now, from any directory, run the following command:
rake jasmine
After chugging a little bit, Jasmine will tell you that your tests are located at http://localhost:8888/. Visit that in your browser, and you’ll see the test pass! Now you can change test.spec.js to spec out whatever you’d like and go from there.
If you’d like to run your tests in the terminal, you can do that by running rake jasmine:ci.
If you have helpers (Jasmine plug-ins, for example), just put them in spec/javascripts/helpers and they’ll be automatically included.
You don’t need Rails to use Jasmine with Ruby. First, you need to install the Jasmine gem.
If your project uses Bundler, add the following to your Gemfile:
gem "jasmine"
If not, run the following commands in the command line:
gem install jasmine
Once you’ve installed Jasmine, you can run it like this:
jasmine init rake jasmine:ci
You should be up and running!
There are countless ways to integrate Jasmine into your project. I’ve covered only a couple here, but there are plenty. There’s a section on the Jasmine wiki about using Jasmine with Scala, Java, .NET, and plenty more.