0
0
Svelteframework~20 mins

Vitest setup in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vitest Svelte Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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');
  });
});
ATest passes because the count increments from 0 to 1 after click
BTest fails because the count does not update after click
CTest fails with a syntax error in the test file
DTest passes but the button text remains 'Count: 0'
Attempts:
2 left
💡 Hint
Think about how Svelte updates reactive variables and how fireEvent simulates user clicks.
📝 Syntax
intermediate
2: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?
A
import { defineConfig } from 'vitest/config';

export default defineConfig({
  plugins: ['svelte'],
  test: {
    environment: 'jsdom'
  }
});
B
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    environment: 'node'
  }
});
C
import { defineConfig } from 'vitest/config';
import svelte from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
  plugins: [svelte()],
  test: {
    environment: 'node'
  }
});
D
import { defineConfig } from 'vitest/config';
import svelte from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
  plugins: [svelte()],
  test: {
    environment: 'jsdom'
  }
});
Attempts:
2 left
💡 Hint
Svelte needs its plugin and tests usually run in a browser-like environment.
🔧 Debug
advanced
2: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();
  });
});
AThe render function expects props inside a 'props' object, not passed directly
BThe component file MyComponent.svelte is missing and causes the error
CThe getByText method is not available from render
DThe test is missing an async keyword causing the error
Attempts:
2 left
💡 Hint
Check how props are passed to the render function in @testing-library/svelte.
state_output
advanced
2: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');
  });
});
A1
B2
C0
DUndefined
Attempts:
2 left
💡 Hint
Each click triggers increment which adds 1 to count.
🧠 Conceptual
expert
2:00remaining
Which statement best describes Vitest's role in Svelte testing?
Choose the most accurate description of what Vitest does when testing Svelte components.
AVitest replaces Svelte's compiler to generate test-specific component code
BVitest compiles Svelte components into static HTML files for manual testing
CVitest runs tests in a Node.js environment simulating browser APIs to verify component logic and DOM updates
DVitest only runs unit tests on JavaScript functions, ignoring Svelte components
Attempts:
2 left
💡 Hint
Think about how Vitest uses jsdom and testing libraries to simulate browser behavior.