Jasmine

Jasmine is a behavior-driven JavaScript testing framework that has been around longer than some of the oldest JavaScript frameworks themselves. Backbone was originally released in October 2010, but Jasmine was actually released one month earlier, in September 2010. Jasmine was originally a port of the popular Java testing framework named jUnit, and was called jsUnit. The age of this framework shows that it has stood the test of time, and has been constantly updated with new features to allow even the most modern frameworks to use it.

Jasmine has a very simple syntax, and is designed so that it can be read and understood easily. It can also be extended easily, and is the recommended framework for Aurelia as well as Angular unit and integration testing. Installation of Jasmine using npm is as follows:

npm install jasmine --save 

The relevant declaration files for Jasmine can be installed by using @types as follows:

npm install @types/jasmine -save-dev

We can run Jasmine directly from the command line in Node by installing Jasmine as a global module, as follows:

npm install -g jasmine

Once installed, we will need to initialize our project directory for Jasmine to use, as follows:

jasmine init

This will create a spec directory, where our tests should reside, and a spec/support/jasmine.json file, as follows:

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

Here, Jasmine has set up a sample jasmine.json file that has a few standard properties. The spec_dir property tells Jasmine that it should look in the /spec directory to find runnable tests. The spec_files property specifies that any file that ends with .spec.js or .Spec.js will be considered to be a Jasmine test specification. We will not use the helpers directory just yet, although it is there to load files that our tests may need. The stopSpecOnExpectationFailure property set to false means that Jasmine will continue to run all tests in the suite, regardless of failure. The random property is used to randomly select a test to run from the entire suite.

Our full suite of tests (which do not actually have any tests yet), can be run as follows:

jasmine

As there are no tests in the specs folder as yet, Jasmine will report as such:

Finished in 0.001 seconds
Incomplete: No specs found
..................Content has been hidden....................

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