Skip to main content

mock

@webda/mock

Coherent mock-data generation for @webda/models classes — decorator-driven field population with deterministic seeding, multi-model graph support, and optional AI-generated text.

When to use it​

  • You need realistic, consistent test fixtures for Webda models without writing manual factory functions.
  • You want reproducible test data with mode: "test" (seed=0) and varied demo data with mode: "demo".
  • You need to populate an entire relational graph of models (e.g. Users + Orders + Posts) in one call.

Install​

pnpm add -D @webda/mock

Configuration​

@webda/mock is a pure library — no webda.config.json entry is required. Field hints are added via @Mock.* decorators from @webda/models (zero production cost — tree-shaken out).

OptionTypeDefaultDescription
countnumber1Number of instances to generate
seednumber0 (test) / Date.now() (dev)RNG seed for reproducibility
mode"test" | "dev" | "demo" | "load""test" in VitestControls seed default, AI usage, and session pool
overridesPartial<T>—Force-set specific fields regardless of inference
strictbooleanfalseThrow instead of skip on unhinted, uninferable fields
aiAIProvider—AI provider for @Mock.ai fields (e.g. AnthropicProvider)

Usage​

import { Mock } from "@webda/models";
import { generate, generateGraph } from "@webda/mock";

// Annotate your model fields
class Post {
@Mock.word accessor title!: string;
@Mock.paragraph accessor body!: string;
@Mock.pastDate accessor publishedAt!: Date;
}

class User {
@Mock.firstName accessor firstName!: string;
@Mock.email accessor email!: string;
@Mock.integer({ min: 18, max: 99 }) accessor age!: number;
}

// Generate 5 deterministic posts (seed=42)
const posts = await generate(Post, { count: 5, seed: 42, mode: "test" });
// posts[0].title is always the same string across runs

// Generate a relational graph: 10 users and 50 posts
const graph = await generateGraph(
{ User: 10, Post: 50 },
{ models: [User, Post], seed: 1, mode: "dev" }
);
// graph.User → User[], graph.Post → Post[]

// Optional: AI-generated text fields (Anthropic)
import { AnthropicProvider } from "@webda/mock";

const ai = new AnthropicProvider({ apiKey: process.env.ANTHROPIC_API_KEY });

class Product {
@Mock.word accessor name!: string;
@Mock.ai({ prompt: "One-sentence marketing tagline for a fictional product." })
accessor tagline!: string;
}

const products = await generate(Product, { count: 3, mode: "demo", ai });

Modes​

ModeSeed defaultAIUse case
test0 (deterministic)Throws on @Mock.aiUnit tests, snapshots
devDate.now()Enabled if provider givenLocal development seeding
demoLoggedEnabled + preferred for textDemo environments
loadCaller-suppliedDisabledLoad/performance testing

Reference​

  • API reference: see the auto-generated typedoc at docs/pages/Modules/mock/.
  • Source: packages/mock
  • Related: @webda/models for the @Mock.* decorator definitions; @webda/test for the WebdaTest harness where mock data is typically used; @webda/fs for FileStore to persist generated data locally.

Classes​

Interfaces​

Type Aliases​

Functions​