Jest 101

Why use Jest

Jest is a testing framework that helps us easily doing tests in javascript, it works out of the box on most projects. But first I want to explain the types of testing there are that I have investigated.

Types of Tests

Unit Tests

Unit tests only test a single part of an implementation. Basically a unit, theres no dependencies or integrations involved.

Integration Tests

Integration tests concern about the functionality of the code with another third party, this could be a database, a server, another module, etc.

Functional Tests

Functional test makes sure the system works as the user expects it to behave.

Frequency of Tests

The smaller and more isolated the tests the more frequent they become because of their simplicity, so functional tests are written just a few times.

Test will help build a software which is less prone to defects latter in the future and more well structured.

What I learnt using jest

I used an already built simple project with jest with the configuration being done, what my objective was is learning the basics structure of the jests test and how it is applied in a project.

First Test

First I made the simplest test ever to just see how a test is written, it was a simple arrow function which returns Hello as a string. All I need to do with the function is export to later imported in a test file.

Later imported the function in a test file which expects the output to be Hello, it shows that it ran correctly, the good thing about jest is the feedback given by the framework and every time a change is made and save the test run automatically.

The test has a describe to grab the function being imported, the it first parameter gives a description of what the test should do, and the second is a functions with the expect which is the test case that will run. We expect hello() to have “Hello” as an output.

To give another example which is more likely to happen when coding in a project I used a simple technique call TDD (Test Driven Development) where tests are written first and the code is done after to make it comply with the tests being made. This makes it easier to make the code reliable for the project, not just making it work for some cases, but all cases possible. The function will have to remove all names that begin with a letter s.

I wrote three kind of tests, one that should see if all name with S are removed, one that keeps all names without a letter S as a first letter, and one that takes attention to the lower and upper case letters. With that done I can make the function. Each expect has a matcher toContain or a negated toContain.

As an example I omitted the removal of upperCase letters. Just as an example of how jest can tell me what went wrong and help me figure out what the code needs to have.

In the third case I see that “scott” passed and “Scott” did not pass, which I can tell that I’m not accounting for upper case letters. I can also see the expected array given by the method and the result. By adding a toLowerCase function to the name array the problem is fixed. Another useful case for TDD is if I made my function to have a for loop instead of a filter I can later try to optimize the code and see how well its made by the feedback of the tests cases.

My conclusions

By making tests code can be thought out more, make functions smaller to making them easily testable with unit tests, have code that works in all cases so that each part of the project wil not fail in the future with unforeseen problems and can save a lot of time. When I was working in my social service making a webpage the automatic test runs could have saved me a lot of time becuase I made the tests case manually by inputting values that came to my head one by one and later forgot which one I did. With a tool like jest I can have my tests organize and easily run them whenever I need.

I will be learning more about Jest in the future and will write about my progress.