Unit tests for utility functions
2 minutes read
Pure functions that can be tested directly with minimal or no mocking. These are the easiest tests to write and provide immediate value.
Part of Add tests and CI integration (sub-task 2 of 4). Requires testing environment setup first.
src/lib/escape.js
escape_html(value, is_attr)
- Content mode: escapes
&and< - Attribute mode: escapes
&,<, and" - Passthrough: strings with no special characters return unchanged
- Edge cases:
null,undefined, numbers, empty string - Multiple special characters in one string
- Special characters at start, middle, and end of string
pathEncode(str, maxInputLen)
- Roundtrip:
pathDecode(pathEncode(str)) === strfor various inputs - URL-safety: output contains no
+,/, or=characters - Truncation: input longer than
maxInputLengets truncated before encoding - Custom
maxInputLenparameter - Unicode strings (emoji, CJK, diacritics)
- Empty string
pathDecode(str)
- Decodes valid base64url strings
- Fallback: returns original string on invalid input (does not throw)
src/lib/url-utils.js
pathToPublicURL(filePath, prefix)
- Strips
+page.sveltesuffix - Strips route group prefixes like
(demo)/ - Default prefix
../replaced with/ - Custom prefix parameter
- Adds trailing slash
- Nested routes:
../blog/post-slug/+page.sveltebecomes/blog/post-slug/
src/lib/scroll-to.js
Easing functions
The easing functions are not exported directly, but can be tested by extracting them or by testing through the module's internal structure.
easeInOutCubic,easeOutSine,easeInOutSine,easeInOutQuint- Boundary:
f(0) = 0andf(1) = 1for all easing functions - Midpoint:
f(0.5)is reasonable (between 0 and 1) - Monotonicity: output increases as input increases from 0 to 1
scrollTo(element, speed, easing)
- Returns early without throwing when
elementisnull
Read next