Installation and configuration

We will use the NPM tool to install Jasmine. Open your favorite Terminal and execute the following command:

$ npm install jasmine --global

The preceding instruction will install Jasmine as an executable program that you can invoke in any folder in your Terminal. We achieve this using the --global flag in the npm install command.

Once Jasmine is installed, we need to create our project example skeleton to practice with Jasmine. For this, we need to do the following:

  1. Create a new folder called practice-jasmine
  2. Initialize a new Jasmine project by executing the jasmine init command
  3. Write our test script

So, having the steps clear, open your Terminal and execute the following commands:

$ mkdir practice-jasmine
$ cd practice-jasmine
$ jasmine init

Once the jasmine init command is executed, it will create the following folder structure for our project:

└───spec 
└───support
└───jasmine.json
You can get the preceding tree list by installing the tree program and executing it into your practice-jasmine folder with the tree command.

As you can see in the root of the project's folder, there is a spec folder, where we will save all our test scripts. Right in the spec folder, there is a support folder where a jasmine.json file has been created for us. This file contains all the configuration that Jasmine will use to find our test scripts and then execute them. Using the editor of your choice, open the jasmine.json file:

{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": true
}

The important properties to consider in this file are spec_dir, which points to the folder that hosts our test scripts, and spec_files, which declares a regular expression that tells Jasmine that the files containing spec.js or Spec.js at the end of their names have to be considered as test scripts and have to be processed.

To validate that everything is configured correctly, execute the following command in the root practice-jasmine directory:

$ jasmine

Started


No specs found
Finished in 0.002 seconds

You should see the message No specs found displayed which means that as we didn't write any test yet, Jasmine was not able to process any test case. Also, you can see how much time Jasmine takes to execute your tests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset