NashTech Blog

Svelte App Testing in a Vite Ecosystem: A Developer’s Guide

Table of Contents
svelte testing library

As frontend developers, we’re constantly balancing rapid development with robust code quality. When working with modern tools like Svelte and Vite, this balance becomes even more critical. Testing Svelte apps, especially those bundled with Vite, offers unique advantages—and challenges. In this guide, we’ll explore practical strategies for testing Svelte applications, share tooling recommendations, and invite you to collaborate and contribute to the ecosystem.

Why Test Svelte Apps?

Svelte’s compiler transforms declarative components into optimized vanilla JavaScript, which means traditional testing approaches might not always apply. Effective testing ensures:

  • Component isolation: Verify UI behaviour without external dependencies.
  • State management: Validate reactive logic and data flow.
  • Performance: Catch regression in bundle size or runtime efficiency.

Setting Up Testing for a Svelte App with Vite

Before we dive into writing tests, let’s make sure we have the right setup. If you haven’t already, create a new Svelte app with Vite:

npm create vite@latest my-svelte-app -- --template svelte
cd my-svelte-app
npm install

Vite’s lightning-fast build tooling pairs seamlessly with Vitest, a Vite-native test runner that leverages your existing configuration. For component testing, the Svelte Testing Library (built on DOM Testing Library) provides utilities to render components, query the DOM, and simulate user interactions.

1. Install the necessary testing tools

To enable unit testing in our Svelte + Vite project, we’ll use Vitest (a Jest-compatible testing framework optimized for Vite). Let’s install it along with Svelte Testing Library for component testing:

npm install --save-dev \
  @testing-library/svelte \
  @testing-library/jest-dom \
  @sveltejs/vite-plugin-svelte \
  vitest \
  jsdom

2. Add vitest setup

Add a vitest-setup.js file to optionally set up @testing-library/jest-dom expect matchers.

// vitest-setup.js
import '@testing-library/jest-dom/vitest'

3. Configure Vitest in the project

Add vitest.config.js, or update your existing vite.config.js, with the svelte and svelteTesting Vite plugins. Set the testing environment to your DOM library of choice and optionally configure your setup file from step (2).

// vite.config.js
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { svelteTesting } from "@testing-library/svelte/vite";

// https://vite.dev/config/
export default defineConfig({
  plugins: [svelte(), svelteTesting()],
  test: {
    environment: "jsdom",
    setupFiles: ["./vitest-setup.js"],
  },
});

4. Update package.json

Add test scripts to your package.json to run the tests with Vitest

{
  "scripts": {
    "test": "vitest run",
    "test:ui": "vitest --ui",
    "test:watch": "vitest"
  }
}

Writing Your First Svelte Component Test

Let’s say we have a simple Counter.svelte component:

<script>
  let count = $state(0)
  const increment = () => {
    count += 1
  }
</script>

<button onclick={increment}>
  count is {count}
</button>

To test this component, create Counter.test.js inside the src directory:

import { render, fireEvent } from "@testing-library/svelte";
import Counter from "./Counter.svelte";
import { expect, test } from "vitest";

test("increments count on button click", async () => {
  const { getByText } = render(Counter);
  const button = getByText("count is 0");
  await fireEvent.click(button);
  expect(button).toHaveTextContent("count is 1");
});

Run the test:

npm run

End-to-End (E2E) Testing with Playwright

For full app validation, Playwright automates browser interactions. David Peng’s demo repo illustrates integrating Playwright with SvelteKit/Vite projects:

1. Install Playwright:

npm init playwright@latest  

2. Write tests to simulate user journeys:

// tests/example.spec.js  
test('navigates to /about', async ({ page }) => {  
  await page.goto('/');  
  await page.click('text=About');  
  await expect(page).toHaveURL('/about');  
});  

Best Practices

  • Test behaviors, not internals : Focus on user-facing outcomes rather than component implementation details.
  • Leverage Vite’s HMR : Vitest’s watch mode refreshes tests instantly during development
  • Combine unit + E2E tests : Unit tests cover edge cases; E2E tests validate integration

Have a better approach? Found a cool tool we should include? Feel free to share your thoughts and contribute! Let’s build better frontend applications together. 🚀

For more such blogs and updates follow Front-end Competency.

Picture of deepaksrivastava

deepaksrivastava

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top