Category Archives: Testing

Mocks: Testing Reactjs Components

Have recently been learning how to test Reactjs components via unit testing using jest and @testing-library/react which come prebundled with any new Reactjs app created with CRA (Create-react-app CLI).

Mocking functions with jest

Many times components have eventHandlers (onclick, onchange, onblur etc) passed down to them via props and with tests these event handlers are usually mocked out.

Mocking ajax/fetch/axios calls in tests.

Since almost every app has a remote request that they make, learning to mock async calls seemed very important. For this, a package called MSW ( model service worker) can be used to intercept ajax calls to external remote sources to keep them local so that tests are less flaky and quicker.

My package.json for the example that I’m running:

Notice the inclusion of jest-enviroment-jsdom-sixteen. Further more, it’s use here:

I added this to suppress some errors about not using act() during async calls. It can be further explained here: https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning

To use MSW we have to setup a server with endpoints that it will intercept. Might want to break your endpoints in to a separate file but for this example I kept them all in the same server.js under the mock directory.

What above will do is intercept any call to “https://jsonplaceholder.typicode.com/todos/1”  and instead return a json object of

Also, we want our server to setup and tear down for each and every test and for that we create a setupTests.js within the /src folder:

The testing library should automatically see this setupTests.js file and run it during testing.

So the actual component we are going to test is called Todo.js

The ajax call is actually within the useEffect that gets called once on the initial rendering of the component.

Here is our testing file for Todo component. Todo.test.js

The actual test we are interested in is “msw intercepts the axios on effect” as this is the one that will check if the ajax call was intercept and replaced with our mocked return  data that we get from MSW.

First we render the component which will trigger the intitial useEffect call which then triggers the ajax call to ultimately update our title state.

The line below enables us to “wait”(async) for the ajax call to complete and for the state to be updated before we check to see if the mocked data was in fact returned via MSW.

Our tests pass as MSW does infact return the mocked title of “the mocked title” which is what our test is looking for.

Mocking 3rd party libraries or modules

We can use jest to mock a library so our tests are easier to write without going in to the complexity of incorporating the libraries that maybe used. For example:

We mocked the useGetLocation function that is from the ‘react-get-location’ library and made it so we return a custom state hook to be used in our tests instead of the normal non-mocked version of useGetLocation

(video lesson 184 )