Vitest is a testing framework used to write and run tests for your Svelte components and JavaScript code. It helps ensure your code works as expected.
Click to reveal answer
beginner
Which command installs Vitest and its related dependencies in a Svelte project?
You run npm install -D vitest @testing-library/svelte @testing-library/jest-dom to install Vitest and testing libraries for Svelte.
Click to reveal answer
intermediate
Where do you add the Vitest configuration in a SvelteKit project?
You add Vitest settings inside the vite.config.js or vite.config.ts file under the test property to customize test behavior.
Click to reveal answer
beginner
How do you run Vitest tests from the command line?
Use npx vitest or add a script like "test": "vitest" in package.json and run npm test.
Click to reveal answer
intermediate
Why is it important to include @testing-library/svelte with Vitest?
Because @testing-library/svelte provides helpful tools to render Svelte components and interact with them in tests, making tests easier and more reliable.
Click to reveal answer
Which command installs Vitest and testing libraries for 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
});