Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content
Official v5.0.0-beta.84 MIT kubb >=5.0.0 node >=22

@kubb/plugin-msw

Generates MSW request handlers from your OpenAPI spec so you can mock the API in tests and during local development.

mswmock-service-workerapi-mockingmockstestingcodegenopenapi
Downloads
118k / mo
Stars
3
Bundle size
193.5 kB
Updated
2d ago

@kubb/plugin-msw

@kubb/plugin-msw turns your OpenAPI spec into MSW request handlers. Drop them into a test setup or a service worker to mock the API. Each handler matches the spec's path, method, status, and response body.

By default a handler returns an empty typed payload you fill in from tests. Set parser: 'faker' to return generated data from @kubb/plugin-faker instead.

Installation

shell
bun add -d @kubb/plugin-msw@beta
shell
pnpm add -D @kubb/plugin-msw@beta
shell
npm install --save-dev @kubb/plugin-msw@beta
shell
yarn add -D @kubb/plugin-msw@beta

Dependencies

This plugin always depends on @kubb/plugin-ts, so keep pluginTs() in the plugins array.

It depends on @kubb/plugin-faker only when you set parser: 'faker'. With the default parser: 'data', Faker is not needed.

Example

typescript
import {  } from 'kubb'
import {  } from '@kubb/plugin-ts'
import {  } from '@kubb/plugin-msw'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  : [
    (),
    ({
      : { : './mocks' },
      : {
        : 'tag',
        : ({  }) => `${}Service`,
      },
      : true,
    }),
  ],
})

See also