All experienced frontend developers know one thing to be true: Users are unpredictable. No matter how much user research you conduct or how thick the font-weight is on your input label, you can never be certain how users will interact with your product. That’s why, as the creators of the interface, we put in constraints. And to ensure that those constraints work properly, we write tests.

But there’s a problem with traditional unit and integration tests.

They require us to manually think of and write every scenario that our tests will cover. Not only does this take a lot of time, but it also limits the test coverage to our imaginations. Whereas users, as we know, are unpredictable. So we need a way to test our software to withstand an unlimited number of potential user flows.

That’s where property-based testing comes in.

Within this guide, we’ll explain the must-knows of property-based testing in JavaScript. We’ll walk through practical examples and you’ll write your first test using the fast-check framework. Finally, we’ll touch on what other property-based testing frameworks are out there.

Prerequisites:

  • A solid understanding of what unit tests are.
  • Familiarity with Jest or another JavaScript testing framework.
  • (Optional) NPM or Yarn installed if you want to follow along in your IDE.

References:

We’ve created a GitHub repository to accompany this guide. This repository includes all of the featured tests with instructions for how to execute them. It also provides more resources for learning property-based testing.

Property-Based Testing in JavaScript: What and Why

Software testing as we know it today requires a lot of time and imagination. When you’re writing traditional example-based tests, you’re stuck trying to manually reproduce every action that a user might make.

Property-based testing is a different approach to writing tests designed to accomplish more in less time. This is because instead of manually creating the exact values to be tested, it’s done automatically by the framework you’re using. That way, you can run hundreds or even thousands of test cases in the same amount of time it takes you to write one expect statement.

As the developer writing the tests, what you have to do is:

  • Specify what type of values the framework should generate (i.e. integers or strings).
  • Assert those values on guarantees (or properties) that are true regardless of the exact value.

We’ll cover how to choose which properties to test for later in this guide. But before going any further, let’s talk about why you would want to integrate property-based testing into your workflow.

Nicolas Dubien, the creator of the fast-check framework we’re exploring in this guide, wrote a post outlining the primary benefits of property-based testing.

To summarize his words, property-based testing enables developers to:

  • Cover the entire scope of possible inputs: Unless you specifically tell it to, property-based testing frameworks don’t restrict the generated values. As a result, they test for the full spectrum of possible inputs.
  • Shrink the input when tests fail: Shrinking is a fundamental part of property-based testing. Each time a test fails, the framework will continue to reduce the input (i.e. removing characters in a string) to pinpoint the exact cause of the failure.
  • Reproduce and replay test runs: Whenever a test case is executed, a seed is created. This allows you to replay the test with the same values and reproduce the failing case.

In this guide, we’ll focus on that first benefit: Covering the entire scope of possible inputs.

Differences Between Property-Based and Example-Based Tests

Even with the limitations mentioned, traditional example-based tests are likely to remain the norm in software testing. And that’s ok because property-based tests aren’t meant to replace example-based ones. These two test types can, and very likely will, co-exist in the same codebase.

While they may be based on different concepts, property-based and example-based tests have many similarities. This becomes evident when you do a side-by-side comparison of the steps necessary to write a given test.

Property-based

  1. Define data type matching a specification
  2. Perform some operations on the data
  3. Assert properties about the result

Example-based

  1. Set up some example data
  2. Perform some operations on the data
  3. Assert a prediction about the result

At its core, property-based testing is meant to provide an additional layer of confidence to your existing test suite and maybe reduce the number of boilerplate tests. So if you’re looking to try out property-based testing but don’t want to rewrite your entire test suite, don’t worry.

#javascript #property based testing #testing and developing #programming

Property-Based Testing for JavaScript Developers
1.20 GEEK