Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Set Up Tests that Render a React Component with Jest and Babel


    Testing a React component takes a little more configuration than a test for standard JavaScript. In this lesson, we’ll install and configure react-testing-library and jest-dom matchers, create a test that renders our App component and configure it all to work properly with Babel 7 and dynamic import syntax.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Now that we know our test script is working, we need to be able to create a valuable test. Since this is a test file for our app component, we need to be able to test React components. I'm going to go into the terminal, and I'm going to do a couple more installs.

    I'm going to run npm i -d react-testing-library and jest-dom. With those installed, let's update our test. Back up in the editor, I'm going to add a couple of imports. I'm going to import react from react, and I'm also going to import render from react-testing-library, and I'm going to import jest-dom/extend-expect.

    This is going to add some extra matchers that are specific to testing DOM. I'm also going to import react-testing-library/clean-up-after-each. This is going to run some code after each test that'll clean up the virtual DOM to make sure that we don't have any state hanging around from one test to the next.

    Then we want to test our app component, so we're going to import app from app. Now, I'm going to go down to my test. I'm going to update my name here, and we're going to say that it renders without error. I'm going to replace the body of my test, and I'm going to call render. I'm going to pass that app.

    I'll save that, and then I'm going to run npm test. Our test is going to fail. If I expand the terminal here, we can see that it's having a problem with the JSX in our test. There's a lot of descriptive information here, but it's basically telling us that we don't have standard JavaScript and we need to transform our code in order to test it.

    Let's fix this. In our webpack config, we're using Babel to transform our application code. We want to do the same thing for our test code. I'm going to come into this webpack.config.base, and I'll get the terminal out of the way. We'll see that we have all of this configuration for Babel. 

    I want to take this, and I want to move it into a centralized location. I'm going to add a new file to the root of my project. I'll just call that .babelrc. In my webpack config, I'm going to find all the options for Babel. I'm going to leave the rule here, with test, the Babel loader, what to exclude. I'm going to take the options object, and I'm going to cut that.

    I'm going to remove the options key there. We can save that config. I'm going to go into my Babel RC, and I'm going to paste that object in. Because this is treated as JSON, I have to update the strings to use double quotes. With that cleaned up a little bit, I'm going to go ahead and save that.

    I'm going to open a terminal, and I'm going to do an npm run build. I just want to make sure that by moving that configuration out, I haven't changed the build at all. All right, everything still successfully built. By default, the Babel loader is going to find this Babel RC and use those options.

    Now that those settings are in there, let's go back to our test, and in the terminal, I'm going to do an npm run test. My test is going to fail again. Let's expand the terminal and see what's happening. If I scroll up, we'll see that we're getting a new error, so this is progress. Now, it has an issue with babel-core.

    Because we're using Babel 7, some of the internals are still looking for this babel-core module. We can fix that by doing another install. I'm going to clear out the terminal, and I'm going to install two more modules. I'll do an npm i -d. We're going to install babel-jest, and we're going to install babel-core.

    I'm going to specify a tag here. I'm going to say @bridge. There's a bridge version of babel-core that'll resolve that discrepancy between babel-core 6 and babel-core from Babel 7. We'll install those, and with those installed, let's clear that out and npm test again. Our tests are going to fail again, but we're getting a different error.

    Again, we're making progress. We'll see here that it has an issue with this dynamic import syntax. The problem here is that while we are using Babel to do our transform, Jest is running in a Node process, and Node doesn't support this syntax, even with that Babel transform.

    We're going to need to install another module to take care of this. I'm going to clear this out. I'm going to do an npm i -d, and this is going to be babel-plugin-dynamic-import-node. This is going to be the Node version of Babel support for that dynamic import syntax.

    We'll install that. With that installed, we need to update our Babel RC file. I'll move that out of the way, and open up Babel RC. Then down here at the bottom, I'm going to add a new key, because we only want to use this new plugin when we're in our tests.

    We can add a top level ENV key, and then we can specify environment-specific settings. Jest is going to set the environment variable to test. Then within test, we're going to have plugins, and that'll be an array.

    We're going to pass it in our dynamic import Node plugin, and we'll save this. Then we're going to run npm test again, and we finally have a passing test.