The Jest Handbook

Take your JavaScript testing skills to the next level with the Advanced Jest Guide.

  1. Gain a deep understanding of the options available to the Jest CLI and how to use different views to write and debug tests more effectively.
  2. Leverage spying, stubbing and module import interception functionality in tests and create mock JavaScript object instances, stub ES6 classes and mock out global objects.
  3. Use and contrast 2 approaches to testing backend applications with Jest as well as illustrate synchronous and asynchronous testing patterns.
  4. Employ advanced Jest partial matchers to write tests with high specificity and reduce toil when updating application code.
  5. Leverage Jest’s built-in coverage tool to set minimum coverage thresholds, find parts of the code that aren’t tested and how to disable it in specific cases.

Introduction

Jest is a batteries-included framework with best-practices, a test runner, CLI, assertion library, stubbing library, module mocking library and coverage built-in.

As of early 2020, it’s the most downloaded JavaScript testing library.

2 year trends (November 2019) for Jest vs Mocha, AVA, Jasmine, QUnit and Chai

The reasons it’s recently become a de-facto standard in the JavaScript ecosystem are as follows (as per jestjs.io):

  • zero config: it should take little to no configuration to get Jest working in your JavaScript project
  • snapshots: a simple system to keep track of changes within a system
  • isolated: tests are isolated from each other, this means fewer issues with tests modifying shared resources like global objects or module mocks.
  • familiar/“great” API: Jest supports helper methods to write your tests with, including describe, it, test. Tests are Promise-aware (and therefore support async functions as tests), for example if a Promise returned from a test rejects or an async test throws, the test will fail with the corresponding error.

Jest is a very powerful framework for those who know how to wield it.

Using the Jest Handbook

The Jest Handbook is designed as a reference on how to use Jest in different use cases.

Beyond the Jest documentation and some titles on “Testing React/Vue.js with Jest”, there are few pieces of work similar to the Jest Handbook.

It’s recommended that you give The Jest Handbook mainly to give you an overview of what’s possible with Jest. This solves the “I didn’t even think of doing that” problem.

One read will, however give you deep understanding or muscle memory as to how to solve everyday JavaScript testing challenges with Jest. That’s where the “Handbook” format comes in. Each Section uses a descriptive title to enable quick reference.

If you come across a JavaScript/Jest testing issue, you can look in the relevant section for ideas and approaches to solve it.

Examples

All the examples in the Jest Handbook are available at github.com/HugoDF/jest-handbook-examples.

Jump to table of contents