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:
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.
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:
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:
In this guide, we’ll focus on that first benefit: Covering the entire scope of possible inputs.
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
Example-based
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