Chapter 7. Using Jasmine with Other Tools

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.

Jasmine and CoffeeScript

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. ...

Get JavaScript Testing with Jasmine now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.