- Numeric values are one of the most important data types
that require testing in your code.
In this video, we'll be doing just that.
Your official JS documentation consists of
a couple of matches for testing numbers,
and we will be making use of some of them in this exercise.
To follow along with this video,
you need the 02_03b branch of the project.
So make sure you are on the 02_03b branch.
A new model has been added to the project that will contain
utilities to be used in our tests.
Inside the source folder,
there's a utilities.js file containing the utilities model.
The model currently contains a single function,
"addNumbers".
This function simply adds the two numbers
supplied to it as arguments.
We'll be making use of this model in our next test.
Navigate to the tests folder.
And within it create a new file numbers.js.
(keyboard typing)
This file will contain test cases for our numeric tests.
First thing to do in this test is to import
the addNumbers function.
(keyboard typing)
Good, now let's add a test case
that uses addNumbers to add the numbers four and five.
(keyboard typing)
Using it toEqual Jest matcher,
let's assert its result to be nine as expected.
Good, now save the file
and returned it to the command line to run the tests.
We currently have two test suites in the project.
One for the strings test,
from a previous video and the one we just added for testing
numeric values.
If we want npm, run test,
Jest will run all the tests suites currently available
within the project.
We can, however,
run a specific test suite by adding a space
and the name of the test suite file,
after the test command.
Let's run the numbers.js file this way.
As seen from the results,
just reports a passing state for our addNumbers function.
Let's introduce a bug into this function
and see if our tests can help us detect that.
Going to the utilities model.
And we'll place the addition operator,
which is subtraction.
Save the file and switch back to the command line
to run the test suite once again.
This time Jest reports that our function is not producing
the right output,
which indicates a bug in its implementation.
This shows the importance of tests in our application.
As we can be quickly notified of unwanted behaviors
in our code.
Let's go back to the utilities model to fix the function
and next we'll look at testing floating point numbers.
To test floating point numbers,
we'll be adding another test case.
This time we'll be adding floating point numbers,
0.4 and 0.5 to a certain expected result of 0.9.
(keyboard typing)
Good.
Now save the file and switch back to the command line
to run the test suite once again.
(keyboard typing)
Everything appears to be fine.
our two tests are passing as expected.
However, there's an hidden bug right inside our code.
Switch back to the test file
and this time add the floating point numbers 0.1 and 0.2.
Asserting an expected result of 0.3.
Make sure you save the file.
Now we talk to the command line,
I'm going to test case once again.
(keyboard typing)
Surprise, surprise, our test fails.
But why?
Turns out floating point numbers often have
rounding up errors.
and this is common across different
programming languages, JavaScript inclusive.
As seen, the result didn't turn out to be exactly 0.3.
It was a teeny tiny little bit above 0.3.
But don't worry, just adjust the right matcher
to fix this problem.
Back in the text editor,
let's replace the toEqual matcher in the floating point
numbers test to the, "toBeCloseTo" matcher.
This matcher is preferred by Jest for testing equality
in floating point numbers.
Save the file on and head to the command line.
Now run the test once again
and this time we should have passing results for both tests.
(keyboard typing)
Awesome. In the next video,
we'll take a look at how we can test objects and arrays.