🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@testing-library/svelte-core

Package Overview
Dependencies
Maintainers
16
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@testing-library/svelte-core

Core rendering and cleanup logic for Svelte testing utilities.

latest
Source
npmnpm
Version
1.0.0-next.1
Version published
Maintainers
16
Created
Source

@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

import { beforeEach } from 'vitest'
import * as SvelteCore from '@testing-library/svelte-core'

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

beforeEach(() => {
  SvelteCore.cleanup()
})

export interface RenderResult<
  C extends SvelteCore.Component,
> extends SvelteCore.RenderResult<C> {
  screen: Screen
}

export const render = <C extends SvelteCore.Component>(
  Component: SvelteCore.ComponentImport<C>,
  options: SvelteCore.ComponentOptions<C>
): RenderResult<C> => {
  const renderResult = SvelteCore.render(Component, options)
  const screen = bindQueries(baseElement)

  return { screen, ...renderResult }
}

API

render

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

const { baseElement, container, component, unmount, rerender } = render(
  Component,
  componentOptions,
  setupOptions
)
ArgumentTypeDescription
ComponentSvelte componentAn imported Svelte component
componentOptionsProps or partial mount optionsOptions for how the component will be mounted
setupOptions{ baseElement?: HTMLElement }Optionally override baseElement
ResultTypeDescriptionDefault
baseElementHTMLElementThe base elementdocument.body
containerHTMLElementThe component's immediate parent element<div> appended to document.body
componentcomponent exportsThe component's exports from mountN/A
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)

setup

Validate options and prepare document elements for rendering.

const { baseElement, target, mountOptions } = setup(options, renderOptions)
ArgumentTypeDescription
componentOptionsProps or partial mount optionsOptions for how the component will be mounted
setupOptions{ baseElement?: HTMLElement }Optionally override baseElement
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: {} }

mount

Mount a Svelte component into the document.

const { component, unmount, rerender } = mount(Component, options)
ArgumentTypeDescription
ComponentSvelte componentAn imported Svelte component
mountOptionscomponent optionsOptions to pass to Svelte's mount function
ResultTypeDescription
componentcomponent exportsThe component's exports from mount
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.

Keywords

testing

FAQs

Package last updated on 03 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts