How to Start Unit Testing with Jasmine
Tags: javascript, testing, jasmine
Published: 2022-08-17 04:26:00

In my [previous article](https://how-to.dev/the-simplest-case-for-unit-tests-pure-functions), I showed pseudocode examples of unit testing _pure functions_. In response to that article, I got an excellent question—so how do I start testing my code then? Let’s take a more practical look at the same, simple case for testing.

## Get the codebase

I prepared a [repository](https://github.com/how-to-js/how-to-unit-test) with the example code. The main content is `pure-functions.js`:

```JS
export function greet(name, surname) {
  return `Hello ${name} ${surname}!`;
}

export function calculateDiscountedPrice(originalPrice, discount) {
  return originalPrice - originalPrice * discount;
}

export function power(base, exponent) {
  return base ** exponent;
}
```

As you can see, the code is simple. 

### Gotcha if you create a new package locally

If you want to copy the code file instead of the whole repository, then there is one gotcha. You will need to:
1. Generate a package with:
```SH
$ npm init
```
So, your node dependencies are installed in the current folder.
2. Switch the code to be _module_, by adding to `package.json`:
```JSON
{
  …
  "type": "module",
  …
}
```
You can see how it’s done in [my repo](https://github.com/how-to-js/how-to-unit-test/blob/main/package.json#L6)


## Install Jasmine

The code we are going to test will work the same way on browser or Node. For simplicity, let’s test it on Node. First, we need to install Jasmine. To do so, let’s follow the [official documentation](https://jasmine.github.io/pages/getting_started.html):

```sh
$ npm install --save-dev jasmine #1

$ npx jasmine init #2
```

The commands perform the following functions:

1. Install Jasmine as development dependency
2. Generate the basic configuration in `spec/support/jasmine.json`:

```JSON
{
  "spec_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.?(m)js"
  ],
  "helpers": [
    "helpers/**/*.?(m)js"
  ],
  "env": {
    "stopSpecOnExpectationFailure": false,
    "random": true
  }
}
```

Lastly, let’s update `package.json` to configure the testing command:

```JSON
{
  …
  "scripts": {
    "test": "jasmine"
  },
  …
}
```

## Running (no) tests

At this point, you should have completed all the necessary configuration, and not a single test in place. Let’s see if it works as expected:

```SH
$ npm test

> how-to-unit-test@1.0.0 test
> jasmine

Randomized with seed 10285
Started


No specs found
Finished in 0.002 seconds
Incomplete: No specs found
Randomized with seed 10285 (jasmine --random=true --seed=10285)
```

## Example test

Let’s add a simple test. We will create a new file `./spec/pure-functions.spec.js` that matches the Jasmine convention:
* it’s inside `./spec` folder—following what is set in this line of the generated configuration: `"spec_dir": "spec",`
* it ends with `spec.js`—another naming pattern established in `"spec_files": ["**/*[sS]pec.?(m)js"`

Code that goes inside `./spec/pure-functions.spec.js`:

```JS
﻿﻿import { greet } from "../pure-functions.js";

describe("greet", () => {
  it("should greet by name & surname", () => {
    expect(greet("Lorem", "Ipsum")).toEqual("Hello Lorem Ipsum!");
  });
});
```

Code:
* `﻿﻿import { greet } from "../pure-functions.js";`—gets the function from our source code. This line wouldn’t work as expect without `"type": "module",` in `package.json`.
* `describe("", <callback>)`—wraps related tests for better error messages.
* `it("", <callback>)`—an individual test,
* `expect(<value>).<matcher>(<arguments>);`—it’s how we set expectations in Jasmine. You can find [matchers in the documentation](https://jasmine.github.io/api/edge/matchers.html).

## Run!

This test running:

```SH
$ npm run test

> how-to-unit-test@1.0.0 test
> jasmine

Randomized with seed 09863
Started
.


1 spec, 0 failures
Finished in 0.004 seconds
Randomized with seed 09863 (jasmine --random=true --seed=09863)
```

## Final code

You can find my final code [here](https://github.com/how-to-js/how-to-unit-test/tree/jasmine).

## Homework

Here’s some homework: get the code, and continue reimplementing tests from the [first article](https://how-to.dev/the-simplest-case-for-unit-tests-pure-functions). You are welcome to share the results or struggles in the comments