What if a tiny mistake in your code silently ruins your app's core features?
Why Unit testing logic in Svelte? - Purpose & Use Cases
Imagine you write a function to calculate discounts for your online store. You change the code later, but you forget to check if it still works correctly everywhere.
Manually checking every possible input and output is slow, tiring, and easy to miss mistakes. Bugs can sneak in and break your app without you noticing.
Unit testing lets you write small automatic checks for your logic. These tests run quickly and tell you if something breaks right away.
function calculateDiscount(price) {
// manual checks needed every time
if (price > 100) return price * 0.9;
return price;
}
// Manually test with console logs or clicking buttonsimport { test, expect } from 'vitest'; function calculateDiscount(price) { if (price > 100) return price * 0.9; return price; } test('discount applies for price over 100', () => { expect(calculateDiscount(150)).toBe(135); }); // Tests run automatically and catch errors fast
Unit testing makes your code safer to change and helps you build features faster with confidence.
When adding a new payment method, unit tests ensure your discount logic still works without breaking the checkout process.
Manual checks are slow and error-prone.
Unit tests automatically verify your logic.
Tests help catch bugs early and save time.
Practice
Solution
Step 1: Understand unit testing purpose
Unit testing focuses on testing small, isolated parts of code to find bugs early.Step 2: Compare options to unit testing goal
Only To check small parts of code for errors early describes checking small code parts for errors early, which matches unit testing.Final Answer:
To check small parts of code for errors early -> Option CQuick Check:
Unit testing = check small code parts early [OK]
- Confusing unit testing with styling or deployment
- Thinking unit tests write HTML templates
- Assuming unit tests run only in production
Solution
Step 1: Recall correct test syntax
In Vitest/Jest, tests use describe() and it() functions with arrow functions.Step 2: Check each option's syntax
describe('test group', () => { it('test case', () => { /* test code */ }) }) uses correct parentheses and arrow functions. Others miss parentheses or use wrong syntax.Final Answer:
describe('test group', () => { it('test case', () => { /* test code */ }) }) -> Option DQuick Check:
describe and it with arrow functions = correct syntax [OK]
- Omitting parentheses around function arguments
- Using wrong arrow function syntax
- Confusing test and it keywords
describe('sum function', () => {
it('adds two numbers', () => {
const sum = (a, b) => a + b;
expect(sum(2, 3)).toBe(5);
});
});Solution
Step 1: Analyze the sum function and test
The sum function adds two numbers correctly. The test checks if sum(2, 3) equals 5.Step 2: Evaluate the expect statement
expect(sum(2, 3)).toBe(5) compares 5 to 5, which is true, so the test passes.Final Answer:
The test will pass because 2 + 3 equals 5 -> Option AQuick Check:
sum(2,3) = 5, expect toBe(5) passes [OK]
- Assuming sum is undefined
- Confusing toBe with other matchers
- Thinking syntax error occurs here
describe('multiply function', () => {
it('multiplies two numbers', () => {
const multiply = (a, b) => a * b
expect(multiply(2, 3)).toBe(6)
}
});Solution
Step 1: Check syntax of it block
The it block starts with a parenthesis but lacks a closing parenthesis before the closing brace.Step 2: Verify other parts
multiply is defined correctly, toBe is used properly, and describe has a callback function.Final Answer:
Missing closing parenthesis for the it block -> Option AQuick Check:
Syntax error due to missing parenthesis [OK]
- Ignoring missing parentheses errors
- Thinking multiply is undefined
- Assuming toBe usage is wrong
function checkPositive(n) {
return n > 0 ? 'positive' : 'not positive';
}Solution
Step 1: Understand function behavior
checkPositive returns 'positive' if input > 0, else 'not positive'.Step 2: Match test expectations to function logic
expect(checkPositive(5)).toBe('positive'); expect(checkPositive(-1)).toBe('not positive'); tests positive input 5 returns 'positive' and negative input -1 returns 'not positive', matching function logic.Final Answer:
expect(checkPositive(5)).toBe('positive'); expect(checkPositive(-1)).toBe('not positive'); -> Option BQuick Check:
Test matches function output for positive and negative inputs [OK]
- Testing zero as positive incorrectly
- Swapping expected results for inputs
- Testing only one input case
Ready to prove it?
Pass the test and this pattern moves unknown
