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

svelte-mock

v1.1.0

Published

A package for mocking svelte components with jest.

Readme

svelte-mock Test Status svelte-mock Dev Token NPM Latest Version NPM Downloads GitHub License PRs Welcome

Full Documentation

Table of Contents

Setup

Note: Jest version must be 25.0.0 for code coverage to work

Add the following to your jest config:

transform: {
  '\\.html$': 'svelte-mock/transform',
  '\\.svelte$': 'svelte-mock/transform'
},
setupFilesAfterEnv: [
  'svelte-mock/extend'
],

Usage

You can mock a svelte component with the following code:

jest.mock('Component.svelte')
import Component from 'Component.svelte'
svelteMock.mockImplementation(Component)

If you want to specify your own mock implementations you can pass a svelte component as a second argument:

svelteMock.mockImplementation(Component, MockComponent)

Expect Extensions

svelte-mock includes some useful expect extensions


toHaveInstance

Passes if a mocked component class has been instantiated at least once.

Example

<Component />
expect(Component).toHaveInstance()
  • Component - a svelte component class to be checked for an instance

toHaveInstanceWithProps

Passes if a mocked component class has an instance with props.

Example

<Component first={firstValue} second={secondValue} />
expect(Component).toHaveInstanceWithProps(['first'])
expect(Component).toHaveInstanceWithProps(['first', 'second'])
expect(Component).not.toHaveInstanceWithProps(['nonExistent'])
expect(Component).toHaveInstanceWithProps({ first: firstValue })
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue })
  • Component - a svelte component class to be checked for a matching instance

toHaveInstanceWithBoundProps

Passes if a mocked component class has an instance with bound props.

Example

<Component bind:first=firstValue bind:second=secondValue />
expect(Component).toHaveInstanceWithBoundProps(['first'])
expect(Component).toHaveInstanceWithBoundProps(['first', 'second'])
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent'])
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue })
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue })
  • Component - a svelte component class to be checked for a matching instance

toHaveInstanceWithSlots

Passes if a mocked component class has an instance with the specified slots.

Example

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>
// Check for unnamed slot
expect(Component).toHaveInstanceWithSlots()
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first'])
expect(Component).toHaveInstanceWithSlots(['first', 'second'])
expect(Component).not.toHaveInstanceWithSlots(['nonExistent'])
expect(Component).toHaveInstanceWithSlots({ first: firstSlot })
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot })
  • Component - a svelte component class to be checked for a matching instance

toHaveInstanceWithEventHandlers

Passes if a mocked component class has an instance with event handlers.

Example

<Component on:click="clickFn()" on:custom="customFn()" />
expect(Component).toHaveInstanceWithEventHandlers(['click'])
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom'])
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent'])
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn })
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn })
  • Component - a svelte component class to be checked for a matching instance

toHaveProps

Passes if a component instance has the specified props.

Example

<Component first={firstValue} second={secondValue} />
expect(component).toHaveProps(['first'])
expect(component).toHaveProps(['first', 'second'])
expect(component).not.toHaveProps(['nonExistent'])
expect(component).toHaveProps({ first: firstValue })
expect(component).not.toHaveProps({ first: wrongValue })
  • component - an instance of Component

toHaveBoundProps

Passes if a component instance has the specified bound props.

Example

<Component bind:first=firstValue bind:second=secondValue />
expect(component).toHaveBoundProps(['first'])
expect(component).toHaveBoundProps(['first', 'second'])
expect(component).not.toHaveBoundProps(['nonExistent'])
expect(component).toHaveBoundProps({ first: firstValue })
expect(component).not.toHaveBoundProps({ first: wrongValue })
  • component - an instance of Component

toHaveSlots

Passes if a component instance has the specified slots.

Example

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>
// Check for unnamed slot
expect(component1).toHaveSlots()
// Check for named slots
expect(component2).toHaveSlots(['first'])
expect(component2).toHaveSlots(['first', 'second'])
expect(component2).not.toHaveSlots(['nonExistent'])
expect(component2).toHaveSlots({ first: firstSlot })
expect(component2).not.toHaveSlots({ first: wrongSlot })
  • component1 - an instance of Component
  • component2 - an instance of Component

toHaveEventHandlers

Passes if a component instance has the specified event handlers.

Example

<Component on:click="clickFn()" on:custom="customFn()" />
expect(component).toHaveEventHandlers(['click'])
expect(component).toHaveEventHandlers(['click', 'custom'])
expect(component).not.toHaveEventHandlers(['nonExistent'])
expect(component).toHaveEventHandlers({ click: clickFn })
expect(component).not.toHaveEventHandlers({ click: wrongFn })
  • component - an instance of Component

Query Functions

getInstanceByProps

Returns the first instance for a component that has the specified props.

Example

<Component first={firstValue} second={secondValue} />
Component.getInstanceByProps(['first'])
Component.getInstanceByProps(['first', 'second'])
Component.getInstanceByProps({ first: firstValue })
  • Component - a svelte component class to be checked for an instance

getInstanceByBoundProps

Returns the first instance for a component that has the specified bound props.

Example

<Component bind:first=firstValue bind:second=secondValue />
Component.getInstanceByBoundProps(['first'])
Component.getInstanceByBoundProps(['first', 'second'])
Component.getInstanceByBoundProps({ first: firstValue })
  • Component - a svelte component class to be checked for an instance

getInstanceBySlots

Returns the first instance for a component that has the specified slots.

Example

<Component>
  <span>First</span>
</Component>
<Component>
  <span slot="first">First</span>
  <span slot="second">Second</span>
</Component>
// Get component with unnamed slot
Component.getInstanceBySlots()
// Check for named slots
Component.getInstanceBySlots(['first'])
Component.getInstanceBySlots(['first', 'second'])
Component.getInstanceBySlots({ first: firstSlot })
  • Component - a svelte component class to be checked for an instance

getInstanceByEventHandlers

Returns the first instance for a component that has the specified event handlers.

Example

<Component on:click="clickFn()" on:custom="customFn()" />
Component.getInstanceByEventHandlers(['click'])
Component.getInstanceByEventHandlers(['click', 'custom'])
Component.getInstanceByEventHandlers({ click: clickFn })
  • Component - a svelte component class to be checked for an instance