JavaScript unit testing with Jasmine

For this tutorial, we first need to install the latest version of Jasmine. We will use Jasmine 3.4 as a standalone version

Download the latest version of Jasmine from https://jasmine.github.io and unpack the file in your workspace

Alternatively, you can install Jasmine from the command line:

#Global installation
npm install -g jasmine

#Local installation:
npm install –save-dev jasmine

After installation, you have the following file structure in your workspace:

/lib
/spec
/src
SpecRunner.html

The /spec directory will host all your tests and /src will host your JavaScript code. Open SpecRunner.html to see which tests passed and which failed

In Jasmine tests are summarized to suites. A Jasmine suite consists of several tests or specifications and is included in the describe() function. The describe () function has the following signature: describe (‘name or title’, function callback ())

describe(“Test Suite”, () => {

})

Specs are tests that are implemented in the it () function. The it () function has the following signature: it (‘name or title’, function callback ())

In the next section, we will implement some of the most commonly used tests during unit testing with Jasmine. Keep in mind that tests, also known as specs, are executed in the it () function

In our first Jasmine suite, we want to test whether a function named “orderTotal” meets the following requirements:

a. If orderTotal is of type function
b. Returns the aggregate of the prices of all products in the order
c. If productDescriptions is of type function
d. All products have a string product which is not empty

SpecRunner.html