Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 962 of the free egghead.io lessons, plus get React content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Reloading React component tests using webpack-dev-server

2:16 React lesson by

Unit testing a React component using Webpack and Jasmine, can involve slow and repetitive tasks when confirming that tests are passing when your code changes. For example, manually running commands instructing webpack to compile code plus refreshing the browser. This lesson demonstrates how to speed up your development time by automating these tasks using webpack-dev-server.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Unit testing a React component using Webpack and Jasmine, can involve slow and repetitive tasks when confirming that tests are passing when your code changes. For example, manually running commands instructing webpack to compile code plus refreshing the browser. This lesson demonstrates how to speed up your development time by automating these tasks using webpack-dev-server.

In this lesson we're going to start with the React component that we're going to be testing using Jasmine. We're going to compile the code using Webpack and we're going to actually demonstrate...If we make any changes to the spec or to the code, we then have to go back into Webpack, recompile the code, and once the code is actually recompiled, we have to go back to the browser, refresh the browser, and determine whether our test passes or fails.

Obviously, this is a cumbersome process, quite slow, but one that we can overcome and I'll demonstrate that in this lesson by using the Webpack dev server. Let's install it using MPM and then once it's installed we can then run it from within our node modules.bin directory.

We run it from there because we installed it locally and not globally. This will run under local host port 8080/webpack-dev-server. We test at the end because that's what we defined in our public path within the Webpack configuration.

We're expecting Webpack dev server to reload once we made any code changes. If we go into our spec and change run the jewels to run the jewel, we would expect the test to fail, which it has.

If you look here, the artist named Run the Jewel was expected to match the artist named Run the Jewels. Now if we go back to our spec and revert that change and go back to Run the Jewels, we should expect, automatically, for Webpack to reload the tests and the tests should pass.

Now, it's quite a lot of typing, just to get Webpack dev server up and running. We can make it a little less work for our fingers and make use of the MPM run command. What you can do is essentially provide a command to your script object and then using MPM run, run that command.

You can also supply arguments to our command, but for us we simply need Webpack dev server to be run. MPM run adds the node_modules.bin directory to our paths and we don't need to worry about that prefix.

Let's create a package.json file and add that to the root of our project. Create a script object and give it one property of test. The test value will be Webpack dev server, the command that we're expecting to be run. MPM actually provides us with a shortcut option so that we can simply run MPM test, which, as expected, runs our Webpack dev server.

If we go back to our browser, we can see that the Webpack dev server is running our test.



HEY, QUICK QUESTION!
Joel's Head
Why are we asking?