TIL: How to Mock Classes Using Vitest

TIL: How to Mock Classes Using Vitest Recently I have been creating a SvelteKit app, when creating a new SvelteKit app you get a choice of different things you can add. Such as using vitest for unit testing. I needed to spy on/mock a method in a class, to see if it was called when a button was pressed and, it was called with the correct arguments. Let’s say we have a Button component which looks like this: ...

Originally Posted On:  2023-04-01 263 words 2 min  Last Modified: 2025-07-15 bc3bbcc