The Art of Unit Testing, Third Edition
with examples in JavaScript
Roy Osherove
  • MEAP began December 2019
  • Publication in Spring 2021 (estimated)
  • ISBN 9781617297489
  • 325 pages (estimated)
  • printed in black & white
free previous edition eBook included
An eBook copy of the previous edition of this book is included at no additional cost. It will be automatically added to your Manning Bookshelf within 24 hours of purchase.

A book that is seeing a third release must be awesome and this book certainly is.

Sander Zegveld
The Art of Unit Testing, Third Edition updates an international bestseller to reflect modern development tools and practices. This expanded edition teaches you to write unit tests for async and observable code, as well as methods of faking functions, modules and refactorings. You'll explore test patterns and organization, working with legacy code, and even "untestable" code. The many tool-agnostic examples are presented in JavaScript, and are carefully designed so that they apply to code written in any language.

About the Technology

Unit testing is a universally-adopted practice on dev teams of all sizes. Great developers will tell you that testing is as much a state of mind as a collection of tools and practices. The Art of Unit Testing, Third Edition takes you below the surface and understand how unit testing can transform the way you deliver and maintain software. Now with examples in JavaScript, this new edition has been updated to reflect the characteristics of a modern codebase, including async and modularized, distributed services. The practices and patterns you’ll learn can also be easily applied to other languages, including C#, Java, and Python.

About the book

The Art of Unit Testing, Third Edition guides you step by step from your first simple unit tests to building complete test sets that are maintainable, readable, and trustworthy. Now with examples using JavaScript and Node, this expanded third edition teaches you to write unit tests for async and observable code, as well as methods of faking functions, modules and refactorings. You’ll get to grips with complex subjects such as mocks and stubs, explore how to handle tricky legacy codebases, and save yourself time with tests that function for both frontend and backend applications. The Art of Unit Testing, Third Edition is an essential guide for any unit tester who wants to feel confident writing code that’s easier to create, maintain, and adapt.
Table of Contents detailed table of contents

Part 1: Getting started

1 The basics of unit testing

1.1 The first step

1.2 Defining unit testing, step by step

1.3 Entry Points & Exit Points

1.4 Exit Point Types

1.5 Different Exit Points, Different Techniques

1.6 A Test from Scratch

1.7 Characteristics of a good unit test

1.7.1 So, what’s a “good” unit test?

1.7.2 A simple checklist

1.8 Integration tests

1.8.1 Drawbacks of integration tests compared to automated unit tests

1.9 Finalizing our definition

1.10 What About Regression Tests?

1.11 Test-driven development

1.12 Three core skills needed for successful TDD

1.13 Summary

2 A first unit test

2.1 About Jest

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 & 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

2.5.1 Testing the test

2.5.2 U.S.E Naming

2.5.3 String comparisons and maintainability

2.5.4 Using describe()

2.5.5 Structure can imply context

2.5.6 The ‘It’ function

2.5.7 Two Jest Flavors

2.5.8 Refactoring the Production Code

2.6 Trying the beforeEach() route

2.6.1 beforeEach() and scroll fatigue

2.7 Trying the factory method route

2.7.1 Replacing beforeEach() completely with factory methods.

2.8 Going Full Circle to test()

2.9 Refactoring to parameterized tests

2.10 Checking for expected thrown errors

2.11 Setting Test Categories

2.12 Summary

Part 2: Core techniques

3 Breaking dependencies with stubs

3.1 Types of Dependencies

3.2 Reasons to use stubs

3.3 Generally accepted design approaches to stubbing

3.3.1 Stubbing out time with parameter injection

3.3.2 Dependencies, Injections and Control

3.4 Functional Injection Techniques

3.4.1 Injecting a function

3.4.2 Using Factory Functions

3.4.3 Moving Towards Objects with Constructor Functions

3.5 Object Oriented Injection Techniques

3.5.1 Constructor Injection

3.5.2 Injecting an object instead of a function

3.5.3 Extracting a common interface.

3.6 Summary

4 Interaction testing using mock objects

4.1 Interaction Testing, Mocks and Stubs

4.2 Depending on a logger

4.3 Standard Style: Introduce Parameter Refactoring

4.4. The Importance of Mock and Stub Differentiation

4.5 Modular Style Mocks

4.5.1 Example of production code

4.5.2 Refactoring the production code in a modular injection style

4.5.3 A test example with modular style injection

4.6 Mocks in a Functional Style

4.6.1 Working with a currying Style

4.6.2 Working with higher order functions and not currying

4.7 Mocks in an Object-Oriented Style

4.7.1 Refactoring Production Code for Injection

4.7.2 Refactoring Production with Interface Injection

4.8 Dealing with complicated interfaces

4.8.1 Example of a Complicated Interface

4.8.2 Writing tests with complicated interfaces

4.8.3 Downsides of using Complicated Interfaces directly

4.8.4 The Interface Segregation Principle

4.9 Partial Mocks

4.9.1 A functional partial mock example

4.9.2 An object oriented partial mock example

4.10 What About Spies?

4.11 Summary

5 Isolation (mocking) frameworks

5.1 Defining Isolation Frameworks

5.2 A quick look at the Isolation frameworks landscape

5.3 Choosing a loose vs typed “flavor”

5.4 Faking Modules Dynamically

5.4.1 Some things to notice about jest’s API:

5.4.2 Consider abstracting away direct dependencies

5.5 Functional Mocks and Stubs - Dynamically

5.6 Object Oriented Dynamic Mocks and Stubs

5.6.1 Switching to a type-friendly Isolation Framework

5.7 Stubbing Behavior Dynamically

5.7.1 An Object-Oriented example with a mock and a stub

5.7.2 Stubs and Mocks with Substitute.JS

5.8 Advantages and traps of isolation frameworks

5.8.1 You don’t need mock objects most of the time

5.8.2 Unreadable test code

5.8.3 Verifying the wrong things

5.8.4 Having more than one mock per test

5.8.5 Over specifying the tests

5.9 Summary

6 Digging deeper into isolation frameworks

Part 3: The test code

7 Test hierarchies and organization

8 The pillars of good unit tests

Part 4: Design and process

9 Integrating unit testing into the organization

10 Working with legacy code

11 Design and testability

What's inside

  • Creating readable, maintainable, and trustworthy tests
  • Working with fakes, stubs, mock objects, and isolation (mocking) frameworks
  • Simple dependency injection techniques
  • Refactoring legacy code

About the reader

The examples in the book use JavaScript and Node and cover the full dev stack.

About the author

Roy Osherove is an internationally-recognized expert in unit testing and agile software methodology. Still an active coder, he consults and trains teams worldwide on the gentle art of unit testing and test-driven development. He is also the author of Elastic Leadership (Manning 2016). Roy’s blog is at

placing your order...

Don't refresh or navigate away from the page.
Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
print book $29.99 $49.99 pBook + eBook + liveBook
includes previous edition eBook
Additional shipping charges may apply
The Art of Unit Testing, Third Edition (print book) added to cart
continue shopping
go to cart

eBook $31.99 $39.99 3 formats + liveBook
includes previous edition eBook
The Art of Unit Testing, Third Edition (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.
customers also reading

This book 1-hop 2-hops 3-hops

FREE domestic shipping on three or more pBooks