npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

@testing-library/svelte-core

1.1.3

@GitHub Actions

npmHomeRepoSnykSocket
Downloads:616486
$ npm install @testing-library/svelte-core
DailyWeeklyMonthlyYearly

@testing-library/svelte-core

Do you want to build your own Svelte testing library? You may want to use our rendering core, which abstracts away differences in Svelte versions to provide a simple API to render Svelte components into the document and clean them up afterwards.

Table of Contents

  • Example Usage
  • API
    • render
    • setup
    • wrapperSetup
    • mount
    • cleanup
    • addCleanupTask
    • removeCleanupTask
    • Utility types

Example Usage

import { beforeEach } from 'vitest'
import * as SvelteCore from '@testing-library/svelte-core'
import type {
  Component,
  Exports,
  Rerender,
} from '@testing-library/svelte-core/types'

import { bindQueries, type Queries } from './bring-your-own-queries.js'

beforeEach(async () => {
  // Required to use the `wrapper` render option
  await SvelteCore.wrapperSetup()
  SvelteCore.cleanup()
})

export interface RenderResult<C extends Component> extends Queries {
  container: HTMLElement
  component: Exports<C>
  rerender: Rerender<C>
  unmount: () => void
}

export const render = <C extends SvelteCore.Component>(
  Component: SvelteCore.ComponentImport<C>,
  options: SvelteCore.ComponentOptions<C>
): RenderResult<C> => {
  const { baseElement, component, container, rerender, unmount } =
    SvelteCore.render(Component, options)

  const queries = bindQueries(baseElement)

  return { component, container, rerender, unmount, ...queries }
}

API

render

Set up the document and mount a component into that document.

const { baseElement, container, component, rerender, unmount } = render(
  Component,
  componentOptions,
  setupOptions
)
ArgumentTypeDescription
ComponentSvelte componentAn imported Svelte component
componentOptionsProps or partial mount optionsOptions for how the component will be mounted
setupOptionsSetupOptionsOptionally override baseElement or wrap the component
ResultTypeDescriptionDefault
baseElementHTMLElementThe base elementdocument.body
containerHTMLElementThe component's immediate parent element<div> appended to document.body
componentcomponent exportsThe component's exports from mountN/A
wrappercomponent exportsThe wrapper's exports, if a wrapper was providedundefined
rerender(props: Partial<Props>) => Promise<void>Update the component's propsN/A
unmount() => voidUnmount the component from the documentN/A

[!TIP] Calling render is equivalent to calling setup followed by mount

const { baseElement, container, mountOptions } = setup(
  componentOptions,
  setupOptions
)
const { component, rerender, unmount } = mount(
  Component,
  mountOptions,
  setupOptions
)

setup

Validate options and prepare document elements for rendering.

const { baseElement, container, mountOptions } = setup(
  componentOptions,
  setupOptions
)
ArgumentTypeDescription
componentOptionsProps or partial mount optionsOptions for how the component will be mounted
setupOptionsSetupOptionsConfigure the document and wrap the component

setupOptions accepts the following fields, all optional:

FieldTypeDescriptionDefault
baseElementHTMLElementThe base element to bind queries todocument.body
wrapperSvelte componentA component to wrap the component under test, e.g. a context providerundefined
wrapperPropsPropsProps to pass to the wrapper componentundefined

[!IMPORTANT] Using the wrapper option requires awaiting wrapperSetup beforehand, e.g. in a beforeEach hook.

ResultTypeDescriptionDefault
baseElementHTMLElementThe base elementdocument.body
containerHTMLElementThe component's immediate parent element<div> appended to document.body
mountOptionsmount optionsValidated options to pass to mount{ target, props: {} }

wrapperSetup

Load the wrapper scaffold for the installed version of Svelte. Await this before rendering with the wrapper option, e.g. in a beforeEach hook. Rendering with a wrapper before wrapperSetup resolves throws a WrapperNotSetupError.

await wrapperSetup()

The scaffold is loaded once and cached, so calling wrapperSetup repeatedly is cheap.

mount

Mount a Svelte component into the document.

const { component, wrapper, unmount, rerender } = mount(
  Component,
  mountOptions,
  setupOptions
)
ArgumentTypeDescription
ComponentSvelte componentAn imported Svelte component
mountOptionscomponent optionsOptions to pass to Svelte's mount function
setupOptionsSetupOptionsOptionally wrap the component (wrapper, wrapperProps)
ResultTypeDescription
componentcomponent exportsThe component's exports from mount
wrappercomponent exportsThe wrapper's exports, if a wrapper was provided
unmount() => voidUnmount the component from the document
rerender(props: Partial<Props>) => Promise<void>Update the component's props

cleanup

Cleanup rendered components and added elements. Call this when your tests are over.

cleanup()

addCleanupTask

Add a custom cleanup task to be called with cleanup()

addCleanupTask(() => {
  // ...reset something
})

removeCleanupTask

Remove a cleanup task from cleanup(). Useful if a cleanup task can only be run once and may be run outside of cleanup

const customCleanup = () => {
  // ...reset something
}

addCleanupTask(customCleanup)

const manuallyCleanupEarly = () => {
  customCleanup()
  removeCleanupTask(customCleanup)
}

Utility types

This module exports various utility types from @testing-library/svelte-core/types. They adapt to whatever Svelte version is installed, and can be used to get type signatures for imported components, props, exports, etc.

See ./types.d.ts for the full list of available types.