Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content

Migration: @kubb/plugin-faker

Part of the v4 → v5 migration guide. See the full option reference in @kubb/plugin-faker.

dateType, integerType, unknownType, emptySchemaType, and contentType moved to adapterOas. See Migration: @kubb/adapter-oas. resolver.resolveName replaces transformers.name, and macros replace transformers.schema. The generators option is gone.

Removed: paramsCasing

Properties inside the generated path, query, and header mocks are now always camelCase, so drop the option. This keeps the mocks assignable to the types from @kubb/plugin-ts, which also camelCases parameters.

v4 kubb.config.ts
typescript
pluginFaker({ paramsCasing: 'camelcase' })

Removed: mapper

The mapper option mapped a property name to a raw Faker expression. v5 removes it, matching the removal on plugin-ts and plugin-zod. Rewrite the property's schema with a macro instead. The default printer turns an enum into faker.helpers.arrayElement([...]), so an enum macro reproduces the common case, and a printer override changes how a schema type renders.

The generated mock is typed against the @kubb/plugin-ts output, so pick values the property's type allows. The v4 mapper bypassed that check with a raw expression.

kubb.config.ts
diff
import { ast } from 'kubb/kit'

pluginFaker({
  mapper: {
    status: `faker.helpers.arrayElement<any>(['available', 'pending'])`,
  },
  macros: [
    {
      name: 'pet-status-values',
      schema(node) {
        if (node.name === 'Pet' && 'properties' in node) {
          return {
            ...node,
            properties: node.properties.map((property) =>
              property.name === 'status'
                ? { ...property, schema: ast.factory.createSchema({ type: 'enum', primitive: 'string', enumValues: ['available', 'pending'] }) }
                : property,
            ),
          }
        }
        return node
      },
    },
  ],
})

Generated output

Generic return type and intermediate variable

The create prefix stays, so createPet is still createPet. The signature and internal structure change. The factory now takes a generic TData and lifts the fake values into a defaultFakeData variable before the spread.

Diff
diff
 export function createPet(data?: Partial<Pet>): Pet {
   return {
     ...{
       id: faker.number.int(),
       ...
     },
     ...(data || {}),
   }
 }
 export function createPet<TData extends Partial<Pet> = object>(data?: TData) {
   const defaultFakeData = {
     id: faker.number.int(),
     ...
   }
   return {
     ...defaultFakeData,
     ...(data || {}),
   } as Omit<typeof defaultFakeData, keyof TData> & TData
 }

The inferred return type keeps the fields you pass in data exactly as typed and fills the rest from defaultFakeData, so an override like createPet({ id: 1 }) reads back id as the literal you set rather than the wider schema type.