2 A first unit test

 

This chapter covers

  • Writing your first test with Jest
  • Test structure and naming conventions
  • Working with the assertion library
  • Refactoring tests and reducing repetitive code

When I first started writing unit tests with a real unit testing framework, there was little documentation, and the frameworks I worked with didn’t have proper examples. (I was mostly coding in VB 5 and 6 at the time.) It was a challenge learning to work with them, and I started out writing rather poor tests. Fortunately, times have changed. In JavaScript, and in practically any language out there, there’s a wide range of choices and plenty of documentation and support from the community for trying out these bundles of helpfulness.

In the previous chapter, we wrote a very simple home-grown test framework. In this chapter, we’ll take a look at Jest, which will be our framework of choice for this book.

2.1 Introducing Jest

Jest is an open source test framework created by Facebook. It’s easy to use, easy to remember, and has lots of great features. Jest was originally created for testing frontend React components in JavaScript. These days it’s widely used in many parts of the industry for both backend and frontend project testing. It supports two major flavors of test syntax (one that uses the word test and another that’s based on the Jasmin syntax, a framework that has inspired many of Jest’s features). We’ll try both of them to see which one we like better.

2.1.1 Preparing our environment

2.1.2 Preparing our working folder

2.1.3 Installing Jest

2.1.4 Creating a test file

2.1.5 Executing Jest

2.2 The library, the assert, the runner, and the reporter

2.3 What unit testing frameworks offer

2.3.1 The xUnit frameworks

2.3.2 xUnit, TAP, and Jest structures

2.4 Introducing the Password Verifier project

2.5 The first Jest test for verifyPassword

sitemap