Apress Media, LLC., 2023. — 267 p. — ISBN-13: 978-1-4842-9247-1.
Learn to write better-automated tests that will dramatically increase your productivity and have fun while doing so. This book is a build-your-own adventure designed for individual reading and collaborative workshops.
You will build a xUnit automated test framework using JavaScript: initially a clone of Jest, but adding a couple of neat features borrowed from RSpec, the genre-defining tool for behavior-driven development (BDD). Along the way, you will explore the philosophy behind automated testing best practices. The automated test runner is one of the most important innovations within software engineering. But for many programmers, automated testing remains a mystery, and knowing how to write good tests is akin to sorcery.
This book is a follow-along practical exercise in building an automated unit test framework, written in the JavaScript language and running on the Node platform. You can think of it as a less functional replacement for well-known packages like Jest, Mocha, Jasmine, and Vitest.
The framework is called a concise test, and it takes the form of an NPM package that you’ll start building in Chapter 1. The book also makes use of a package named todo-example, which is a sample application that will make use of concise-test, as a package dependency.
By the end of Chapter 1, you’ll be able to run the command npm test in your todo-example project directory and have your test runner execute the application test scripts.
Do I Need to Know JavaScript? You’ll need to know modern JavaScript syntax to use this book, including arrow functions, destructuring assignments, and rest parameters. It also uses ECMAScript Modules for importing and exporting values. That being said, don’t stop reading if you don’t know any JavaScript; as long as you have experience in at least one programming language, you should be able to pick it up as we go along. If you’re unsure what some of the syntaxes means, take a moment to stop and look it up online. JavaScript is a wonderfully flexible language. It is dynamic and functional at its core but also has support for objects in a rather unique way, which is empowering.
The beauty of unit testing is that it is universally applicable to all mainstream programming languages, so if you were keen, you could rebuild all of the same code in your favorite language. The only chapters that are directly related to JavaScript are Chapters 9 and
As the chapters of this book unfold, you will see how the humble test runner is an elegant and simple piece of software. Each chapter picks a single feature to build, like the "it" function or the "beforeEach" block. It picks apart the theory of why the feature needs to exist, and how to use it effectively in your test suites. Every chapter ends with a set of ideas for extension points should you wish to explore further, alone or in groups.
The book culminates in an implementation of test doubles and mocks? one of the most difficult and misunderstood concepts within automated testing.
By the end of the book, you will have gained a solid understanding of automated testing principles that you can immediately apply to your work projects.
What You'll Learn:Build a xUnit automated test framework.
See how an automated test runner works.
Understand the best practices for automated unit testing.
Effectively use test doubles and mocks.
Who This Book Is For:Software developers with JavaScript experience who are seeking to master the art of automated testing.
Part I: Building the Core of a Test FrameworkCreating a Barebones Test Runner.
Building a Function to Represent a Test Case.
Grouping Tests.
Promoting Conciseness with Shared Setup and Teardown.
Improving Legibility with Expectations and Matchers.
Part II: Constructing a Usable FrameworkFormatting Expectation Errors.
Automatically Discovering Test Files.
Focusing on Specific Tests.
Supporting Asynchronous Tests.
Reporting.
Part III: Extending for Power UsersShared Examples.
Tagging Tests.
Skipping Tests.
Part IV: Test Doubles and Module MocksDeep Equality and Constraining Matchers.
Test Doubles.
Module Mocks.