📖 Scenario: You are building a simple Svelte app that shows a list of tasks. You want to make sure the app works correctly by testing it step-by-step.
🎯 Goal: Build a Svelte component with a list of tasks, add a variable to track completed tasks, filter tasks based on completion, and finally add a test to check the filtered output.
📋 What You'll Learn
Create a Svelte component with a tasks array containing specific tasks
Add a completedThreshold variable to filter tasks
Use a reactive statement to filter tasks based on completedThreshold
Add a test block that checks the filtered tasks length
💡 Why This Matters
🌍 Real World
Testing ensures your Svelte app behaves correctly before users see it. It helps catch bugs early and keeps your app reliable.
💼 Career
Knowing how to test Svelte components is important for frontend developer roles to maintain quality and confidence in your code.
Progress0 / 4 steps
1
DATA SETUP: Create the initial tasks array
Create a tasks array with these exact objects: { id: 1, name: 'Wash dishes', completed: 2 }, { id: 2, name: 'Do laundry', completed: 5 }, and { id: 3, name: 'Write report', completed: 1 } inside a Svelte component.
Svelte
Hint
Use let tasks = [ ... ] inside the <script> tag.
2
CONFIGURATION: Add a completedThreshold variable
Add a variable called completedThreshold and set it to 2 inside the <script> tag below the tasks array.
Svelte
Hint
Use let completedThreshold = 2; inside the <script> tag.
3
CORE LOGIC: Filter tasks based on completedThreshold
Add a reactive statement called $: filteredTasks that filters tasks to include only those with completed greater than or equal to completedThreshold.
Svelte
Hint
Use $: filteredTasks = tasks.filter(task => task.completed >= completedThreshold);
4
COMPLETION: Add a test block to check filteredTasks length
Add a <script context="module"> block with a test that imports assert from node:assert and checks that filteredTasks.length equals 2.
Svelte
Hint
Use a <script context="module"> block with a test function that uses assert.strictEqual.
Practice
(1/5)
1. Why is testing important for a Svelte application?
easy
A. It changes the app's design without coding.
B. It automatically writes the app code for you.
C. It makes the app load faster in the browser.
D. It checks if the app works as expected by simulating user actions.
Solution
Step 1: Understand the purpose of testing
Testing is used to verify that the app behaves correctly and meets requirements.
Step 2: Identify what testing does in Svelte apps
Testing simulates user actions like clicks and checks if the app responds properly.
Final Answer:
It checks if the app works as expected by simulating user actions. -> Option D
Quick Check:
Testing simulates user actions [OK]
Hint: Testing checks app behavior by simulating users [OK]
Common Mistakes:
Thinking testing writes code automatically
Confusing testing with performance optimization
Believing testing changes app design
2. Which of the following is the correct way to import the testing library in a Svelte test file?
easy
A. import render from 'svelte-testing-library';
B. require('@testing-library/svelte').render;
C. import { render } from '@testing-library/svelte';
D. import { test } from 'svelte-test';
Solution
Step 1: Recall the standard testing library for Svelte
The official library is '@testing-library/svelte' and it exports render as a named export.
Step 2: Check syntax for importing named exports in ES modules
Correct syntax is: import { render } from '@testing-library/svelte';
Final Answer:
import { render } from '@testing-library/svelte'; -> Option C
Quick Check:
Named import { render } [OK]
Hint: Use named import with curly braces for render [OK]
Common Mistakes:
Using require instead of import
Importing default instead of named export
Using wrong package names
3. Given this Svelte test code, what will be the output of the test?