Setup Jasmine testing Framework in Node JS | a step by step guide
Tags: testing, javascript, beginners, node
Published: 2022-08-25 10:00:00

In this guide I will walk you step by step through the process of setting up Jasmine Testing framework in a Node JS environment. We will introduce Jasmine as a testing Framework, get it installed and configured and write a simple tests to demonstrate the process of Testing with Jasmine. 

**Prerequisites**
- **Node**: You need to have node installed on your machine as we will be using it to install Jasmine and run our files
- **VS Code** I recommend using Visual studio code as the code editor but feel free to use any code editor of your choice.

We will go over all concepts step by step but I will assume that you have the basics of working with JavaScript and Node applications.

### Introducing Jasmine JS
Jasmine is a simple and popular testing framework for testing JavaScript application. From their [official documentation](https://jasmine.github.io/) they describe Jasmine as a _framework for Behavior-driven JavaScript._

Being behavior driven means Jasmine supports and promotes Behavior driven development

**Behaviors Driven Development (BDD)**

Behavior driven development (BDD) is a testing practice that focuses on testing the way the application behaves when end-users interact with it. The idea is to describe how the application should behave in a very simple user/business-focused language.

Behavior driven development is invented from Test driven Development which promotes writing test for your application and later write the actual code to make your tests pass 

I won't focus much on these concepts since this article is just about setting up Jasmine but if you want to learn more about them check resources shared in the references

**1. initializing node project**

create a new folder, open it in VS Code and run `npm init -y` this will initialize a new node project and add the `package.json` file.

```
npm init -y
```
for now your `package.json` should look something like this

![package.json file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvrpw07oiibnolbr6oj2.png)

**2. Install and configure Jasmine**

Run the following command to install Jasmine dependency

```
npm install --save-dev jasmine
```
- we are installing `jasmine` package as the one that will enable us to write tests. we install it as a dev dependency because you only need testing in development only

Run the following command to initialize Jasmine in your project

```
npx jasmine init 
```
- By running this command a `spec` folder should be created and in it there will be another folder called `support` which contains a `jasmine.json` file and this is the file that contains Jasmine configuration 

By default `jasmine.json` will look something like this 

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

- `"spec_dir"`: specifies the directory to find configurations and tests for Jasmine to run in this case it is set to `spec` (the one we just created)
- `"spec_files"`: file that Jasmine will look for when running tests in this case Jasmine will run all files that has `.spec.js` or match that declared pattern
- `"helpers"`: files that contains configurations that Jasmine will include when running tests in this case if there is any it will expect them to be in the `helpers` folder
- `"env"`: specifies the environment Jasmine will run in
- `"stopSpecOnExpectationFailure"`: whether it will stop execution of a spec after the first expectation failure in it in this case it is set to false.

You can customize these configurations according to your project needs and you can find more configurations in the official docs [Here](https://jasmine.github.io/setup/nodejs.html#configuration) for the simplicity I will keep everything the way it is

**3. Writing your first test**
in the spec folder create a file called `index.spec.js` and add the following code 

```js
describe('simple tests', () => {
  it('should find true to be true', () => {
    expect(true).toBe(true);
  });
  
  it('should find false to be different from true', () => {
    expect(false).not.toBe(true);
  });
});
```

These are just simple tests that are not basically testing anything we are expecting `true` to be `true` and `false` to not be `true` and these should pass for just demonstration

In a real application you will be writing test that actually test the application behavior and tests such as these ones above would be useless

**4. Testing script**
In the `package.json` in the field of scripts add the `"test"` script and set the value to `"jasmine"` 

```js
//... other code

"scripts": {
    "test": "jasmine"
  },
```
when you run `npm test` jasmine will be launched and test your application

![running test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyy6fnexg15kw3sti49r.png)

Our tests are passing!

That's it, that's how you setup Jasmine testing framework in a Node environment. cheers!

**References**
- [Code mentioned in the article](https://github.com/Yvad60/blog-code-references/tree/setup-jasmine)
- [Behavior driven development](https://en.wikipedia.org/wiki/Behavior-driven_development)
- [Test driven development](https://en.wikipedia.org/wiki/Test-driven_development)
- [Jasmine configurations](https://jasmine.github.io/setup/nodejs.html#configuration)
- [Jasmine docs](https://jasmine.github.io/index.html)
- [Your first Jasmine suite](https://jasmine.github.io/tutorials/your_first_suite)