Jasmine is an independent unit testing framework for effectively testing JavaScript components in a behavior-driven development architecture.
Installing jasmine-node on Unix and Linux
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. Leave the flag off if you’d prefer to keep it in a project directory. This also (probably) means you don’t need sudo at the front.
Basic Usage
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 required calls you need to make, the specs are just like browser-based Jasmine specs—except for one asynchronous component.
If you encounter the below error while running the jasmine-node command:
jasmine-node: command not found
you may install it using the below commands as per your choice of distribution.
Distribution | Command |
---|---|
Arch Linux | pacman -S jasmine-node |
Fedora | dnf install jasmine-node |
Jasmine and Ruby on Rails
1. First, you’ll need to add Jasmine to your Gemfile, like so:
gem "jasmine"
2. Next, let’s install it:
bundle install rails generate jasmine:install
jasmine-node and CoffeeScript
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