Challenge - 5 Problems
Vitest Svelte Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Vitest test for a Svelte component?
Given this Svelte component and Vitest test, what will the test output?
Svelte
/* Counter.svelte */ <script> export let count = 0; function increment() { count += 1; } </script> <button on:click={increment} aria-label="increment">Count: {count}</button> /* Counter.test.js */ import { render, fireEvent } from '@testing-library/svelte'; import { describe, it, expect } from 'vitest'; import Counter from './Counter.svelte'; describe('Counter component', () => { it('increments count on button click', async () => { const { getByRole } = render(Counter, { props: { count: 0 } }); const button = getByRole('button', { name: 'increment' }); expect(button.textContent).toBe('Count: 0'); await fireEvent.click(button); expect(button.textContent).toBe('Count: 1'); }); });
Attempts:
2 left
💡 Hint
Think about how Svelte updates reactive variables and how fireEvent simulates user clicks.
✗ Incorrect
The test renders the Counter component with count 0. Clicking the button triggers increment(), updating count to 1. The button text updates accordingly, so the test passes.
📝 Syntax
intermediate2:00remaining
Which Vitest config snippet correctly sets up Svelte testing?
You want to configure Vitest to work with Svelte components. Which config snippet is correct?
Attempts:
2 left
💡 Hint
Svelte needs its plugin and tests usually run in a browser-like environment.
✗ Incorrect
Option D correctly imports the Svelte plugin and sets the test environment to 'jsdom' which simulates a browser for DOM testing.
🔧 Debug
advanced2:00remaining
Why does this Vitest test for a Svelte component fail with 'TypeError: Cannot read property'?
Examine the test code below. Why does it throw a TypeError during execution?
Svelte
import { render } from '@testing-library/svelte'; import { describe, it, expect } from 'vitest'; import MyComponent from './MyComponent.svelte'; describe('MyComponent', () => { it('renders with prop', () => { const { getByText } = render(MyComponent, { title: 'Hello' }); expect(getByText('Hello')).toBeTruthy(); }); });
Attempts:
2 left
💡 Hint
Check how props are passed to the render function in @testing-library/svelte.
✗ Incorrect
The render function expects an options object with a nested 'props' property. Passing { title: 'Hello' } leaves the component's props undefined, causing a TypeError when accessing the title property.
❓ state_output
advanced2:00remaining
What is the value of 'count' after this Vitest test runs?
Consider this Svelte component and test. What is the final value of 'count' after the test completes?
Svelte
/* Counter.svelte */ <script> export let count = 0; function increment() { count += 1; } </script> <button on:click={increment} aria-label="increment">Count: {count}</button> /* Counter.test.js */ import { render, fireEvent } from '@testing-library/svelte'; import { describe, it, expect } from 'vitest'; import Counter from './Counter.svelte'; describe('Counter', () => { it('increments twice', async () => { const { component, getByRole } = render(Counter, { props: { count: 0 } }); const button = getByRole('button', { name: 'increment' }); await fireEvent.click(button); await fireEvent.click(button); expect(button.textContent).toBe('Count: 2'); }); });
Attempts:
2 left
💡 Hint
Each click triggers increment which adds 1 to count.
✗ Incorrect
The test clicks the button twice, each click increments count by 1, so final count is 2.
🧠 Conceptual
expert2:00remaining
Which statement best describes Vitest's role in Svelte testing?
Choose the most accurate description of what Vitest does when testing Svelte components.
Attempts:
2 left
💡 Hint
Think about how Vitest uses jsdom and testing libraries to simulate browser behavior.
✗ Incorrect
Vitest runs tests in a Node.js environment with jsdom to simulate browser APIs, allowing tests to check component logic and DOM changes without a real browser.