Reader small image

You're reading from  Hands-On Full Stack Development with Spring Boot 2.0 and React

Product typeBook
Published inJun 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781789138085
Edition1st Edition
Languages
Right arrow
Author (1)
Juha Hinkula
Juha Hinkula
author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula

Right arrow

Chapter 12. Testing Your Frontend

This chapter explains the basics of testing React apps. We will give an overview of using Jest, which is a JavaScript test library developed by Facebook. We will also cover Enzyme, which is a testing utility for React, developed by Airbnb. We will look at how you can create new test suites and tests. We will also cover how to run the test and discover the results of the test.

In this chapter, we will look at the following:

  • The basics of Jest
  • How to create new test suites and tests
  • The basics of the Enzyme test utility
  • How to install Enzyme
  • How to create tests with Enzyme

Technical requirements


The Spring Boot application that we created in Chapter 4, Securing and Testing Your Backend, is needed (GitHub: https://github.com/PacktPublishing/Hands-On-Full-Stack-Development-with-Spring-Boot-2.0-and-React/tree/master/Chapter%204).

We also need the React app that we used in the previous chapter (GitHub: https://github.com/PacktPublishing/Hands-On-Full-Stack-Development-with-Spring-Boot-2.0-and-React/tree/master/Chapter%2011).

Using Jest

Jest is a test library for JavaScript and it is developed by Facebook (https://facebook.github.io/jest/en/). Jest is widely used with React and it provides lots of useful features for testing. You can create a snapshot test, where you can take snapshots from React trees and investigate how states are changing. Jest also has mock functionalities that you can use to test, for example, your asynchronous REST API calls. Jest also provides functions that are needed for the assertions in your test cases.

We will first see how you can create...

Summary


In this chapter, we gave a basic overview of how to test React apps. Jest is a testing library developed by Facebook, and it is already available in our frontend because we created our app with create-react-app. We created a couple of tests with Jest, and ran those tests to see how you can check the results of tests. We installed Enzyme, which is a test utility for React. With Enzyme, you can easily test your React component rendering and events. In the next chapter, we will secure our application, and add the login functionality to the frontend.

Questions


  1. What is Jest?
  2. How should you create test cases using Jest?
  3. How should you create a snapshot test using Jest?
  4. What is Enzyme?
  5. How should you install Enzyme?
  6. How should you test rendering with Enzyme?
  7. How should you test events with Enzyme?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Full Stack Development with Spring Boot 2.0 and React
Published in: Jun 2018Publisher: PacktISBN-13: 9781789138085
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula