npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@testing-library/svelte-core

v1.0.0

Published

Core rendering and cleanup logic for Svelte testing utilities.

Readme

@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 type {
  Component,
  Exports,
  Rerender,
} from '@testing-library/svelte-core/types'

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

beforeEach(() => {
  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
)

| Argument | Type | Description | | ------------------ | ------------------------------------------------------- | --------------------------------------------- | | Component | Svelte component | An imported Svelte component | | componentOptions | Props or partial mount options | Options for how the component will be mounted | | setupOptions | { baseElement?: HTMLElement } | Optionally override baseElement |

| Result | Type | Description | Default | | ------------- | ------------------------------------------ | ---------------------------------------- | ----------------------------------- | | baseElement | HTMLElement | The base element | document.body | | container | HTMLElement | The component's immediate parent element | <div> appended to document.body | | component | component exports | The component's exports from mount | N/A | | rerender | (props: Partial<Props>) => Promise<void> | Update the component's props | N/A | | unmount | () => void | Unmount the component from the document | N/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, container, mountOptions } = setup(
  componentOptions,
  setupOptions
)

| Argument | Type | Description | | ------------------ | ------------------------------------------------------- | --------------------------------------------- | | componentOptions | Props or partial mount options | Options for how the component will be mounted | | setupOptions | { baseElement?: HTMLElement } | Optionally override baseElement |

| Result | Type | Description | Default | | -------------- | ------------------------------------ | ---------------------------------------- | ----------------------------------- | | baseElement | HTMLElement | The base element | document.body | | container | HTMLElement | The component's immediate parent element | <div> appended to document.body | | mountOptions | mount options | Validated options to pass to mount | { target, props: {} } |

mount

Mount a Svelte component into the document.

const { component, unmount, rerender } = mount(Component, mountOptions)

| Argument | Type | Description | | -------------- | ----------------------------------------- | -------------------------------------------- | | Component | Svelte component | An imported Svelte component | | mountOptions | component options | Options to pass to Svelte's mount function |

| Result | Type | Description | | ----------- | ------------------------------------------ | --------------------------------------- | | component | component exports | The component's exports from mount | | unmount | () => void | Unmount 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.