Vitest helps you check if your Svelte code works correctly by running tests automatically.
Vitest setup in Svelte
npm install -D vitest @testing-library/svelte @sveltejs/vite-plugin-svelte // vitest.config.ts import { defineConfig } from 'vitest/config' import { svelte } from '@sveltejs/vite-plugin-svelte' export default defineConfig({ plugins: [svelte()], test: { globals: true, environment: 'jsdom' } })
Install Vitest and Svelte testing tools as developer dependencies.
Configure Vitest to use the Svelte plugin and set the test environment to simulate a browser.
npm install -D vitest @testing-library/svelte @sveltejs/vite-plugin-svelte
// vitest.config.ts import { defineConfig } from 'vitest/config' import { svelte } from '@sveltejs/vite-plugin-svelte' export default defineConfig({ plugins: [svelte()], test: { globals: true, environment: 'jsdom' } })
This test renders a simple Button component, checks if the label appears, and verifies the click event works.
// Example test file: src/__tests__/Button.test.ts import { render, fireEvent } from '@testing-library/svelte' import Button from '../Button.svelte' import { describe, it, expect } from 'vitest' describe('Button component', () => { it('shows the correct label and reacts to click', async () => { const { getByText } = render(Button, { props: { label: 'Click me' } }) const button = getByText('Click me') expect(button).toBeTruthy() let clicked = false button.addEventListener('click', () => { clicked = true }) await fireEvent.click(button) expect(clicked).toBe(true) }) })
Use jsdom environment to simulate browser features in tests.
Use @testing-library/svelte for easy component rendering and user event simulation.
Run tests with npx vitest or add a script in package.json like "test": "vitest".
Vitest helps test Svelte components quickly and simply.
Install Vitest and configure it with the Svelte plugin and jsdom environment.
Write tests using @testing-library/svelte to check component behavior.
Practice
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 DQuick Check:
Vitest = Testing tool [OK]
- Confusing Vitest with build tools
- Thinking Vitest styles components
- Assuming Vitest deploys apps
vite.config.js?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 CQuick Check:
Plugin + jsdom = Correct Vitest config [OK]
- Omitting the Svelte plugin
- Using 'node' instead of 'jsdom' environment
- Importing defineConfig from wrong package
import { render } from '@testing-library/svelte';
import Button from './Button.svelte';
test('shows button text', () => {
const { getByText } = render(Button, { props: { label: 'Click me' } });
getByText('Click me');
});What happens if the component does NOT display the label prop text?
Solution
Step 1: Understand getByText behavior
getByText throws an error if the text is not found in the rendered output.Step 2: Analyze test outcome if label missing
If the component does not show 'Click me', getByText fails and test fails.Final Answer:
The test fails with an error that text was not found -> Option BQuick Check:
getByText missing text = test fail [OK]
- Assuming test passes without text
- Confusing runtime errors with syntax errors
- Thinking warnings allow test to pass
import { defineConfig } from 'vite';
import svelte from '@sveltejs/vite-plugin-svelte';
export default defineConfig({
plugins: [svelte()],
test: {
environment: 'node'
}
});Why might your Svelte component tests fail to run correctly?
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 AQuick Check:
Use jsdom for DOM tests, not node [OK]
- Thinking 'node' environment supports DOM
- Confusing import source for defineConfig
- Misplacing test config outside defineConfig
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
});Solution
Step 1: Test absence when show is false
Use queryByText to check 'Hello' is not rendered (returns null).Step 2: Test presence when show is true
Use getByText to confirm 'Hello' is rendered and found.Final Answer:
Use queryByText for absence and getByText for presence in two renders -> Option AQuick Check:
queryByText null means no text; getByText finds text [OK]
- Using getByText expecting null when text missing
- Not testing both true and false cases
- Confusing queryByText and getByText roles
Ready to prove it?
Pass the test and this pattern moves unknown
