Unit testing
Unit testing is the practice of testing the smallest possible units of our code, functions. We run our tests and automatically verify that our functions do the thing we expect them to do. We assert that, given a set of inputs, our functions return the proper values and handle problems.
This boilerplate uses the Jest test
framework to run tests and make assertions. This library makes writing tests as easy as speaking - you
describe
a unit of your code and expect
it
to do the correct thing.
We use this glob pattern to find unit tests app/**/*.test.js
- this tells
Jest to run all files that end with .test.js
anywhere within the app
folder. Use this to your advantage, and put unit tests next to the files you
want to test so relevant files stay together!
Basics
For the sake of this guide, lets pretend we're testing this function. It's
situated in the add.js
file:
Note: The
export
here is ES6 syntax, and you would need an ES6 transpiler (e.g. babel.js) to run this JavaScript.
The
export
makes our function available as a module, which we canimport
and use in other files. Continue below to see what that looks like.
Jest
Jest is our unit testing framework. Its API, which we write tests with, is speech like and easy to use.
Note: The official documentation for Jest can be found here.
We're going to add a second file called add.test.js
with our unit tests
inside.
First, we import
the function in our add.test.js
file:
Second, we describe
our function:
Note:
(arg1, arg2) => { }
is ES6 notation for anonymous functions, i.e. is the same thing asfunction(arg1, arg2) { }
Third, we tell Jest what it
(our function) should do:
Now, We're going to test that our little function correctly adds two numbers.
We are going to take some chosen inputs, and expect
the result toEqual
the
corresponding output:
Lets add the second test, which determines that our function doesn't add the third number if one is present:
Note: Notice that we call
add
intoEqual
. I won't tell you why, but just think about what would happen if we rewrote the expect asexpect(add(2, 3, 5)).toEqual(5)
and somebody broke something in the add function. What would this test actually... test?
Should our function work, Jest will show this output when running the tests:
Lets say an unnamed colleague of ours breaks our function:
Oh no, now our function doesn't add the numbers anymore, it multiplies them! Imagine the consequences to our code that uses the function!
Thankfully, we have unit tests in place. Because we run the unit tests before we deploy our application, we see this output:
This tells us that something is broken in the add function before any users get the code! Congratulations, you just saved time and money!
Testing Redux Applications
Imagine a navigation bar, this is what its folder might look like:
This boilerplate uses Redux, partially because it turns our data flow into
testable (pure) functions. Using the NavBar
component above,
let's see what testing the actions and the reducer would look like.
This is what our NavBar
actions look like:
with this reducer:
Lets test the reducer first!
Reducers
First, we have to import the reducer and the action.
Then we describe
the reducer, and add two tests: we check that it returns the
initial state and that it handles the toggleNav
action.
Lets write the tests themselves! Since the reducer is just a function, we can
call it like any other function and expect
the output to equal something.
To test that it returns the initial state, we call it with a state of undefined
(the first argument), and an empty action (second argument). The reducer should
return the initial state of the NavBar
, which is
Lets put that into practice:
This works, but we have one problem: We also have to explicitly write the initial state itself. When somebody changes the initial state, they will also have to manually update this code to directly reflect it.
Instead, we can leverage Jest's new snapshots feature.
Snapshots
Jest has the ability to store serialized snapshots of most basic types of information (objects, arrays, etc). It then compares the stored version when later tests are run, to find any unexpected mismatches.
We can write the test like
Jest is now the one responsible for tracking the definition of the initial state. When somebody changes it in the future, Jest will warn that the snapshot doesn't match and then allow them to update the snapshot with a single command. No more manual updates!
For more details on Jest snapshots, please view Kent Dodd's feature video.
This is how our finished reducer test might look like:
Lets see how we can test actions next.
Actions
We have one action toggleNav
that changes the NavBar
open state.
A Redux action is a pure function, so testing it isn't more difficult than
testing our add
function from the first part of this guide!
The first step is to import the action to be tested, the constant it should
return and expect
:
Then we describe
the actions:
Note:
describe
s can be nested, which gives us nice output, as we'll see later.
And the last step is to add the assertion:
If our toggleNav
action works correctly, this is the output Jest will show us:
And that's it, we now know when somebody breaks the toggleNav
action!
Continue to learn how to test your application with Component Testing!