Skip to content

参考

基于 fast-check v4.8 编写

速查

  • core fast-check 4.8.0 / @fast-check/vitest 0.4.1 / @fast-check/jest 2.2.0(2026)
  • 入口:fc.assert(fc.property(...arbs, predicate));异步 fc.asyncProperty + await
  • numRuns 默认 100fc.nat() 含 0、上界 2147483647fc.double() 默认含 NaN/±Infinity
  • shrinking 自动收缩最小反例;复现 = { seed, path } 填回 fc.assert
  • runner-agnostic:裸用 fc.assert 即可;test.prop([…]) 位置参 / test.prop({…}) 命名参
  • 完整说明见 入门 / 概念与范式 / Arbitraries 与 API / 收缩与复现 / 框架集成与进阶 / 最佳实践

版本锚点

版本发布日期说明
fast-check(core)4.8.02026-05-11dist-tags: latest=4.8.0, legacy=2.15.1
@fast-check/vitest0.4.12026-04-280.4.x 对齐 vitest 4.x;0.4.1 支持 test.each
@fast-check/jest2.2.02026-03-08自动同步 Jest 与 fast-check 的 timeout
@fast-check/ava3.0.12026-05-11AVA 适配
@fast-check/worker0.6.02026-03-08把属性跑进 worker thread(隔离 / 真超时)

版本日期以 npm registry 为准(fast-check 4.8.0 = 2026-05-11)。core 4.x 近期里程碑:4.6.0(stringMatchingmaxLength)、4.7.0(stringMatching 支持 Unicode property、可逆 json)、4.8.0(新增 chainUntil 循环式链生成)。

常用 arbitraries

arbitrary生成什么备注
fc.integer({min,max})区间整数
fc.nat(max?)非负整数含 0,默认上界 2147483647
fc.float() / fc.double(opts)浮点数默认含 NaN/±Infinity,需 noNaN/noDefaultInfinity
fc.boolean() / fc.bigInt()布尔 / 大整数
fc.string({minLength,maxLength,unit})字符串v4 收编 fullUnicode/ascii/hexaStringunit
fc.constant(v)恒定单值
fc.constantFrom(a,b,…)从给定值选一个首参 = shrink 默认目标
fc.option(arb,{nil})可能产出 null
fc.oneof(a,b,…)多 arbitrary 间选可带 {arbitrary,weight}
fc.array(arb,{min,max}Length)数组fc.uniqueArray 去重
fc.tuple(a,b,c)定长异构元组
fc.record({…},{requiredKeys})固定键对象fc.dictionary 键值都生成
fc.json() / fc.anything() / fc.date()JSON / 任意值 / 日期

fc.string 的 unit(v4)

unit含义
'grapheme-ascii'默认,可打印 ASCII
'grapheme'多码点字素
'grapheme-composite'组合字素
'binary'任意码点(除半代理)
'binary-ascii'ASCII 码点
自定义 arbitraryfc.string({ unit: fc.constantFrom('a','b') })

组合子

组合子作用提示
.map(fn)把生成值映射成另一种构造合法输入首选,不丢样本
.filter(p)只保留满足条件的值慎用,过滤太狠 → 慢 / 告警
.chain(fn)用上一值动态决定下一个 arbitrary依赖生成
.noShrink()禁止参与收缩反模式,破坏最小反例
fc.pre(cond)谓词体内前置条件丢弃过多会告警

run 参数

参数默认作用
numRuns100每属性跑多少组输入
seed随机固定后整轮可复现;失败回填
path配合 seed 直达收缩后反例
endOnFailurefalsetrue = 跳过 shrinking,首个失败即停
verbose0(None)详细度 0/1/2
examples[]先跑手写示例再跑生成值(钉回归)
timeout单个异步谓词超时(ms)
interruptAfterTimeLimit到时限停止启动新 run
ignoreEqualValuesfalse忽略重复输入

命令 / API 速查

ts
import fc from "fast-check"; // core 4.8.0

// 同步属性
fc.assert(fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a));
// 异步属性(必须 await)
await fc.assert(fc.asyncProperty(fc.nat(), async (n) => (await f(n)) != null));

// 复现失败:把报告里的 { seed, path } 原样填回
fc.assert(prop, { seed: -1819918769, path: "0:...:3" });

// 适配包(语法糖,runner-agnostic 核心不需要它)
import { test, fc } from "@fast-check/vitest"; // 0.4.1(jest 同理 2.2.0)
test.prop([fc.string()])("name", (s) => /* */ true); // 数组:位置参数
test.prop({ s: fc.string() })("name", ({ s }) => true); // 命名:对象解构

// model-based
const cmds = fc.commands([fc.integer().map((v) => new Push(v)), fc.constant(new Pop())]);
fc.assert(fc.property(cmds, (c) => fc.modelRun(() => ({ model, real }), c)));

// 递归结构(别直接递归调用 arbitrary)
const { tree } = fc.letrec((tie) => ({
  tree: fc.oneof(fc.record({ v: fc.nat() }), fc.record({ l: tie("tree"), r: tie("tree") })),
}));

官方资源