Skip to content

Svelte ​

vitest-auto-spy/svelte turns a class-based service or store into a typed spy. A spy is a stand-in object: every method records its calls and returns what you set. You give the spy to the component the same way it gets the real object: through props, context, or the module it imports.

Through props ​

ts
import { fireEvent, render, screen } from '@testing-library/svelte';
import { expect, it } from 'vitest';
import { asInstance, createSpyFromClass } from 'vitest-auto-spy/svelte';

import Cart from './Cart.svelte';
import { CartStore } from './cart-store';

it('renders the total the store reports', () => {
  const cartStore = createSpyFromClass(CartStore);

  cartStore.total.mockReturnValue(42);

  render(Cart, { props: { store: asInstance(cartStore) } });

  expect(screen.getByText('$42')).toBeInTheDocument();
  expect(cartStore.total).toHaveBeenCalled();
});

it('checks out when the button is clicked', async () => {
  const cartStore = createSpyFromClass(CartStore);

  cartStore.checkout.resolveWith({ orderId: 'ord_42' }); // checkout() returns a Promise

  render(Cart, { props: { store: asInstance(cartStore) } });
  await fireEvent.click(screen.getByRole('button', { name: 'Check out' }));

  expect(cartStore.checkout).toHaveBeenCalledTimes(1);
});

If the prop expects a CartStore with private fields, pass asInstance(cartStore). If it expects only the public store interface, pass cartStore directly.

You set answers with mockReturnValue (a plain value) or resolveWith (a resolved promise), and check calls with the usual toHaveBeenCalled… matchers. toBeInTheDocument needs import '@testing-library/jest-dom/vitest' in your Vitest setup file.

vitest-auto-spy/svelte is an entry: the import path you use in Svelte specs. It exports the core API of the library: createSpyFromClass, createAutoMock, the Spy<T> type, asInstance, resetAutoSpy and the rest. Svelte has no class-based dependency injection, so there are no extra helpers.

Through context ​

If the component reads the store with getContext, pass the spy under the same key:

ts
import { render, screen } from '@testing-library/svelte';
import { expect, it } from 'vitest';
import { createSpyFromClass } from 'vitest-auto-spy/svelte';

import Cart from './Cart.svelte';
import { CART_STORE, CartStore } from './cart-store';

it('renders the total the store reports', () => {
  const cartStore = createSpyFromClass(CartStore);

  cartStore.total.mockReturnValue(42);

  render(Cart, { context: new Map([[CART_STORE, cartStore]]) });

  expect(screen.getByText('$42')).toBeInTheDocument();
});

Through a mocked module ​

If the component imports a ready instance (a singleton) from a module, replace that export with the spy:

ts
import { fireEvent, render, screen } from '@testing-library/svelte';
import { expect, it, vi } from 'vitest';
import { asSpy } from 'vitest-auto-spy/svelte';

import Cart from './Cart.svelte';
import { cartStore } from './cart-store';

vi.mock(import('./cart-store'), async (importOriginal) => {
  const original = await importOriginal();
  const { asInstance, createSpyFromClass } = await import('vitest-auto-spy/svelte');

  return { ...original, cartStore: asInstance(createSpyFromClass(original.CartStore)) };
});

it('checks out through the module singleton', async () => {
  const store = asSpy(cartStore);

  store.checkout.resolveWith({ orderId: 'ord_42' });
  render(Cart);
  await fireEvent.click(screen.getByRole('button', { name: 'Check out' }));

  expect(store.checkout).toHaveBeenCalledTimes(1);
});

Do not read an imported class inside vi.hoisted: Vitest has not initialized the imports yet. Build the spy inside the mock factory after await importOriginal().

Which runner ​

This entry never imports svelte or @testing-library/svelte. It does import vitest, so it does not load under bun test or node --test. On those runners, import createSpyFromClass from your runner's entry (vitest-auto-spy/bun, vitest-auto-spy/node).

If a setup file already imports a runner entry such as vitest-auto-spy/bun, that runner's mocks stay in use after you import this entry.

In depth ​

When vi.mock has nothing to replace ​

vi.mock replaces a module by its path. Some test setups bundle the spec together with its imports into one file (for example, the @angular/build:unit-test builder), so a relative path no longer points to a separate module. Then vi.mock changes nothing and the component uses the real store. Pass the spy through props or context instead.

Released under the MIT License.