How would you unit-test a Svelte date utility with Vitest?

This question tests isolating pure logic from Svelte and configuring Vitest. A good answer covers installing Vitest, co-locating a spec file, and asserting known inputs. A red flag is insisting you must mount a Svelte component to test a plain function.
WHAT THIS TESTS: This question checks whether you understand the boundary between framework code and pure logic in a Svelte codebase. Interviewers want to see that you know a simple date formatter is just JavaScript and does not require component-level or browser-level testing. They also want to confirm you have practical experience with modern Vite tooling, specifically Vitest, which is the recommended test runner for Svelte projects using Vite or SvelteKit.
A GOOD ANSWER COVERS: First, clarify that the function is pure logic and can be tested in isolation without rendering any Svelte components. Second, describe the setup: install Vitest as a dev dependency and update the Vite configuration to resolve browser entry points if needed. Third, explain file placement: co-locate the test file next to the utility, such as dateUtils.js and dateUtils.spec.js, to keep imports simple and discoverability high. Fourth, outline the test structure: import the formatter, pass a known Date or timestamp, and assert that the output string matches the expected format. Fifth, mention running the suite with the Vitest CLI or an npm script.
COMMON WRONG ANSWERS: A major red flag is suggesting you need to mount a Svelte component or use a virtual DOM to test a plain utility. Another mistake is proposing Jest without mentioning the extra configuration Jest requires for Vite projects, since Vitest is the idiomatic choice in the Svelte ecosystem. Candidates also stumble by ignoring edge cases, such as null inputs, invalid dates, or timezone offsets, which reveal whether they think about robustness or just the happy path.
LIKELY FOLLOW-UPS: The interviewer might ask how you would test the same formatter if it were used inside a Svelte component, which opens the door to component testing with render helpers. They could also ask how you would mock the system time to ensure deterministic snapshots across CI runners. Another follow-up is asking how you would structure tests in a monorepo or where you would draw the line between unit tests and component tests.
ONE CONCRETE EXAMPLE: Imagine a file src/lib/formatDate.js that exports a function formatDate which takes an ISO string and returns a human-readable string. You would create src/lib/formatDate.spec.js, import describe, it, and expect from vitest, import formatDate from the source file, and write a test that passes the timestamp 1672531200000 and expects the output to be January 1, 2023. You would then run npx vitest to watch the results. This keeps the feedback loop fast and the test free of any Svelte runtime overhead.
Source: svelte.dev
Read the original → svelte.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.