getting to know jasmine

Your javascript code should have the privilege of meeting jasmine the unit testing framework for javascript.

First of all, jasmine is a girl that we programmers don’t know, she is actually a behavior-driven development (BDD) framework to apply tests to the javascript code of our projects. To carry out the tests, the DOM (‘Document Object Model’) is not needed.

The reason for jasmine is that many programmers are developing a web project, it is almost 99% that javascript or some library derived from this language is used, also when coding in this language many functions for events or some other functionality are created within the project . In addition, many programmers have seen unit tests as something very annoying because some frameworks of these types did not make the developer’s job a little easier, jasmine in her simplicity tries to make life easier for the person who implements it in the project

Characteristic

  • It has a very clear syntax
  • functions that allow easier typing
  • each test created by the user is a function

set of functions

The facility to be able to write unit tests in jasmine is thanks to the functions that this framework has, which are the following

suites:

The “suites” is a name that describes which genre or section is going to go through a set of unit tests, it also has a tool that is the core that is needed to be able to have an order when creating the tests. “describes” is a global function and it is with which every unit test starts, it also has two parameters and its syntax is as follows describe(“”, function(){});

  • The first parameter is a character string where the name of the unit test is defined.
  • The second parameter is a function where is the code that will execute the code test.

Specifications:

Expectations in jasmine (are statements that can be true or false depending on what result the programmer expects from the unit test.

See also  Light weighted animated photos

“it” like “describe” is a function that takes two parameters, the first is a character string to describe a title or name of the unit test and as a second parameter a function where a block of code is executed, and the syntax is the following it(“”, function(){});

Expectations:

They are built with the “expect” function which takes the current value, that value is linked to the matcher function which takes the expected value. The syntax is as follows expect(current value).matchers(expected value)

Matchers:

Matchers are functions that implement boolean comparisons between the current and expected values, they are responsible for reporting to jasmine if the expectation is true or false.

List of most common matchers, although custom matchers can be created according to the programmer’s needs.

  • expect(x).toEqual(y); Check if both values ​​are equal.
  • expect(x).toBe(y); Check if both objects are equal.
  • expect(x). toMatch(pattern); checks if the value belongs to the set pattern.
  • expect(x).toBeDefined(); checks if the value is defined.
  • expect(x).toBeUndefined(); Check if the value is undefined.
  • expect(x).toBeNull(); checks if the value is null.
  • expect(x).toBeTruthy(); Check if the value is true.
  • expect(x).toBeFalsy(); check if the value is false.
  • expect(x).toContain(y); checks if the current value contains the expected one.
  • expect(x).toBeLessThan(y); checks if the current value is less than expected.
  • expect(x).toBeGreaterThan(y); checks if the current value is greater than expected.

Now after the vast explanation that was given in the previous list, we will talk about other functionalities that jasmine offers us.

Group “specs” by “describe”

The title is due to the fact that jasmine allows us to group many “specs” within a “describe” that has a clear description of what is being done and can be seen in the following example so that it can be more clear.

But before executing our first example, we must download the file that contains Jasmine to be able to carry out our tests, with the following link, once on your computer you extract the files and in the folder called “spec” you place the files or scripts where the unit tests and in the “src” call they place the files that have the functions of the project. The last step is to look for the file called “SpecRunner.htnl” and it is where you put the paths of the files placed in the folders mentioned above, after that let’s get to work.

See also  Input file that allows uploading several files at the same time

Example 1:

describe(“arithmetic operations”, function(){
it(“addition”, function(){
var sum = 1 + 3;
expect(sum).toEqual(4);
});

it(“subtraction”, function(){
var subtraction = 2 + 1;
expect(subtract).toBeLessThan(4);
});

it(“multiplication”, function(){
var multiplication = 2 * 10;
expect(multiplication).toBeGreaterThan(9);
});

it(“division”, function(){
var division = 15 + 3;
expect(division).toEqual(5);
});
});

In this example we can see how unit tests of the four basic arithmetic operations are executed. As can be seen in the example, many tests can be grouped into a specific group thanks to “specs” and “describe”.

Everything does not end with the previous sample, jasmine still has more surprises for us and they are and will be explained with the next block.

Organizations (“Setup”) and Teardown (“Teardown”)

With these concepts you can avoid code duplication and keep initialized variables in one place while maintaining modularity. Jasmine provides global functions called “beforeEach” and “afterEach” with which our unit tests will be easier to perform.

  • beforeEach is executed before each “spec” within the “describe”.
  • afterEach is executed after each “spec” within the “describe”.

With the following example we will see how these excellent functions are used.

Example 2:

describe(“arithmetic operations”, function(){
var sum;
var subtraction;
var multiplication;
var division;

beforeEach(function(){
sum = 1 + 3;
subtraction = 2 + 1;
multiplication = 2 * 10;
division = 15 + 3;
});

afterEach(function(){
sum = 0;
subtraction = 0;
multiplication = 0;
division = 0;
});

it(“addition”, function(){
expect(sum).toEqual(4);
});

it(“subtraction”, function(){
expect(subtract).toBeLessThan(4);
});

it(“multiplication”, function(){
expect(multiplication).toBeGreaterThan(9);
});

it(“division”, function(){
expect(division).toEqual(5);
});
});

This example is similar to the previous one but with the difference that we apply the functions mentioned in the previous paragraph.

Nesting (“Nesting”) and Blocks (“Blocks”)

Another great help that jasmine gives us is nesting and blocks which are combined to make the work of unit tests somewhat more pleasant for the programmer when seeing how he is going to order his set of tests since generally in projects they are they code many functions therefore the list of tests increases as time goes by and the functionalities of the project grow.

See also  CASE structure

The purpose is to be able to create “describe” inside “describe” so this is done to give it more clarity and modularity, the best way to explain this is through our last example.

Example 3:

describe(“calculator”,function(){
describe(“arithmetic operations”, function(){
var sum;
var subtraction;
var multiplication;
var division;

beforeEach(function(){
sum = 1 + 3;
subtraction = 2 + 1;
multiplication = 2 * 10;
division = 15 + 3;
});

afterEach(function(){
sum = 0;
subtraction = 0;
multiplication = 0;
division = 0;
});

it(“addition”, function(){
expect(sum).toEqual(4);
});

it(“subtraction”, function(){
expect(subtract).toBeLessThan(4);
});

it(“multiplication”, function(){
expect(multiplication).toBeGreaterThan(9);
});

it(“division”, function(){
expect(division).toEqual(5);
});
});

describe(“special operations”,function(){

beforeEach(function(){
var squareroot = 0;
var exponent = 0;
var base = 0;
});

afterEach(function(){
var squareroot = 0;
var exponent = 0;
var base = 0;
});

it(“square root”,function(){
squareroot = 4
expect(squareroot).toEqual(Math.sqrt(16));
});

it(“exponent”,function(){
var exponent = 3;
var base = 4;
answer = Math.pow(base,exponent);
expect(response).toEqual(64);
});

});
});

With this example, it can be seen that the nesting described in the previous paragraph is something that helps us as programmers who carry out unit tests in our projects. Apparently, during this writing, all that remains is to be encouraged to implement this excellent tool in our current or future web project, all that remains is to say “Happy Testing”.

Loading Facebook Comments ...
Loading Disqus Comments ...