Mocking Techniques in Vitest

Live Workshops
with Artem ZakharchenkoArtem Zakharchenko

Mocking is one of those topics that can easily split a room of engineers in half.

Some strongly advise you to test your code as-is, avoiding mocks as much as possible. Others would encourage the use of mocking to keep your tests focused on one particular thing, one behavior.

And as much as it is important for you to know different mocking techniques, it is even more so to understand what mocking truly is.

It is time you learned the truth about mocking. And that truth is rather simple:

Mocking is a tool.

It is as good or bad as any other tool out there. What matters is how well you understand what it’s for and how it affects your tests.

This is precisely what you are going to learn in this 2-day workshop! I will help you understand what mocking is, what it has to do with test boundaries, and also master various mocking techniques in Vitest and when to use them.

Who is this workshop for

This workshop is for you if you’ve ever asked yourself any of these questions while testing:

  • Isn’t mocking bad?
  • Should I mock this?
  • How do you even mock this?
  • What is the difference if I mock this or not?

What you will learn

I’ve split this workshop into several blocks, depending on what you will be mocking:

  1. Boundaries. First, you will see how mocking is a tool to establish test boundaries. You will understand what a boundary is and how it affects your test based on where you put it (if you decide to put it at all!).
  2. Functions. Learn how to mock and spy on functions in TypeScript.
  3. Date and time. Wield the power of space and time to mock date, time, timers, and event loop ticks.
  4. Globals. Go through the three fundamental techniques in mocking globals: monkey patching, proxying, and spying. Learn the difference between them, and which one to use in your tests.
  5. Network. Gain full control over the HTTP requests in your tests. Learn how to use Mock Service Worker to mock “happy path” and error responses, as well as network errors and response timings.
  6. Modules. Finally, discover the ways to mock both local and third-party modules in tests.

This is a ton of exercises waiting for you to solve them so you can finally wield mocking with confidence, and also know when you are better off without it.

Required experience

You can make the most out of this workshop regardless of whether you’ve used mocking before.

Having a solid understanding of the Testing Fundamentals is preferable as we won’t be spending much time on things like intentions in tests and test structure.

Past experience with testing frameworks like Jest or Vitest is a plus but is optional.

Share this Live Workshop with your friends

Artem Zakharchenko
Hosted by Artem Zakharchenko

Author @ApiMocking. Instructor @eggheadio. All things testing.