📖 Scenario: You are building a simple Svelte component that shows a greeting message and a button to change the greeting. You want to write tests to check that the component behaves correctly when the button is clicked.
🎯 Goal: Create a Svelte component called Greeting.svelte with a greeting message and a button. Then write tests using Testing Library to verify the initial message and the message after clicking the button.
📋 What You'll Learn
Create a Svelte component with a greeting message and a button
Add a variable to hold the greeting text
Write a test to check the initial greeting message
Write a test to check the greeting message after clicking the button
💡 Why This Matters
🌍 Real World
Testing components ensures your app works correctly when users interact with it, preventing bugs and improving quality.
💼 Career
Component testing with Testing Library is a common skill for frontend developers working with Svelte or other frameworks to deliver reliable user interfaces.
Progress0 / 4 steps
1
Create the Greeting component with initial message
Create a Svelte component file called Greeting.svelte. Inside it, declare a variable called greeting with the value "Hello, world!". Then add a <p> tag that displays the greeting variable.
Svelte
Hint
Use let greeting = "Hello, world!"; inside the <script> tag and display it inside a <p> tag.
2
Add a button to change the greeting
In Greeting.svelte, add a <button> element with the text Change Greeting. Add a click event handler that changes the greeting variable to "Hello, Svelte!" when the button is clicked.
Svelte
Hint
Define a function changeGreeting that sets greeting to "Hello, Svelte!". Attach it to the button's on:click event.
3
Write a test for the initial greeting message
Create a test file called Greeting.test.js. Import render and fireEvent from @testing-library/svelte and import the Greeting component. Write a test named "shows initial greeting" that renders Greeting and checks that the text "Hello, world!" is in the document.
Svelte
Hint
Use render(Greeting) and getByText('Hello, world!') to check the initial message.
4
Write a test for greeting change on button click
In Greeting.test.js, add a test named "changes greeting on button click". Render the Greeting component. Use getByText to find the button with text "Change Greeting". Use fireEvent.click on the button. Then check that the text "Hello, Svelte!" is now in the document.
Svelte
Hint
Find the button with getByText('Change Greeting'), click it with fireEvent.click, then check for 'Hello, Svelte!'.
Practice
(1/5)
1. What is the main goal of component testing in Svelte using Testing Library?
easy
A. To check if the component behaves correctly from a user's perspective
B. To test the internal variables and functions of the component
C. To measure the performance speed of the component
D. To verify the component's file size and bundle output
Solution
Step 1: Understand component testing purpose
Component testing focuses on how the component behaves and appears to users, not internal code details.
Step 2: Match Testing Library's approach
Testing Library encourages testing visible output and user interactions, aligning with user perspective testing.
Final Answer:
To check if the component behaves correctly from a user's perspective -> Option A
Quick Check:
User-focused testing = To check if the component behaves correctly from a user's perspective [OK]
Hint: Focus on user-visible behavior, not internal code [OK]
Common Mistakes:
Testing internal variables instead of user output
Confusing performance testing with component testing
Checking file size instead of behavior
2. Which of the following is the correct way to import the render function from Testing Library for Svelte?
easy
A. import { render } from 'svelte';
B. import render from 'svelte-testing-library';
C. import { render } from '@testing-library/svelte';
D. import { renderComponent } from '@testing-library/svelte';
Solution
Step 1: Identify the official Testing Library package for Svelte
The official package is '@testing-library/svelte' which exports the render function.
Step 2: Check correct import syntax
The render function is a named export, so it must be imported with curly braces.
Final Answer:
import { render } from '@testing-library/svelte'; -> Option C
Quick Check:
Named import from '@testing-library/svelte' = import { render } from '@testing-library/svelte'; [OK]
Hint: Use named import from '@testing-library/svelte' for render [OK]
Common Mistakes:
Using default import instead of named import
Importing from wrong package name
Using incorrect function name like renderComponent
3. Given this test code snippet, what will be the output of the assertion?
B. Not passing initial count prop to Counter component
C. Incorrect query getByText for the button label
D. Using button.click() instead of fireEvent.click(button)
Solution
Step 1: Check how user events are simulated
Testing Library requires using fireEvent or userEvent to simulate clicks, not direct button.click() calls.
Step 2: Verify other parts of the test
Counter is imported, getByText queries are correct, and initial props are optional here.
Final Answer:
Using button.click() instead of fireEvent.click(button) -> Option D
Quick Check:
Use fireEvent for clicks, not button.click() [OK]
Hint: Use fireEvent or userEvent for clicks, not element.click() [OK]
Common Mistakes:
Calling element.click() directly instead of fireEvent
Forgetting to import the component
Using wrong text in getByText query
5. You want to test a Svelte component that conditionally shows a message only after a button click. Which Testing Library approach correctly waits for the message to appear before asserting?