Installing Jasmine on Windows
- Download and install node.js (it comes as standard Windows .msi installer )
- Once it’s done, type the following in the command line to see whether node’s package manager (npm) was successfully installed (we’ll use npm to download further modules):
> npm --version
Now we only need few more modules: Yeoman, Bower and Generator-Jasmine. Type following in console:
> npm install -g yo
> npm install -g bower
> npm install -g generator-jasmine
-g switch tells npm to install packages in node’s global modules directory (rather than locally within your project’s directories).
To finalize testing environment setup, we need to scaffold Jasmine’s tests directory. To do that, we’ll navigate to project directory and use Yeoman’s yo tool:
> yo jasmine
This will create test directory with index.html and spec/test.js files, which will be of your primary interest.
Running first test
The index.html is Jasmine’s test runner – open it in browser and your tests will run. “How? What tests?” you might ask. Let’s take a quick look and index.html:
We simply need to reference our implementation and test files:
What’s next? First test, obviously. Since this is super-fresh environment our first test for
highlightMatches function is going to be trivial, requiring the implementation to only return value:
Explanation of Jasmine’s methods and BDD-style can be found at Jasmine Introduction page. Without further due, we add equally simple implementation of
To get it we need to execute the following (the
karma-cli is Karma’s command line interface module):
> npm install -g karma
> npm install -g karma-cli
Next, navigate to project directory and initialize configuration. Karma will “ask” few simple questions and basing on your answers it will generate config file (js.config.js):
> karma init jk.config.js
Which testing framework do you want to use ?
Press tab to list possible options. Enter to move to the next question.
What is the location of your source and test files ?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Enter empty string to move to the next question.
Configuration is ready. All that’s left to do is simply run Karma, passing configuration file name as argument:
> karma start jk.config.js
Everything should be find and we’ll be greeted with message similar to the one below:
Modifying test to make it fail will get noticed immediately:
- Install node.js
- (optional) Install Jasmine, Yeoman and Bower
npm install -g yo bower generator-jasmine(this trio isn’t needed when you use Karma – Karma will take care of dependencies on its own)
- (optional) Scaffold Jasmine test directory
- (optional) Run first test by opening Jasmine’s index.html
- Install Karma
npm install -g karma karma-cli
- Configure Karma
karma init <config>
- Start Karma
karma start <config>