Testing utility that generates arbitrary, pseudorandom zod schemas, powered by fast-check
@traversable/zod-test
has 2 peer dependencies:
zod
(v4)fast-check
$ pnpm add -D @traversable/zod-test zod fast-check
Here's an example of importing the library:
import { z } from 'zod'
import { zxTest } from '@traversable/zod-test'
// see below for specifc examples
zxTest.seedToSchema
Use zxTest.seedToSchema
to convert a seed generated by zxTest.SeedGenerator
into a
zod schema that satisfies the configuration options you specified.
import { zxTest } from '@traversable/zod-test'
import * as fc from 'fast-check'
const builder = zxTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = zxTest.seedToSchema(mySeed)
// ^? const mySchema: z.ZodType
zxTest.seedToValidData
Use zxTest.seedToValidData
to convert a seed generated by zxTest.SeedGenerator
into
data that satisfies the schema that the seed represents.
import { zxTest } from '@traversable/zod-test'
import * as fc from 'fast-check'
const builder = zxTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = zxTest.seedToSchema(mySeed)
// ^? const mySchema: z.ZodType
const validData = zxTest.seedToValidData(mySeed)
mySchema.parse(validData) // will never throw
zxTest.seedToInvalidData
Use zxTest.seedToInvalidData
to convert a seed generated by zxTest.SeedGenerator
into
data that does not satisfy the schema that the seed represents.
import { zxTest } from '@traversable/zod-test'
import * as fc from 'fast-check'
const builder = zxTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = zxTest.seedToSchema(mySeed)
// ^? const mySchema: z.ZodType
const invalidData = zxTest.seedToValidData(mySeed)
mySchema.parse(invalidData) // should always throw
zxTest.SeedGenerator
Generates a configurable, pseudo-random "seed builder".
zxTest.seedToSchema
to convert a seed into a zod schemazxTest.seedToValidData
to convert a seed into valid datazxTest.seedToInvalidData
to convert a seed into invalid dataimport { zxTest } from '@traversable/zod-test'
import * as fc from 'fast-check'
const builder = zxTest.SeedGenerator({
include: ["boolean", "string", "object"],
// 𐙘 use `include` to only include certain schema types
exclude: ["boolean", "any"],
// 𐙘 use `exclude` to exclude certain schema types altogether (overrides `include`)
minDepth: 1,
// 𐙘 use `minDepth` to control the schema's minimum depth
// **NOTE:** schemas can get very large!
// using in your CI/CD pipeline is _not_ recommended
object: { maxKeys: 5 },
// 𐙘 specific arbitraries are configurable by name
})
// included schemas are present as properties on your generator...
builder.string
builder.object
// ...excluded schemas are not present...
builder.boolean // 🚫 TypeError
// ...a special wildcard `"*"` property (pronounced "surprise me") is always present:
builder["*"]
/**
* `fast-check` will generate a seed, which is a data structure containing
* integers that represent a kind of AST.
*
* To use a seed, you need to pass it to an interpreter like `zxTest.seedToSchema`,
* `zxTest.seedToValidData` or `zxTest.seedToInvalidData`:
*/
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = zxTest.seedToSchema(mySeed)
// ^? const mySchema: z.ZodType
const validData = zxTest.seedToValidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `validData` should always succeed
const invalidData = zxTest.seedToInvalidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `invalidData` should always fail
zxTest.SeedGenerator
has found several upstream bugs in zod/core
:
z.object
prototype pollution bugz.literal
escaping bugz.core.File
type incompatible with globalThis.File