Skip to content
Plugin
Faker logo
plugins: [pluginFaker()]
Officialv5.4.5kubb >=5.19.0node >=22.22

OpenAPI to Faker mock data generator

@kubb/plugin-faker

Generates a Faker.js mock-data factory for every schema in your OpenAPI spec.

kubb add plugin-faker
fakermock-datamocksfixturestestingcodegenopenapi

The Faker plugin generates one factory per schema. Each factory returns Faker.js mock data and takes partial data that overrides the generated values.

Use the factories in tests, Storybook, and local development without a running backend.

Installation

kubb add plugin-faker

kubb add installs the version the Kubb registry pins and adds pluginFaker() to kubb.config.ts. It needs a kubb.config.ts, so run kubb init first in a new project.

Setup

  • Add @kubb/plugin-ts for the factory types. Generation fails without it.
  • Install @faker-js/faker v10 in the app that uses the generated code.
  • Install dayjs, moment, or randexp when you pick it in dateParser or regexGenerator.

Options worth knowing

OptionDefaultWhat it does
seedNoneMakes every run return the same data.
locale'en'Sets the Faker locale, such as 'de'.
typeMode'inferred'Types factories from the generated values, or from the declared model with 'schema'.
dateParser'faker'Formats date, time, and date-time strings with faker, dayjs, or moment.
regexGenerator'faker'Turns a pattern into a string with faker or randexp.

Example

This config generates the factories with a fixed seed. For a Pet schema, the plugin writes createPet.ts:

import { defineConfig } from 'kubb/config'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginFaker } from '@kubb/plugin-faker'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  plugins: [
    pluginTs(),
    pluginFaker({ seed: [100] }),
  ],
})

Call createPet({ name: 'Rex' }) to override one field and keep the generated rest. To serve these mocks from MSW handlers, see @kubb/plugin-msw.

Dependencies

Extensions that must be in the same config as Faker.

Used by

Extensions that list Faker as a dependency.