惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX - 技术
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
F
Fortinet All Blogs
GbyAI
GbyAI
Forbes - Security
Forbes - Security
The Cloudflare Blog
博客园 - 司徒正美
博客园_首页
量子位
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
N
News | PayPal Newsroom
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
T
Tor Project blog
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
Jina AI
Jina AI
Help Net Security
Help Net Security
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
N
News and Events Feed by Topic
I
Intezer
B
Blog
美团技术团队
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
AWS News Blog
AWS News Blog
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
有赞技术团队
有赞技术团队

The Practical Developer

The Libuv Thread Pool Trap: Why Node.js Async APIs Stall Under Load Postgres Covering Indexes with INCLUDE: Eliminate Heap Fetches on Read-Heavy Workloads Postgres DISTINCT ON: The Fastest Way to Get the Latest Row Per Group Postgres Transaction Isolation: The Anomalies Your App Actually Faces in Production Linux TCP Tuning for Node.js Microservices: The Kernel Settings That Stop Silent Connection Drops Under Load Postgres HOT Updates and Fillfactor: Why Not All Writes Are Created Equal Database Connection Pool Leaks: Finding the Promise That Never Returns Its Seat Linux OOM Killer in Production: Why Your Node.js Containers Die Without a Stack Trace Postgres Materialized Views: Refresh Strategies That Do Not Lock Your Dashboards API Dependency Health Checks: Why /health Is Not Enough Authorization with Zanzibar Tuples: How Google Manages Permissions and How To Build the Same Check in Node.js Postgres Advisory Locks: The 20-Character Primitive That Replaces Redis for Coordination Dead Letter Queues: The Message Queue Pattern That Saves You at 2 a.m. File Descriptor Exhaustion: The Kernel Limit That Silently Drops Node.js Connections Graceful Degradation: The Pattern That Turns Total Outages into Partial Success PostgreSQL Full-Text Search: Dropping Elasticsearch for 90% of Use Cases S3 Presigned Multipart Uploads: Stop Your API Server from Being a File Upload Bottleneck MessagePack vs JSON: The Binary Serialization Switch That Cut Our Internal RPC Overhead by 40% DNS Caching in Node.js: The Silent Cause of Production Latency Spikes Reliable Cron Jobs: The Pattern That Stops Double Runs, Missed Executions, And The 2 AM Page GraphQL Query Complexity: Stop the OOM Query Before It Reaches Your Resolver Node.js Event Loop Lag: The Hidden Metric Behind Random Latency Spikes API Request Validation with Zod: The Schema That Catches Bad Input Before It Corrupts Your Database Load Shedding in Node.js: How to Reject Traffic Before You Drown Request Hedging: Cut Tail Latency In Half Without Overprovisioning Git Bisect: The Automated Binary Search That Finds Breaking Commits in Minutes Node.js Garbage Collection Tuning: Stop Letting V8 Pause Your Event Loop Node.js Server Timeouts: The Settings That Stop Slow Clients from Holding Sockets Hostage Postgres BRIN Indexes: The Time-Series Secret That Shrinks Indexes by 99% Event Sourcing with PostgreSQL: The Pragmatic 80% Solution Node.js Cluster Mode: Scaling the Event Loop Across CPU Cores Postgres Partial Indexes: Stopping Soft Deletes from Ruining Your Query Performance Request Coalescing with the Singleflight Pattern: Stop Drowning Your Database on Every Cache Miss The Bulkhead Pattern: Why One Slow Endpoint Should Not Drown Your Whole Service Node.js AsyncLocalStorage: End-to-End Request Context Without the Propagation Hell Postgres Deadlocks: Logging the Victim, Reproducing the Race, and Fixing the Lock Order Your Node.js HTTP Client Is the Bottleneck: Connection Pool Tuning That Works Optimistic Locking in Postgres: Stop Losing Data to Race Conditions Postgres Read Replicas: Stop Serving Stale Data to Your Users Cursor Pagination: Why Offset Queries Explode at Scale and How to Fix Them Node.js Worker Threads: 60 Lines That Stop a CSV Upload from Timing Out Every Other Request Reliable Webhook Delivery: Architecture for Outbound HTTP You Can Trust Request Timeouts and Deadline Propagation: Stop the Chain of Slowness Advanced Security Practices in Node.js Graceful Shutdown in Node.js: The 40 Lines That Stop 502s During Deploys Finding Node.js Memory Leaks with Heap Snapshots Idempotency Keys in 30 Lines: Stop Your Webhook From Charging Customers Twice Backpressure In Node.js: The Fix For Slow-Motion Queue Meltdowns Retries Done Right: Jitter, Budgets, and the Stampede You Did Not See Coming The Cache Stampede: Why Your "Just Add Redis" Layer Crashes Postgres at 3 a.m. Postgres SKIP LOCKED: An 80-Line Job Queue You Can Run Without Redis Stop Doing Work Nobody Wants: AbortController in Node.js, Done Right The N+1 Query Problem: We Found 23 In One Codebase And Killed Every One I Tried 5 AI Coding Tools for a Month. Here Is What I Actually Use CI/CD From Zero to Production in 30 Minutes With GitHub Actions Node.js vs Bun vs Deno: Which Runtime Should You Pick in 2025? Kubernetes Resource Requests And Limits: The Numbers That Decide If Your Cluster Is Stable The Three Pillars of Observability Are A Myth: What Actually Matters In Production pnpm Vs npm Vs yarn Vs Bun For Monorepos: Which One Earns The Migration In 2024 JSONB Indexing In Postgres: GIN Vs Expression Indexes, And When Each Is The Right Choice A Code Review Checklist That Ends The Same Three Arguments Every Sprint gRPC Vs REST In 2024: When The Switch Pays For Itself React Suspense For Data Fetching: The Pattern That Replaces Half Your Loading State Code The Five-Stage Rollout: How To Ship A Risky Change Without Holding Your Breath GitHub Actions In A Monorepo: Caching, Path Filters, And Secret Boundaries That Actually Work The Blameless Postmortem That Actually Improves Things: A Template And Six Hard-Won Rules Recursive CTEs In Postgres: How To Query A Tree Without N Round Trips Node.js Streams: When They Actually Help, And When They Just Add Complexity Playwright Vs Cypress In 2024: The Honest Comparison Of Which One Earns The Test Time React Server Components: The Mental Model That Makes The "use client" Boundary Obvious Pod Disruption Budgets: The K8s Object That Keeps Your Service Up During Cluster Maintenance Postgres LISTEN/NOTIFY: The Pub/Sub You Already Have And Are Not Using Chaos Engineering Starter Kit: The Five Drills That Don't Need Netflix-Scale Spec-Driven API Development With OpenAPI: How To Stop Drifting From Your Docs Kubernetes Autoscaling Beyond CPU: The Custom-Metric HPA Pattern That Actually Works Postgres Partitioning For Time-Series: The Boring Setup That Saves Your Database Distributed Locks With Redis: An Honest Look At Redlock And When You Don't Need It HTTP/2 vs HTTP/3: What Actually Changes For Your App, And What Doesn't Image Optimization For The Web In 2023: srcset, AVIF, And The Lighthouse Score You Actually Want Kafka vs RabbitMQ: A Decision Tree That Doesn't Hate You UUID vs Bigint Primary Keys In Postgres: The Index Math That Decides For You Flame Graphs: How To Find The Slow Function In 30 Seconds Without Profiling Theatre Postgres Streaming Vs. Logical Replication: Which One Solves Your Actual Problem ESLint Rules That Earn Their Keep: The Twelve I Enable On Every Project Pre-Commit Hooks That Pay For Themselves: Husky, lint-staged, And The Five Rules That Stick Zero-Downtime Database Migrations: The Six-Step Pattern That Rules Them All Circuit Breakers In Node.js: 50 Lines That Stop A Failing Dependency From Taking Down Your Service Postgres VACUUM Is Not Magic: How Your Hot Table Bloats To 80GB And How To Fix It Kubernetes Liveness And Readiness Probes: The Difference That Causes Half Your Outages Rate Limiting In Production: A Token Bucket In 30 Lines Of Redis The Outbox Pattern: How To Stop Losing Events When Postgres And Kafka Disagree Load Testing With k6: The Three Scenarios That Find Real Bugs (Not Synthetic Numbers) Postgres Row-Level Security For Multi-Tenant Apps: The Pattern That Stops You From Leaking Data Rebase vs. Merge: The Team Policy That Ends The Argument Forever OpenTelemetry in Node.js: Distributed Tracing That Actually Helps During an Incident Feature Flags That Pay Rent: The 4 Flag Types And When To Delete Each ETag, Last-Modified, and the Caching Headers Most APIs Get Wrong Connection Pooling Without the Cargo Cult: pgbouncer in 100 Lines of Config JSONB Is Not a Schema: When To Reach For It in Postgres, And When To Stop Bash Strict Mode: The Three Lines That Stop Your Deploy Script From Lying To You
Property-Based Testing with Fast-Check: Tests That Find Bugs You Did Not Know To Look For
The Practica · 2026-06-13 · via The Practical Developer

You wrote 60 unit tests for the payment reconciliation module. Every line is covered. The CI badge is green. The deploy goes out at 10 a.m. At 10:47, the on-call phone rings: a batch of refunds failed with a cryptic arithmetic error that only happens when exactly three line items share the same discount code and one was already voided.

That test case did not exist in your 60 tests. Nobody thought to generate it. You did not need more tests. You needed tests that search for counterexamples instead of validating examples you already guessed at. That is what property-based testing does.

This post is about using Fast-Check, the best property-based testing library for TypeScript and Node.js, to find bugs that example-based tests never catch. You will learn the mental model shift, the concrete patterns that work in production code, and how to graft property-based testing onto an existing test suite without rewriting everything.

The mental model shift

Example-based testing says: “Given input X, the output should be Y.” You pick X. You assert Y. If X is well-chosen, the test passes. If X is poorly chosen or the bug lives at input Z that nobody typed, the test passes anyway.

Property-based testing says: “For all inputs that satisfy condition P, the output should satisfy condition Q.” You define P (the preconditions) and Q (the postconditions). The framework generates hundreds of random inputs that satisfy P, runs your function, and checks Q. If any input violates Q, the framework shrinks it to the smallest failing case and hands it to you.

The hard part is not the library. The hard part is figuring out what properties your code must satisfy. Once you have the property, the library does the brute-force work. The skill is learning to recognize properties in everyday business logic.

Let me show you what that looks like in practice.

Getting started with Fast-Check

Install it in one line:

npm install --save-dev fast-check

Fast-Check works with Vitest, Jest, Mocha, or any test runner. Here is the simplest possible property test:

import * as fc from 'fast-check';
import { describe, it, expect } from 'vitest';
import { formatCurrency } from './money';

describe('formatCurrency', () => {
  it('never produces a negative sign for zero', () => {
    fc.assert(
      fc.property(
        fc.integer({ min: 0, max: 100000 }),
        (amount) => {
          const result = formatCurrency(amount, 'USD');
          return !result.startsWith('-');
        }
      )
    );
  });
});

This generates 100 random integers between 0 and 100,000, runs formatCurrency on each, and checks that none of them produce a string starting with -. If formatCurrency has a bug where 0 becomes -0.00 or a large number overflows into negative, the test finds it.

The fc.assert call is the key. By default it runs 100 random iterations (controlled by fc.configureGlobal or per-test options). When a failure is found, Fast-Check shrinks the input to the minimal reproduction case.

Shrinking: the feature that makes this usable

Shrinking is what separates property-based testing from “just fuzz it.” Without shrinking, you get a failing input like {items: [{price: -8372, qty: 9}, {price: 0.01, qty: 1}], taxRate: 0.07, discount: 0.15} and you have to guess which part caused the failure.

With shrinking, Fast-Check reduces the failing input to the smallest possible counterexample. It looks like this:

Error: Property failed after 42 tests
Counterexample: [{price: -1, qty: 1}]
Shrunk 17 time(s)

That is actionable. You know immediately that a negative price breaks the calculation. You write the fix, add min: 0 to the price input, and move on.

Shrinking works because Fast-Check’s arbitraries know how to reduce themselves. An integer arbitrarily knows it can shrink toward 0. An array arbitrarily knows it can remove elements. A string arbitrarily knows it can shorten. When you compose arbitraries, the shrinkers compose too.

Six properties that apply to almost every function

The hardest part of adopting property-based testing is learning to see properties. Here are six categories that cover 80% of real-world code.

1. Idempotency (running twice gives the same result)

This is the easiest property to write and the most revealing. If your function is idempotent, running it twice should produce the same output as running it once.

import * as fc from 'fast-check';
import { sanitizeEmail } from './sanitize';

describe('sanitizeEmail', () => {
  it('is idempotent', () => {
    fc.assert(
      fc.property(fc.emailAddress(), (email) => {
        const once = sanitizeEmail(email);
        const twice = sanitizeEmail(once);
        return once === twice;
      })
    );
  });
});

If sanitizeEmail is truly idempotent, the test passes. If there is a bug where the first pass misses a case that the second pass catches, the test fails with the specific email that breaks idempotency.

This property catches: incomplete normalization, state leaks, unstable sort orders, and timestamp injection bugs.

2. Round-trip consistency (encode then decode)

Any function pair that serializes and deserializes should survive a round trip.

import * as fc from 'fast-check';
import { encrypt, decrypt } from './crypto';

describe('encrypt/decrypt', () => {
  it('round-trips any valid payload', () => {
    fc.assert(
      fc.property(
        fc.oneof(fc.string(), fc.unicodeString(), fc.json()),
        fc.constant('aes-256-gcm-key-here'),
        (payload, key) => {
          const encrypted = encrypt(payload, key);
          const decrypted = decrypt(encrypted, key);
          return decrypted === payload;
        }
      )
    );
  });
});

This test found a bug in my team’s encryption wrapper on day one: Unicode strings with emoji were getting mangled because the codec assumed UTF-8 was ASCII-compatible in the padding calculation. One Fast-Check test caught what manual testing with “hello world” never would.

Round-trip properties apply to: JSON serialization, URL encoding, encryption, compression, base64, token generation, and database model serialization.

3. Invariant preservation (the shape stays the same)

When you transform data, certain invariants should hold across the transformation.

import * as fc from 'fast-check';
import { sortByPriority } from './scheduler';

describe('sortByPriority', () => {
  it('preserves the set of items', () => {
    fc.assert(
      fc.property(
        fc.array(
          fc.record({
            id: fc.uuid(),
            priority: fc.integer({ min: 1, max: 5 }),
            name: fc.string()
          }),
          { minLength: 1, maxLength: 100 }
        ),
        (items) => {
          const sorted = sortByPriority(items);
          const originalIds = new Set(items.map(i => i.id));
          const sortedIds = new Set(sorted.map(i => i.id));
          return originalIds.size === sortedIds.size &&
                 [...originalIds].every(id => sortedIds.has(id));
        }
      )
    );
  });
});

Invariant tests are great for: sorting (same items before and after), filtering (result is a subset of input), validation (errors on invalid but not valid), and aggregation (total count equals sum of parts).

4. Order independence (commutative operations)

If operations should commute, test that the result does not depend on order.

import * as fc from 'fast-check';
import { applyDiscounts } from './pricing';

describe('applyDiscounts', () => {
  it('applies discounts commutatively', () => {
    fc.assert(
      fc.property(
        fc.record({
          subtotal: fc.float({ min: 0, max: 10000, noNaN: true }),
          discounts: fc.array(
            fc.float({ min: 0, max: 1, noNaN: true }),
            { maxLength: 5 }
          )
        }),
        ({ subtotal, discounts }) => {
          const forward = applyDiscounts(subtotal, discounts);
          const backward = applyDiscounts(subtotal, [...discounts].reverse());
          return Math.abs(forward - backward) < 0.01;
        }
      )
    );
  });
});

This catches: stateful accumulation bugs, global counters, and ordering assumptions baked into business logic.

5. No unexpected exceptions

The lowest bar is also the most valuable: a function should not throw for any valid input.

import * as fc from 'fast-check';
import { parsePacket } from './protocol';

describe('parsePacket', () => {
  it('never throws for valid byte sequences', () => {
    fc.assert(
      fc.property(
        fc.uint8Array({ minLength: 4, maxLength: 65536 }),
        (bytes) => {
          // Mark the beginning of a valid frame
          bytes[0] = 0x02; // STX
          bytes[bytes.length - 1] = 0x03; // ETX
          expect(() => parsePacket(bytes)).not.toThrow();
        }
      )
    );
  });
});

This is the property equivalent of “scream tests” and it finds the same kind of bugs. If your function has an if (x) { ... } else { throw new Error() } path that only activates for a rare combination of inputs, a property test will find it within a few hundred iterations.

6. Input boundaries (off-by-one and overflow)

Even though Fast-Check generates random values, you can guide it toward edge cases.

import * as fc from 'fast-check';
import { paginate } from './pagination';

describe('paginate', () => {
  it('never returns more items than requested', () => {
    fc.assert(
      fc.property(
        fc.array(fc.string()),
        fc.integer({ min: 0, max: 100 }),
        fc.integer({ min: 0 }),
        (items, pageSize, page) => {
          const result = paginate(items, pageSize, page);
          return result.items.length <= pageSize;
        }
      )
    );
  });
});

The fc.integer({ min: 0, max: 100 }) for pageSize generates values that are often 0, 1, 50, 100, and everything in between. Zero page sizes and single-item pages are classic edge cases that manual tests skip.

Grafting property tests onto an existing suite

You do not need to rewrite your entire test suite. Property-based testing complements example-based tests. Here is the strategy I use:

  1. Start with the pure functions. Utils, validators, transformers, serializers, parsers. These have zero side effects and the strongest properties.

  2. One property test per module. Do not write 20 property tests for one function. Start with one invariant that matters most. Add more as you find bugs.

  3. Keep your example tests. Example tests document expected behavior for humans. Property tests verify behavior for machines. Both are needed.

  4. Use the seed to reproduce failures. When Fast-Check finds a counterexample, it prints a seed. Re-running with the same seed reproduces the exact sequence of inputs:

fc.assert(
  fc.property(arbitrary, check),
  { seed: 42, numRuns: 1000 }
);
  1. Constrain inputs wisely. The most common mistake is using arbitraries that are too broad. fc.string() generates empty strings, very long strings, strings with null bytes, and Unicode control characters. If your function is not designed to handle those, constrain the arbitrary to match the domain:
fc.string({ minLength: 1, maxLength: 255 })
fc.constantFrom('pending', 'confirmed', 'cancelled')
fc.integer({ min: 0 })

A real bug caught by property tests

Here is a bug I found in production code using this technique. The function takes a list of scheduled jobs and groups them into hourly batches:

interface Job {
  id: string;
  scheduledAt: Date;
  priority: number;
}

function groupJobsByHour(jobs: Job[]): Map<string, Job[]> {
  const groups = new Map<string, Job[]>();
  for (const job of jobs) {
    const hour = job.scheduledAt.toISOString().slice(0, 13);
    if (!groups.has(hour)) {
      groups.set(hour, []);
    }
    groups.get(hour)!.push(job);
  }
  return groups;
}

Looks fine. The example test passes. The property test:

it('each job appears in exactly one group', () => {
  fc.assert(
    fc.property(
      fc.array(
        fc.record({
          id: fc.uuid(),
          scheduledAt: fc.date({ min: new Date('2025-01-01'), max: new Date('2025-12-31') }),
          priority: fc.integer({ min: 1, max: 10 })
        }),
        { minLength: 1, maxLength: 500 }
      ),
      (jobs) => {
        const groups = groupJobsByHour(jobs);
        const allJobsInGroups = new Set<string>();
        for (const group of groups.values()) {
          for (const job of group) {
            allJobsInGroups.add(job.id);
          }
        }
        return allJobsInGroups.size === jobs.length;
      }
    )
  );
});

After 247 iterations, Fast-Check found the bug: when two jobs have the same ID in the input, one of them is silently dropped. The function assumed callers deduplicate. The callers assumed the function deduplicates. Nobody noticed because all 20 example tests used unique IDs.

The fix was a one-line guard that throws on duplicate IDs. The property test uncovered a data quality issue that example-based testing could have missed for years.

When property-based testing does not help

Property-based testing is not a silver bullet. It struggles with:

  • Side-effect-heavy code. Database calls, HTTP requests, file I/O. You need test doubles, and the property value drops because your arbitraries generate fake data that bypasses real system behavior. Use integration tests for those instead.

  • UI rendering. DOM output is hard to express as properties. “The button should always be visible when the user is logged in” is a property, but writing the arbitrary that generates all valid logged-in states is often harder than writing three example tests.

  • Algorithms with oracle functions. If the only way to check correctness is to run a slower, authoritative implementation and compare, you have effectively written two implementations and proven they agree. That is useful, but it is closer to differential testing than property testing.

  • Non-deterministic code. Functions that depend on time, random numbers, or external state cannot reliably pass property tests unless you inject the dependencies.

For everything else, property-based testing catches bugs that example-based testing misses by construction. The tests are shorter, more expressive, and they force you to think about the invariants your code must satisfy rather than the specific examples you happened to type.

Practical workflow recommendations

If you are starting from zero, here is the fastest path to value:

  1. Add fast-check as a dev dependency.
  2. Pick one pure utility function that does something important (formatting, validation, transformation).
  3. Write one idempotency or round-trip property for it.
  4. Run the test and watch it pass on the first 100 random inputs.
  5. Change the function slightly to introduce a bug (comment out a normalization step). Watch the test fail and observe the shrinking output.
  6. Restore the function. Commit the property test.
  7. Repeat for the next function.

Do not try to convert your entire test suite in one sitting. One property test per module, added during code review when you see a bug or a close-call, compounds into a suite that finds real bugs faster than example-based tests.

The seed matters. When a property test fails in CI, log the seed so the next person can reproduce locally without iterating through randomness:

afterEach(() => {
  if (expect.getState().currentTestName?.includes('property')) {
    // Log seed from fc context
  }
});

A better approach: use fc.configureGlobal({ seed: Number(process.env.FC_SEED) }) so you can pin the seed via environment variable when a CI run reports a failure.

The practical takeaway

Property-based testing shifts the question from “did I guess the right inputs?” to “does my code hold up under all inputs?” That is a fundamentally stronger guarantee. It does not replace example-based tests. It fills the gap they leave open: the edge case nobody thought to write.

Start with one property. Pick a function that transforms data without side effects. Write the inverse or the invariant. Watch Fast-Check generate a hundred inputs you would never have typed. When it finds something you missed, you will never look at unit tests the same way again.

A note from Yojji

Shipping code that survives rare edge cases in production is the difference between a team that fixes bugs and a team that prevents them from reaching users. The discipline of writing property-based tests alongside example tests is one of those high-leverage engineering habits that compounds over the life of a project. Yojji’s teams apply this kind of rigorous testing methodology across the Node.js and TypeScript microservices they build for clients.

Yojji is an international custom software development company founded in 2016, with offices across Europe, the US, and the UK. Their dedicated engineering teams handle full-cycle product delivery including the testing strategies that keep production reliable, working with the JavaScript ecosystem (React, Node.js, TypeScript) and cloud platforms (AWS, Azure, GCP).