Testing utility that generates arbitrary, pseudorandom arktype schemas, powered by fast-check
@traversable/arktype-test has 2 peer dependencies:
arktype (v2.1)fast-check$ pnpm add -D @traversable/arktype-test arktype fast-check
Here's an example of importing the library:
import { type } from 'arktype'
import { arkTest } from '@traversable/arktype-test'
// see below for specifc examples
arkTest.seedToSchemaarkTest.seedToValidDataarkTest.seedToInvalidDataarkTest.SeedGeneratorarkTest.SeedValidDataGeneratorarkTest.SeedInvalidDataGenerator@traversable/arktype-test has found several upstream bugs:
arkTest.seedToSchemaUse arkTest.seedToSchema to convert a seed generated by arkTest.SeedGenerator into an ArkType schema that satisfies the configuration options you specified.
import { type } from 'arktype'
import { arkTest } from '@traversable/arktype-test'
import * as fc from 'fast-check'
const builder = arkTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = arkTest.seedToSchema(mySeed)
// ^? const mySchema: type.Any
arkTest.seedToValidDataUse arkTest.seedToValidData to convert a seed generated by arkTest.SeedGenerator into
data that satisfies the schema that the seed represents.
import { type } from 'arktype'
import { arkTest } from '@traversable/arktype-test'
import * as fc from 'fast-check'
const builder = arkTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = arkTest.seedToSchema(mySeed)
// ^? const mySchema: type.Any
const validData = arkTest.seedToValidData(mySeed)
mySchema(validData) instanceof type.errors // always `false`
arkTest.seedToInvalidDataUse arkTest.seedToInvalidData to convert a seed generated by arkTest.SeedGenerator into
data that does not satisfy the schema that the seed represents.
import { type } from 'arktype'
import { arkTest } from '@traversable/arktype-test'
import * as fc from 'fast-check'
const builder = arkTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = arkTest.seedToSchema(mySeed)
// ^? const mySchema: type.Any
const invalidData = arkTest.seedToValidData(mySeed)
mySchema(invalidData) instanceof type.errors // always `true`
arkTest.SeedGeneratorarkTest.SeedGenerator is fairly low-level. All of the other exports of this library have been implemented in terms of arkTest.SeedGenerator.
Generates a configurable, pseudo-random "seed builder".
arkTest.seedToSchema to convert a seed into an ArkType schemaarkTest.seedToValidData to convert a seed into valid dataarkTest.seedToInvalidData to convert a seed into invalid dataimport { type } from 'arktype'
import * as fc from 'fast-check'
import { arkTest } from '@traversable/arktype-test'
const builder = arkTest.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`)
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 `arkTest.seedToSchema`,
* `arkTest.seedToValidData` or `arkTest.seedToInvalidData`:
*/
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = arkTest.seedToSchema(mySeed)
// ^? const mySchema: type.Any
const validData = arkTest.seedToValidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `validData` should always succeed
const invalidData = arkTest.seedToInvalidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `invalidData` should always fail
arkTest.SeedValidDataGeneratorLike arkTest.SeedGenerator, except arkTest.SeedValidDataGenerator comes pre-configured to exclude schemas that make it impossible to reliably generate valid data.
arkTest.SeedValidDataGenerator does not accept any options. If you need more fine-grained control of the schemas being generated, use arkTest.SeedGenerator.
arkTest.SeedInvalidDataGeneratorLike arkTest.SeedGenerator, except arkTest.SeedValidDataGenerator comes pre-configured to exclude schemas that make it impossible to reliably generate invalid data.
arkTest.SeedInvalidDataGenerator does not accept any options. If you need more fine-grained control of the schemas being generated, use arkTest.SeedGenerator.