![](https://codelido.com/assets/files/2023-01-08/1673199128-418595-1.png)
To use Jasmine framework to test Node.js applications, a series of steps needs to be followed.
In our example below, we are going to define a module which subtract 2 numbers which need to be tested. We will then define a separate code file with the test code and then use jasmine to test the Sub function accordingly.
Step 1) Define the code which needs to be tested. We are going to define a function which will subtract 2 numbers and return the result. This code is going to be written in a file called “SubNum.js.”
SubNum.js:
var exports=module.exports={};
exports.Sub=function(x,y)
{
return x-y;
};
Code Explanation:
The “exports” keyword is used to ensure that the functionality defined in this file can actually be accessed by other files.
We are then defining a function called ‘Sub.’, which is defined to take 2 parameters, x and y. The function is added to the module “exports” to make the function as a public function that can be accessed by other application modules.
We are finally making our function return the subtracted value of the parameters.
Step 2) Next we need to define our jasmine test code which will be used to test our “Sub” function In the SubNum.js file. The below code needs to put in a file called addnum-spec.js.
Note: The word ‘spec’ needs to be added to the test file so that it can be detected by jasmine
Main.js:
var val=require("../SubNum.js");
describe("Subtraction",function()
{
it("The function should subtract 2 numbers",function()
{
var res=val.Sub(24,31);
expect(res).toBe(7);
});
});
Code Explanation:
We need to first include our SubNum.js file so that we can test the ‘Sub’ function in this file.
We are now creating our test module. The first part of the test module is to describe a method which basically gives a name for our test. In this case, the name of our test is “Subtraction”.
The next bit is to give a description for our test using the ‘it’ method.
We now invoke our Sub method and send in 2 parameters 24 and 31. This will be passed to our Sub method in the Main.js file. The return value is then stored in a variable called value.
The final step is to do the comparison or our actual test. Since we expect the value returned by the Sub function to be 7, we define this using the method expect(value).toBe(the expected value).
OUTPUT:
In order to run the test, one needs to run the command jasmine.
The below screenshot shows that after the jasmine command is run , it will detect that there is a test called sub-spec.js and execute that test accordingly. If there are any errors in the test, it will be shown accordingly.
![](https://codelido.com/assets/files/2023-01-08/1673200565-578830-image.png)
Summary:
In order to test a Node.js application, the jasmine framework needs to be installed first. This is done by using the Node package manager.
The test code needs to be written in a separate file, and the word ‘spec’ should be appended to the file name. Only if this is done will jasmine be able to detect that a file needs to be run.
To run the test, you need to execute the jasmine command. This will find all files which have the ‘spec’ word attached to it and run the file accordingly