Setup Jasmine testing Framework in Node JS | a step by step guide

ivadyhabimana

Ivad Yves HABIMANA

Posted on August 25, 2022

Setup Jasmine testing Framework in Node JS | a step by step guide

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 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


Enter fullscreen mode Exit fullscreen mode

for now your package.json should look something like this

package.json file

2. Install and configure Jasmine

Run the following command to install Jasmine dependency



npm install --save-dev jasmine


Enter fullscreen mode Exit fullscreen mode
  • 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 


Enter fullscreen mode Exit fullscreen mode
  • 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



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


Enter fullscreen mode Exit fullscreen mode

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 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



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);
  });
});


Enter fullscreen mode Exit fullscreen mode

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"



//... other code

"scripts": {
    "test": "jasmine"
  },


Enter fullscreen mode Exit fullscreen mode

when you run npm test jasmine will be launched and test your application

running test

Our tests are passing!

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

References

💖 💪 🙅 🚩
ivadyhabimana
Ivad Yves HABIMANA

Posted on August 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related