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)) === str for various inputs
  • URL-safety: output contains no +, /, or = characters
  • Truncation: input longer than maxInputLen gets truncated before encoding
  • Custom maxInputLen parameter
  • 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.svelte suffix
  • Strips route group prefixes like (demo)/
  • Default prefix ../ replaced with /
  • Custom prefix parameter
  • Adds trailing slash
  • Nested routes: ../blog/post-slug/+page.svelte becomes /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) = 0 and f(1) = 1 for 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 element is null

Utility tests

Pure functions, easy wins

A static site template built with SvelteKit.

Sveleton, 2025