After reading a lot of forums and articles related to this topic of software testing, we noticed that most of the time the testers skips the part of the test for lack of time but ends up needing this time “saved”, in the middle or at the end of the project to correct something that the test would easily pick up.
Another problem we noticed is that the staff has used console.log() to analyze/validate your code in development and in a few moments, to help fix something that is already in production. Below you can see an example of a test with console.log() taken from the WEB.
This was just a simple one with console.log(), but what if you found something to like in the image below?
This is one of the problems with testing the code with the console.log(). These examples above are simple, but searching for the WEB you can find something like: “will work“, ” thankGod was“, ” helpsir”.
We know that an ordinary user does not access the browser console to analyze code, but what happens when thinking of maintenance or new functionality in the system?
We believe that in this way, in addition to the code being difficult to debug, there is still a great chance to create some future problems (bugs).
Thinking about it, we decided to study Jasmine and create a series of articles demonstrating how to automate your tests with him.
Above you have a HelloWorld call returning a string. On your day you will create something more complex, but for the first example, here’s how to test this function. function
As you can see in the previous example, to test something with Jasmine you can use: describe, it and expect. Below you have a brief description of each of them:
describe(string, function): name of the scope of the test
it(string, function):test name
expect(actual):event/function call … etc that will be tested
In the previous example, we used to validate if the return was expected. But there are other types that you can use to validate your tests. See the list below for some of Jasmine’s default returns, which you can implement in your code.toEqual
Imagine the following scenario: You need to create a method that validates whether a user is of the minimum age to access X content. You need to develop something that can be called from anywhere on the system and something that validates the user whether they are allowed to access that content based on their date of birth.
To create this test we will use vs code (Visual Studio Code) and jasmine library that you can find at the following link: Jasmine. Download it to your computer, then unzip the .zip file and open the code in your VS Code. Below you have an image demonstrating this step:
Analyzing the above code you have:
lib: directory with Jasmine libraries
spec: directory for creating the tests
src: directory containing the business classes
The next step will be to create the code responsible for validating the test scenario described above. To do this, create the files below within your project in VS Code:
Validation of other parameters
free: to indicate that the content can be viewed by all
date of birth: to pass the age of the user
age: that the user needs to have to access this content
Below is a scenario validating all the requirements of our test:
Now to run and validate the test. We’ll use a VS Code plugin called live serves. But if you don’t have it or aren’t using VS Code, you can upload your code on IIS or another WEBserver.
With the live server running on my VS Code, below is an image demonstrating the status of the tests developed in the previous step: