File size: 9,313 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# 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](https://github.com/facebook/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.
<!-- TOC depthFrom:2 depthTo:4 withLinks:1 updateOnSave:1 orderedList:0 -->
- [Basics](#basics)
- [Jest](#jest)
- [Testing Redux Applications](#testing-redux-applications)
- [Reducers](#reducers)
- [snapshots](#snapshots)
- [Actions](#actions)
<!-- /TOC -->
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:
```javascript
// add.js
export function add(x, y) {
return x + y;
}
```
> 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 can `import` 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](https://facebook.github.io/jest/).
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:
```javascript
// add.test.js
import { add } from './add.js';
```
Second, we `describe` our function:
```javascript
describe('add()', () => {});
```
> Note: `(arg1, arg2) => { }` is ES6 notation for anonymous functions, i.e. is
> the same thing as `function(arg1, arg2) { }`
Third, we tell Jest what `it` (our function) should do:
```javascript
describe('add()', () => {
it('adds two numbers', () => {});
it("doesn't add the third number", () => {});
});
```
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:
```javascript
// [...]
it('adds two numbers', () => {
expect(add(2, 3)).toEqual(5);
});
// [...]
```
Lets add the second test, which determines that our function doesn't add the
third number if one is present:
```javascript
// [...]
it("doesn't add the third number", () => {
expect(add(2, 3, 5)).toEqual(add(2, 3));
});
// [...]
```
> Note: Notice that we call `add` in `toEqual`. I won't tell you why, but just
> think about what would happen if we rewrote the expect as `expect(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:
```
add()
β adds two numbers
β doesn't add the third number
```
Lets say an unnamed colleague of ours breaks our function:
```javascript
// add.js
export function add(x, y) {
return x * y;
}
```
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:
```
β add() βΊ adds two numbers
expect(received).toEqual(expected)
Expected value to equal:
5
Received:
6
add()
β adds two numbers
β doesn't add the third number
```
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:
```
NavBar # Wrapping folder
βββ index.js # Actual component
βββ actions.js # Actions
βββ constants.js # Constants
βββ reducer.js # Reducer
βββ test # Folder of tests
βββ actions.test.js # Actions tests
βββ reducer.test.js # Reducer tests
```
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:
```javascript
// actions.js
import { TOGGLE_NAV } from './constants';
export function toggleNav() {
return { type: TOGGLE_NAV };
}
```
with this reducer:
```javascript
// reducer.js
import { TOGGLE_NAV } from './constants';
const initialState = {
open: false,
};
function NavBarReducer(state = initialState, action) {
switch (action.type) {
case TOGGLE_NAV:
return Object.assign({}, state, {
open: !state.open,
});
default:
return state;
}
}
export default NavBarReducer;
```
Lets test the reducer first!
### Reducers
First, we have to import the reducer and the action.
```javascript
// reducer.test.js
import NavBarReducer from '../reducer';
import { toggleNav } from '../actions';
```
Then we `describe` the reducer, and add two tests: we check that it returns the
initial state and that it handles the `toggleNav` action.
```javascript
describe('NavBarReducer', () => {
it('returns the initial state', () => {});
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
```javascript
{
open: false,
}
```
Lets put that into practice:
```javascript
describe('NavBarReducer', () => {
it('returns the initial state', () => {
expect(NavBarReducer(undefined, {})).toEqual({
open: false,
});
});
it('handles the toggleNav action', () => {});
});
```
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
```javascript
describe('NavBarReducer', () => {
it('returns the initial state', () => {
expect(NavBarReducer(undefined, {})).toMatchSnapshot();
});
it('handles the toggleNav action', () => {});
});
```
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](https://egghead.io/lessons/javascript-use-jest-s-snapshot-testing-feature).
This is how our finished reducer test might look like:
```javascript
// NavBar.reducer.test.js
import NavBarReducer from '../NavBar.reducer';
import { toggleNav } from '../NavBar.actions';
describe('NavBarReducer', () => {
it('returns the initial state', () => {
expect(NavBarReducer(undefined, {})).toMatchSnapshot();
});
it('handles the toggleNav action', () => {
expect(NavBarReducer({}, toggleNav())).toMatchSnapshot();
});
});
```
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`:
```javascript
// actions.test.js
import { toggleNav } from '../actions';
import { TOGGLE_NAV } from '../constants';
```
Then we `describe` the actions:
```javascript
describe('NavBar actions', () => {
describe('toggleNav', () => {
it('should return the correct constant', () => {});
});
});
```
> 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:
```javascript
it('should return the correct constant', () => {
expect(toggleNav()).toEqual({
type: TOGGLE_NAV,
});
});
```
If our `toggleNav` action works correctly, this is the output Jest will show us:
```
NavBar actions
toggleNav
β should return the correct constant
```
And that's it, we now know when somebody breaks the `toggleNav` action!
_Continue to learn how to test your application with [Component Testing](component-testing.md)!_
|