Test with Jest Free Tutorial Download
What testing means?
In tech jargon testing means checking that our code meets some expectations. For example: a function called “transformer” should returns the expected output given some input.
There are many types of testing and soon you’ll be overwhelmed by the terminology, but long story short tests fall into three main categories:
- unit testing
- integration testing
- UI testing
In this Jest tutorial we’ll cover only unit testing, but at the end of the article you’ll find resources for the other types of tests.
Jest Tutorial: what is Jest?
First things first: how do I know what to test?
When it comes to testing, even a simple block of code could paralyze beginners. The most common question is “How do I know what to test?”.
If you’re writing a web application a good starting point would be testing every page of the app and every user interaction. But, web applications are also made of units of code like functions and modules that need to be tested too.
There are two scenarios most of the times:
- you inherit legacy code which comes without tests
- you have to implement a new functionality out of thin air
What to do? For both cases you can help yourself by thinking of tests as of bits of code that check if a given function produces the expected result. Here’s how a typical test flow looks like:
- import the function to test
- give an input to the function
- define what to expect as the output
- check if the function produces the expected output
Really, that’s it. Testing won’t be scary anymore if you think in these terms: input – expected output – assert the result.
Now hands on Jest!