📖 Scenario: You are creating a new Svelte project and want to add testing using Vitest. Vitest helps you run tests easily and quickly.
🎯 Goal: Set up Vitest in a Svelte project by creating the configuration file, adding a test script, writing a simple test, and running it.
📋 What You'll Learn
Create a Vitest configuration file named vitest.config.js with Svelte support
Add a test script named test in package.json to run Vitest
Write a simple test file src/__tests__/example.test.js that checks if 2 + 2 equals 4
Run the test using the npm test command
💡 Why This Matters
🌍 Real World
Setting up Vitest is a common first step when adding automated tests to a Svelte project. It helps catch bugs early and ensures your code works as expected.
💼 Career
Many frontend developer jobs require knowledge of testing frameworks like Vitest to write reliable and maintainable code.
Progress0 / 4 steps
1
Create Vitest configuration file
Create a file named vitest.config.js in the project root. Inside it, write the code to import defineConfig from vitest/config and export the default config with test property having globals: true and environment: 'jsdom'.
Svelte
Hint
Use defineConfig from vitest/config and set globals to true and environment to 'jsdom' inside the test property.
2
Add test script to package.json
Open package.json and add a new script named test with the value vitest inside the scripts section.
Svelte
Hint
Inside package.json, find the scripts section and add "test": "vitest".
3
Write a simple test file
Create a file named src/__tests__/example.test.js. Inside it, write a test using Vitest syntax: import test and expect from vitest, then write a test named 'adds 2 + 2' that expects 2 + 2 to equal 4.
Svelte
Hint
Use test and expect from vitest. Write a test named 'adds 2 + 2' that checks if 2 + 2 equals 4.
4
Run the test
Run the test by executing the command npm test in your terminal. This will use the test script you added to package.json and run Vitest.
Svelte
Hint
Open your terminal and type npm test to run the tests using Vitest.
Practice
(1/5)
1. What is the main purpose of Vitest in a Svelte project?
easy
A. To deploy the app to a server
B. To build the Svelte app for production
C. To style components with CSS
D. To run tests and check if components work correctly
Solution
Step 1: Understand Vitest's role
Vitest is a testing tool used to verify code correctness.
Step 2: Compare options with Vitest's purpose
Only To run tests and check if components work correctly describes testing, which matches Vitest's function.
Final Answer:
To run tests and check if components work correctly -> Option D
Quick Check:
Vitest = Testing tool [OK]
Hint: Vitest is for testing, not building or styling [OK]
Common Mistakes:
Confusing Vitest with build tools
Thinking Vitest styles components
Assuming Vitest deploys apps
2. Which of the following is the correct way to configure Vitest for Svelte testing in vite.config.js?
easy
A. import { defineConfig } from 'vite'; export default defineConfig({ plugins: [], test: { environment: 'node' } });
B. import { defineConfig } from 'vite'; import svelte from '@sveltejs/vite-plugin-svelte'; export default defineConfig({ plugins: [], test: { environment: 'jsdom' } });
C. import { defineConfig } from 'vite'; import svelte from '@sveltejs/vite-plugin-svelte'; export default defineConfig({ plugins: [svelte()], test: { environment: 'jsdom' } });
D. import { defineConfig } from 'vitest'; import svelte from '@sveltejs/vite-plugin-svelte'; export default defineConfig({ plugins: [svelte()] });
Solution
Step 1: Check plugin usage for Svelte
The Svelte plugin must be included in plugins array for Svelte support.
Step 2: Verify test environment setting
Vitest tests for Svelte need 'jsdom' environment to simulate browser.
Final Answer:
Correct config includes Svelte plugin and jsdom environment -> Option C
Quick Check:
Plugin + jsdom = Correct Vitest config [OK]
Hint: Include svelte plugin and set environment to jsdom [OK]
Common Mistakes:
Omitting the Svelte plugin
Using 'node' instead of 'jsdom' environment
Importing defineConfig from wrong package
3. Given this test code for a Svelte component using Vitest and @testing-library/svelte:
Why might your Svelte component tests fail to run correctly?
medium
A. Because the test environment is set to 'node' instead of 'jsdom'
B. Because the Svelte plugin is missing from plugins
C. Because defineConfig must be imported from 'vitest' not 'vite'
D. Because the test property should be outside defineConfig
Solution
Step 1: Check environment setting for browser features
Svelte components need a browser-like environment; 'node' lacks DOM APIs.
Step 2: Understand impact on tests
Using 'node' causes tests that rely on DOM (like rendering) to fail.
Final Answer:
Because the test environment is set to 'node' instead of 'jsdom' -> Option A
Quick Check:
Use jsdom for DOM tests, not node [OK]
Hint: Use 'jsdom' environment for Svelte component tests [OK]
Common Mistakes:
Thinking 'node' environment supports DOM
Confusing import source for defineConfig
Misplacing test config outside defineConfig
5. You want to test a Svelte component that conditionally renders a message only if a prop show is true. Which Vitest test code correctly checks that the message appears only when show is true?
import { render } from '@testing-library/svelte';
import Message from './Message.svelte';
test('message visibility', () => {
// Fill in test code here
});