React Testing With Jest And Enzyme

Tags: React

Improve your React, Redux, Hooks and Context Code with Test Driven Development

Last updated 2022-01-10 | 4.5

- Write unit
- integration and functional tests for React
- Hooks
- Contex and Redux using Jest and Enzyme.
- Know the tradeoffs for different testing approaches and when to choose which approach.
- Plan your React app more effectively via Test Driven Development.

What you'll learn

Write unit
integration and functional tests for React
Hooks
Contex and Redux using Jest and Enzyme.
Know the tradeoffs for different testing approaches and when to choose which approach.
Plan your React app more effectively via Test Driven Development.
Mock methods and modules to keep your tests isolated.

* Requirements

* Basic Familiarity with JavaScript and React

Description

Take your React code to the next level by learning Test Driven Development (TDD) with Jest and Enzyme! Jest is a powerful, flexible testing framework, and Enzyme provides tools to test React and Redux applications. In this course, you will learn to test:

  • React hooks, including useEffect, useState and useReducer

  • Asynchronous functions using Axios

  • Redux action creators and reducers

  • Complex Redux action creators that use Redux Thunk

  • React context, including context with embedded state

You will also learn how to make the most of Jest capabilities, including

  • Mocking and restoring individual properties of modules

  • Mocking entire modules

  • Controlling which tests run using, .skip, and .only

  • Running test suites multiple times with different data, using .each

Please Note: This course does not cover Jest snapshots, as they are not compatible with the TDD mode of testing.

Why Learn to Test?

Test Driven Development will help you write better organized code that’s easier to maintain, which will save you time in the long run. Your tests provide value to your software development team, since others know they can rely on your code. Employers want developers with testing skills!

Unit tests and Functional Tests

This course focuses on unit tests. Unit tests are tightly coupled to specific areas of code, which leads to easy diagnosis of failures, and a great match for Test-Driven Development. The course also teaches functional tests, which are modeled on user flows (and resulting behavior from the user perspective). Functional tests are not connected to code, which makes them more difficult to diagnose, but more resilient to code refactors.

Learn the Reasons behind the Syntax

This course discusses tradeoffs when considering different approaches to testing, leaving you confident in the testing choices you make. Furthermore, you will deepen your understanding of React, Redux and Context as we dig into how and why we test each aspect.

Practice your New Skills

You will also have opportunities to practice what you’ve learned. There are occasional “quizzes” while we’re building the course projects, where you can apply what you learned, and then watch a video to see the solution. There are also two sets of challenges to extend the course projects, with solutions on GitHub.

Who this course is for:

  • Web developers interested in improving their React apps with testing

Course content

17 sections • 206 lectures

Welcome and Introduction Preview 03:22

TDD: What and Why Preview 03:44

TDD stands for Test-Driven Development, which means writing tests before you write functioning code. This approach leads to code that is better organized, and also makes it possible to re-test every aspect of your code without any extra effort whenever you make code changes.

create-react-app Preview 03:05

Next lecture is optional Preview 00:13

OPTIONAL: Setting up Jest without create-react-app Preview 07:46

Demo of Jest Watch Mode Preview 04:00

More about Jest Watch Mode, Test Files and Tests Preview 05:40

Enzyme vs. React Testing Library Preview 06:52

IMPORTANT NOTE: enzyme-adapter-react-17 not yet available Preview 00:28

Enzyme Introduction and Setup Preview 04:26

Using Enzyme in a Test Preview 03:34

Jest Assertions Preview 04:00

Types of Tests Preview 03:00

Testing Tradeoffs Preview 06:58

Why There is no Snapshot Testing in this Course Preview 03:10

Course Repository on GitHub Preview 00:06

Demo and Start Click Counter App Preview 02:52

Set up Enzyme and Write Tests Preview 05:55

data-test Attributes Preview 06:01

First data-test Attribute Test Preview 04:02

OPTIONAL: Removing data-test Attributes for Production Preview 08:44

More data-test Attribute Tests Preview 04:09

DRY Refactor Preview 06:50

Strategies and Design Decisions Preview 05:11

Test Initial Counter Display Preview 06:40

Test Button Click Preview 05:40

Acceptance Testing and Review Preview 03:05

Click Counter Challenges Preview 02:19

Click Counter Challenge Descriptions Preview 00:17

Jotto App Demo Preview 01:49

Jotto App Planning Preview 04:17

Jotto App Plan of Attack Preview 03:36

Set up Jotto App and Congrats Component Preview 07:22

Congrats Component Test Setup Preview 07:25

Congrats Tests and the .text() Method Preview 05:43

Code the Congrats Component Preview 03:17

Notes about PropTypes Testing Preview 00:23

OPTIONAL PropTypes Testing Preview 10:38

OPTIONAL Code PropTypes and Update Tests Preview 03:41

OPTIONAL Default Props in setup() Preview 02:29

Move Enzyme Configuration to setupTests.js Preview 02:14

Summary of Abstractions Preview 02:08

Playing the Long Game Preview 02:18

Set up GuessedWords Component and Tests Preview 04:06

GuessedWords PropTypes and setup() Preview 07:38

Use `describe` for Context Preview 01:51

Write Tests and beforeEach for "no words guessed" Preview 04:56

Code for "no words guessed" Preview 03:12

Tests for "nonzero guessed words" Preview 06:22

Code for "nonzero guessed words" Preview 05:15

Manual Acceptance Testing Preview 03:06

Bootstrap Styling Preview 04:10

Code Quiz! Basic Input Component Preview 02:30

Input Component Props Preview 04:44

OPTIONAL: Introduction to React Hooks Preview 04:28

Mocking React Methods Preview 04:02

Prepare Input Component for useState Tests Preview 05:11

State-Controlled Field Tests and Code Preview 07:27

Common Questions about Mocking React Methods Preview 06:21

Clearing State-Controlled Field on Submit Preview 08:24

Testing Input Render for `success` as `true` or `false` Preview 08:29

`getLetterMatchCount` Helper Function Preview 08:36

Introduction to Functional Tests, and update App Component Preview 04:49

Set up Functional Tests Preview 08:43

First Functional Test: Submit Guess with No Previous Guesses Preview 07:23

Code Quiz! Functional Tests for "Some Words Guessed" and "Correct Guess" Preview 06:28

Jest methods: .only() .skip() and .todo() Preview 05:13

`secretWord` Plan Preview 01:49

`moxios`: Why and How Preview 03:32

Set up `getSecretWord` Action and Tests Preview 04:10

Testing Async Actions Preview 02:37

Write `getSecretWord` Action Preview 05:00

Asynchronous Action and Moxios Summary Preview 02:02

Mocking Modules with Jest Preview 05:58

Using useEffect to Get Secret Word on App Mount Preview 04:43

Mocking the actions Module Preview 05:30

Testing that useEffect is Called on App Mount, Not Called on App Update Preview 05:52

Write useEffect Code to Pass Tests Preview 02:01

Choice point: Redux or React Context? Preview 04:43

Redux Planning Preview 04:56

Set up Redux Preview 05:36

Success State Planning Preview 01:00

`CORRECT_GUESS` Action Creator Preview 07:05

`successReducer` Test Preview 06:52

OPTIONAL: Introduction to the `useSelector` Hook Preview 02:31

Testing Redux State for Input Component Preview 03:48

Choices for Testing `useSelector` Preview 05:45

`storeFactory` Test Helper Preview 03:54

Updating Setup Function to use Store Factory Preview 04:05

Summary: Intro to Redux Testing Preview 03:28

Other Pieces of State Preview 01:00

`guessedWords` Planning Preview 03:13

Redux Thunk: What and Why Preview 01:24

Installing Redux Thunk Preview 03:35

Thunk Test Planning Preview 02:32

Set up `guessWords` Tests Preview 08:49

First Thunk Test Preview 05:47

Add `secretWordReducer` Preview 02:08

Remaining Tests Preview 07:13

Write Reducer and Action Creator Preview 06:52

Plan for Functional Tests Preview 05:10

Update Components Preview 04:01

Thunk Testing Summary Preview 01:37

`secretWord` Plan Preview 01:52

Update moxios Tests for Redux Preview 02:45

Write Action Creator and Reducer Preview 04:04

Update App to use secretWord state: mock response and Jest settings Preview 04:48

Manual Acceptance Testing Preview 02:05

Jotto Challenges for Redux Preview 05:43

Introduction to React Context Preview 02:15

Jotto Data Flow with Context Preview 05:04

Jotto Context Plan of Attack Preview 02:13

Update `getSecretWord` Tests for Context Implementation Preview 05:23

Update `getSecretWord` Code for Context Implementation Preview 01:13

Intro to `useReducer` Hook Preview 06:08

Refactor App State with `useReducer` Hook Preview 08:39

Loading Spinner Planning and `describe.each()` Preview 06:15

Test and Code Loading Spinner Preview 12:03

Context Implementations in this Course Preview 03:30

Language Context Planning Preview 04:31

Strings File Setup Preview 05:38

Strings Tests Preview 04:35

Code getStringByLanguage Preview 02:09

Strings Warnings Preview 07:13

LanguagePicker Component: Planning and Setup Preview 04:47

LanguagePicker Tests: Render and PropTypes Preview 04:43

LanguagePicker Icons Preview 04:28

LanguagePicker Icon Click Preview 02:07

languageContext File Preview 06:25

Context Test Options Preview 06:23

Congrats Language Context Test Setup Preview 06:15

Congrats Language Context Tests Preview 03:34

Congrats Language Context Code Preview 02:30

Code Quiz! Input Language Context Preview 07:38

Fix App Test Language Warnings Preview 02:40

GuessedWords Language Context Tests: Mocking useContext Preview 08:28

GuessedWords Language Context Code Preview 03:09

Manual Acceptance Testing Preview 01:16

Context with Embedded State Preview 03:20

Code Success Context Preview 06:07

Test Success Context Preview 08:21

Code Quiz! guessedWords Context Preview 03:29

Set up Congrats and App Components for Success Context Testing Preview 03:56

Test and Code Congrats Success Context Preview 06:20

Set up GuessedWords Component for Context Testing Preview 03:53

Test and Code GuessedWords Context in GuessedWords Component Preview 09:30

Review of Context with Embedded State Preview 04:12

Update Functional Test Setup for Context Implementation Preview 06:38

Test and Code Input Consuming Success Context Preview 05:23

Test and Code Input Setting Success Context Preview 02:27

Test and Code Input Setting GuessedWords Context Preview 04:54

Fix GuessedWords Provider Error in Input.test.js Preview 01:47

Manual Acceptance Testing Preview 02:13

Jotto Challenges for Hooks and Context Preview 00:16

About these legacy lectures Preview 00:12

Review of Jotto Redux State Plan Preview 04:56

Set up Redux Preview 05:46

Success State Planning Preview 01:00

`CORRECT_GUESS` action creator Preview 07:33

`successReducer` Tests Preview 07:18

`successReducer` Code Preview 03:13

Tradeoffs to Unit Testing Action Creators / Reducers Preview 01:51

Set Up Connected Component Tests Preview 04:32

Input Component Test Organization Preview 02:27

Start setup Function for Input Component Preview 03:06

`storeFactory` Test Helper Preview 02:46

Enzyme `.dive()` method Preview 04:05

Summary of Connected Component Setup and Choices Preview 04:21

Input Tests and Code for success=false Preview 07:09

Quiz! Write Input Tests and Code for success=true Preview 02:44

Simple Redux Summary Preview 01:03

Other Pieces of State Preview 01:22

`guessedWords` Planning Preview 03:13

Redux Thunk: What and Why Preview 01:24

Installing Redux Thunk Preview 04:30

Thunk Test Planning Preview 02:32

Set up `guessWords` Tests Preview 09:05

First Thunk Test Preview 05:54

Add `secretWordReducer` Preview 02:05

Remaining Tests Preview 07:53

Write Reducer and Action Creator Preview 07:14

Thunk Integration Testing Summary Preview 00:37

`secretWord` Plan Preview 02:21

`moxios`: Why and How Preview 03:32

Set up `secretWord` Action Creator and Tests Preview 05:26

Testing Asynchronous Action Creators Preview 03:31

Write `getSecretWord` tests Preview 05:09

Write `secretWord` Action Creator and Reducer Preview 04:51

Summary: Asynchronous Action Creators and `moxios` Preview 02:45

Testing Props and Action Creator Calls Preview 01:26

Testing Redux Props Preview 01:59

Input Redux Props Preview 07:38

Quiz! App Redux Props Preview 06:40

OPTIONAL: Configure Jest Setup for non-create-react-app Preview 01:52

Approach to Testing Action Creator Calls Preview 02:50

`getSecretWord` call on App Mount Preview 10:41

Quiz! Tests for `guessWord` on Input Submit Click Preview 05:52

Write test for `guessWord` Argument Preview 09:35

Write `guessWord` Call with Argument Preview 06:26

Clear Input Box on Submit Preview 02:37

Demo of Completed App Preview 02:08

Jotto Challenges Preview 05:52

Jotto Challenge Descriptions Preview 01:18