02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Testing with JEST

Testing with JEST: Setting Up Automated Testing for Redux

Before diving deeper into Redux, it's crucial to ensure that our code is reliable and maintainable. In this lecture, we'll move our testing into an external file using JEST. By the end of this lecture, you will be able to build test cases to validate that your code doesn't break when you make updates.

Why Testing Matters

Testing is an integral part of software development, as it ensures that the application behaves as expected, even as new features are added or changes are made. In this lecture, we'll focus on testing our reducers using JEST.

JEST is a powerful JavaScript testing framework that comes pre-installed with Create React App. We'll use it to write tests that validate our Redux reducers and ensure that they work correctly.

Setting Up JEST

To start, let's create a test file for our Redux store. We'll name this file store.test.js and use it to write our tests.

First, we'll export our reducer and action constants from the store.js file:

// store.js
export const STEP_UP = 'STEP_UP';
export const STEP_DOWN = 'STEP_DOWN';
export default function counter(state = 0, action) {
  switch (action.type) {
    case STEP_UP:
      return state + 1;
    case STEP_DOWN:
      return state - 1;
    default:
      return state;
  }
}

Writing Our First Test

In store.test.js, we will import the counter function, along with the constants STEP_UP and STEP_DOWN. Here's how we can create our first test:

// store.test.js
import counter, { STEP_UP, STEP_DOWN } from './store';

describe('Counter Reducer', () => {
  it('should increment state from 0 to 1', () => {
    const result = counter(0, { type: STEP_UP });
    expect(result).toBe(1);
  });

  it('should decrement state from 1 to 0', () => {
    const result = counter(1, { type: STEP_DOWN });
    expect(result).toBe(0);
  });
});

Breaking Down the Code

  1. Importing the Reducer and Action Types: We import the counter reducer, as well as the STEP_UP and STEP_DOWN action constants, to use them in our test cases.
  2. Writing Tests with describe and it: We use describe to group related test cases and it to define individual test scenarios.
  3. Assertions with expect: The expect function allows us to assert that the value returned by the reducer matches the expected value.

Running the Tests

To run the tests, use the following command in your terminal:

npm run test

This command will run all the tests in your project, including the ones we've just created for the Redux reducer.

If all tests pass, you'll see output indicating that all tests have succeeded. If any test fails, JEST will provide a detailed error message that points out what went wrong, making it easier to debug.

Conclusion

By moving our tests to an external file and using JEST, we are ensuring that our reducers work as expected. Testing gives us confidence that our code will continue to function correctly as our application evolves. In the next lecture, we will continue integrating Redux into our application and expanding our testing suite.

Happy testing!

Video Transcript

00:01Now that we have some data, it's time for us
00:03to actually test out to see if our component,
00:06if our reducer is actually doing what we expect it to do.
00:08To do that, we're gonna use jest
00:10and we'll get familiar a bit with the library,
00:13mainly the basics of testing.
00:15So let's jump right into it and let's make our first test
00:17using Jes.
00:20Now I, I wanna show you just one more thing
00:22before we wrap up this lecture, which is gonna get us very,
00:25very close to actually having a completely
00:27running redux application.
00:29It is almost this easy, there's just one more step involved,
00:32but even before that I wanna make sure that you kind
00:34of get used to the concept of testing out everything you do.
00:39Now, to do that for right now, what I'm,
00:41what I'm gonna do is I'm gonna export our
00:43default to be the counter.
00:45Now this is not gonna be the real actual state
00:48and you, you might notice why,
00:49because we have nothing here
00:51that's actually storing the state itself.
00:53All we have here is different actions.
00:55We also, we'll need some mediator
00:57to actually store the state itself.
00:59So we'll be able to keep a reference to the last
01:03aggregate version of the actual state itself,
01:06which is something that's missing right now.
01:08But now that I'm exporting it, I'm gonna go ahead
01:10and I'm gonna create a new file
01:12and I'm gonna call it store dot test js
01:15because I really wanna create a quick test
01:17and also show you how to work with tests inside
01:20of our environment.
01:21We're gonna be using just here,
01:23which is automatically installed by Create
01:27React app.
01:28So we don't need to install anything else.
01:30And I'm just gonna go ahead
01:31and start creating, oh, I forgot to save it.
01:34So let's go ahead and save it.
01:36And I'm gonna go ahead and start creating our test itself.
01:39Now the test itself is quite easy to create
01:41and what I wanna do is I wanna, first of all,
01:44I wanna basically import our store
01:46and I wanna make sure that I'm exporting all the things I
01:49might need to use, such as our constants that we've created.
01:52So I'm gonna go ahead and export them
01:55and I'm gonna also export the step down
01:59and I'm gonna go ahead and save that.
02:01And inside of our, our test itself, it is quite easy.
02:04All I'm gonna do is I'm gonna import our store itself.
02:07So first of all, I'm gonna import the store itself
02:10and I'm gonna import it from.store,
02:14but I also want to have access for step up and step down.
02:17So let's go ahead and just create that step up,
02:20literally step up and step down.
02:25And I'm just gonna go ahead here and start creating tests.
02:29So I'm gonna start with it
02:32and put here some sort of description.
02:34So for example, step up from zero to one.
02:37And this description is just gonna help me know
02:40where the issues are happening.
02:42The more descriptive I am,
02:43the easier it'll be for me for my testing.
02:45And notice how it's inside of a test file so you don't have
02:48to worry about it actually being included
02:50in your production file.
02:52Now the second parameter that it expects is a function.
02:54So we're gonna send there inside of there an arrow function
02:57and trigger inside of that error function and expect
03:03and tell it what to expect.
03:05So first of all, the first parameter will be
03:07to trigger a function.
03:08So in this case, I'm gonna go ahead
03:10and trigger our store that currently is our default,
03:13which is literally our counter,
03:14but soon it will be something else and I'm gonna go ahead
03:17and run my store, literally sending it the stuff
03:20that I want it to test.
03:22So I'm gonna start off by assuming that our state is equal
03:25to zero and I'm gonna go ahead
03:27and I'm gonna ask for it to, for a type of step up.
03:30And whenever we're stepping up from zero, I wanna make sure
03:33that the value that is coming back is a specific value.
03:38Now the way I do that is once I triggered the expect,
03:41I'm gonna type here a dot
03:42and I'm gonna open up ano, I'm gonna open up uh, two B
03:46and literally define what that value will be.
03:49So in this case, I'm gonna test to see
03:51that the value equals one.
03:53Lemme go ahead and do another test.
03:55So that is a a, a test for expecting from zero to one.
03:59And lemme go ahead and create another one.
04:01And I'm just gonna go ahead here
04:03and create a, from one to zero.
04:06Notice how I'm doing this separately right now
04:08because we actually don't have an actual state.
04:11We only have here our beginner element here that kind
04:14of like describes the way things work.
04:16So step down the actions itself that are being committed
04:19and that action should end up being the value zero.
04:23Now how do we test things out?
04:25So first of all, if I go to my application itself,
04:27you'll notice that inside
04:29of our application itself you'll see
04:31that right now it's actually not doing anything
04:33because now, um, our store might be loaded,
04:36but we're actually not doing anything with that store.
04:39Um, and again, we're getting this warning only
04:41because we're loading in that store in the app itself
04:43and we're actually not doing anything with it.
04:46But let's go ahead and test our store out.
04:47And to do that, you'll see inside of your package
04:50that we have built in a script
04:52that is called test to be able to use it.
04:55I'm literally just gonna go into my terminal
04:57and I'm gonna let my server continue running in a
05:00separate tab.
05:01I'm just gonna go ahead and type your NPM Run test.
05:06And when I run NPN Run test, it's gonna start that, that,
05:10uh, tester for us on its own and it's gonna test
05:14and see, okay, it had two tests, uh, suites.
05:18There are a total of three tests.
05:19They all have passed without any problem. Beautiful.
05:22So we know that for now we don't have a issue
05:25with our core logic.
05:27Let's go ahead and just show you
05:28what happens if you type your something
05:30that is not expected.
05:31So for example, if we expected the value to be two,
05:34but it actually went down to zero, instead, if we go ahead
05:38and run that test one more time,
05:39we'll see it automatically ran again
05:41and it's telling us that we have a failure
05:43and where is that failure expected value
05:46to be using da da da equals two.
05:49So if we take a, a deeper look at it, we'll see
05:51that the value step up from one to zero, step up,
05:55step down, that would probably be a better description from
05:58one to zero is actually not the value
06:01that we'd expected it to be.
06:02And that is exactly how we would add just testing, just
06:07testing, whatever you want to call it.
06:09I'm so bad at naming conventions
06:10and how to pronounce different names
06:12and I apologies for that 'cause I'm sure many
06:13of you are very, very strict on how you name things.
06:17I used to argue with my sister all the time about
06:19how do you pronounce NULL?
06:22In my mind it's null.
06:24Um, but I think she said it's null
06:26and I, I just can't,
06:27sometimes I just like invent words when I read them.
06:29Um, so I apologize for that. But there you go.
06:32So that's how we added testing.
06:33We we're not obviously done yet,
06:35but now we have the basic core structure.
06:37And I want to go just repeat through everything we did.
06:39'cause we've done quite a lot.
06:41We started off by importing our store even though we're not
06:44doing anything with it yet.
06:46And in our store itself, we made sure
06:47that we're exporting our step up and step down constant.
06:51And we're also exporting our currently only function,
06:54our only action, uh, trigger.
06:57And we're gonna take a look at what it's gonna be a reducer
06:59and we're gonna take a look at how do we connect it
07:01with the reducer of the library in the next lecture.
07:04Once we've exported both of them,
07:05we've created a new file called store do test.
07:10And inside of that suite of tests, by the way,
07:12you'll notice also that our app itself also has tests.
07:15So we could literally create multiple suites
07:17that test different parameters, making sure that
07:20whenever we make changes we could test them
07:22out and see that they're working.
07:24Once we created our tests itself, we've imported
07:27inside our store
07:28and also the different constants
07:31that we're going to be using.
07:33We then created two tests, one with,
07:35with the word descriptive, starting with it,
07:37giving it a description, second parameter
07:40as a arrow function
07:41where we called the expect function triggered inside of it,
07:45our actual thing that we want to trigger.
07:47And then checked to see that it equals to one.
07:51Same applied, we did the other way around.
07:53So we tested our action to step up
07:55and we tested our action to step down.
08:00In this lecture, we set up our testing environment using
08:04just, and in the following lecture,
08:05we'll continue down the path of integrating Redux.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!