Minimizing Failing Test Inputs with fast-check

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About This Article
This article was created using an automated generation workflow leveraging generative AI. The implementation from existing educational materials was cross-referenced with official documentation to organize usage examples, constraints, and reference test results.

Verification Status: Partially cross-referenced with official documentation and reference-tested. Browser behavior for the fixed demo version has not been verified.

Assuming that addition always increases values causes negative inputs to be overlooked. Experiments are used to find failing inputs and determine which values invalidate the assumption.

Open Live Demo

Key Terms to Know First

  • A counterexampleis an input where the assumption does not hold.

  • Shrinkingis the process of reducing the input while maintaining the failure.

Thinking via a Branching Flowchart

Use a diamond node to verify whether the assumption holds. The next step changes depending on the result.

flowchart TD
    A["二つの整数を生成"] --> B{"仮定は成り立つ?"}
    B -- "はい" --> C["次の入力を試す"]
    B -- "いいえ" --> D["失敗を保って入力を縮める"]

Live Demo Operations

Try with seed 42. Change it to 43 and observe the counterexample.

How to Read the Results

A small counterexample containing a negative b is obtained.

The assumption tested in the example is that a+b is greater than or equal to a. Focusing on the sign of b allows for a concise explanation of why it failed.

Code Highlights

Prepare two integer generators and check the assumption using fc.property.

The implementation can be viewed in the implementation code section of the demo screen.

When using in production

Save the seed and path in failure reports to make it easier to reproduce the same counterexample later.

Observing a test failure as a success

The purpose of this tutorial is not to ensure that a program passes all tests, but to find inputs that break false assumptions. The number 42 in the input field is a seed that specifies the starting condition for random number generation, not the target numbers a or b. Clicking Execute displays the presence or absence of failure, the counterexample, the number of shrink steps, the seed, and the path in the result column. Next, change it to 43 and compare, anticipating both cases where the counterexample remains the same and where it differs. Specific counterexamples and shrink counts are not predetermined as fixed values until the actual result is verified.

The assumption is that a+b is greater than or equal to a. For example, if a=10 and b=3, it becomes 13 and satisfies the assumption. However, if b=-1, it becomes 9, which does not satisfy it. The cause is not whether a is negative or positive, but that b is negative. In this tutorial, which limits integers from -1000 to 1000, this is not a range where the assumption breaks due to JavaScript numeric precision. Substituting the found counterexample value into the expression explains the reason for the failure.

Differences between test cases and properties

In conventional unit tests, input and expected values are prepared one by one. Property-based tests express rules that should hold true regardless of specific inputs, and generate various inputs for those rules. The implementation's fc.integer defines the conditions for generating integers, and fc.property combines two generators with a property condition. fc.check examines the property and returns result information. Instead of ending with an exception throw upon failure, the tutorial displays the results so readers can observe them.

numRuns is set to 100. This is a standard setting for the number of trials and does not guarantee that all 100 items will be executed before stopping, nor does it guarantee exhaustive coverage of all inputs. Trials to shrink the counterexample may be performed starting from the point where a failure is found. Do not conclude that a property is mathematically correct based solely on 100 successes.

Reasons for shrinking counterexamples

Even if a counterexample containing a large negative number appears, the same failure can sometimes be explained with simpler values. Shrinking is the process of simplifying inputs while maintaining the failure. In this example, values like 0 and -1 make it easier for humans to understand why the failure occurs. However, this does not guarantee that a specific value will always become the final counterexample. Results may vary depending on generators, library versions, and configurations.

To achieve reproducibility, save not only the counterexample itself, but also the seed, path, library version, property predicate, and generation range. While the seed can be specified in the demo input field, there is no UI to specify and replay up to the path. Detailed reproduction requires providing the appropriate settings in your local test code. Note that because the implementation uses Number(v)||42, inputs that evaluate to 0 or cannot be converted to a number will default to 42.

Connecting to business code tests

For example, if you assumed that the quantity always increases in an inventory addition process, check whether the specifications handle negative numbers for returns or corrections. Whether to limit the generation range to positive quantities or change the property itself to "inventory is the result of adding the change amount to the original value" depends on business specifications. Rather than conveniently excluding inputs to match test failures, verify the rationale for the tolerance conditions.

After fixing a bug, the specific counterexample found can be added as a standard regression test. Combining property-based inspection with fixed regression testing helps both with exploring unknown inputs and preventing the recurrence of known bugs. Even if the execution result shows a failure, observing that the assumption is incorrect aligns with the objective of this tutorial. In the case of a module fetch error, execution stops before a counterexample is produced, so distinguish between Console and Network. The reference test verified arithmetic across the entire range, but the fast-check shrinking process was not executed.

Verification Range

Asserted (a+b>==a)===(b>=0) to assert. Verified the incorrect assumption. Generation, shrinking, and seed/path reproduction of fast-check itself were not performed.

The targets are current browsers and the course-specified fast-check 4.0.0. Network communication to external CDNs and Mermaid is required, and actual rendering of course materials, diagram drawing, and re-verification of the pinned-version license remain pending. Since official documentation also contains descriptions for other versions, this must be distinguished from proof of execution with the pinned version.

References

GitHub Samples

Implementation and Experimental Procedures

Document information

Article title
Minimizing Failing Test Inputs with fast-check
Published
Updated
Source
https://papanda925.com/?p=18979&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL