Skip to content

Testing Utilities

ts
import { createMockToast, mockUseToast } from 'vue-toast-kit/testing'

Compatible with Vitest and Jest.

Functions

createMockToast(overrides?)

(overrides?: Partial<ToastItem> & { options?: Partial<ToastOptions> }) => ToastItem

Creates a minimal ToastItem stub for unit tests — useful when testing a component that renders a ToastItem directly (e.g. a custom #toast slot implementation) without going through a real ToastQueue.

mockUseToast(overrides?)

(overrides?: Partial<ToastApi>) => ToastApi

Creates a spy-friendly mock of the ToastApi returned by useToast(). All methods are no-ops that return empty strings unless overridden.

Examples

Mocking useToast() in a component test:

ts
import { createMockToast, mockUseToast } from 'vue-toast-kit/testing'

// In a Vitest / Jest test:
describe('MyComponent', () => {
  it('calls toast.success on save', async () => {
    const mockToast = mockUseToast()
    vi.mock('vue-toast-kit', () => ({ useToast: () => mockToast }))

    // render component, trigger save…

    expect(mockToast.success).toHaveBeenCalledWith('Saved!')
  })
})

Creating a minimal ToastItem stub:

ts
const item = createMockToast({
  message: 'Upload complete',
  options: { type: 'success', duration: 3000 },
})