Mocking Techniques in Vitest
More
What you will learn:
In this practical workshop, you will learn:
- The purpose of mocking;
- What and when to mock;
- How to write mocks that are beneficial rather than a maintenance burden;
- How to separate mocks from implementation details;
- How to ensure type safety in mocks;
- And all the mocking techniques you need to test your code!
By solving real-world tasks using the Vitest framework, you will gain a complete understanding of mocking and finally make it a useful tool in your testing arsenal.
Workshop Structure: The workshop consists of 6 blocks of exercises dedicated to different mocking techniques for various data types.
Boundaries
Understand what mocking really is and how it helps create test boundaries. Learn how each mock affects the code being tested with clear examples.
Mocking Functions
Functions are at the core of JavaScript logic. Learn to mock functions for effective testing of input and output data. Create mocks for functions, track their calls, and configure behavior in the code being tested.
Mocking Date and Time
Eliminate side effects of changing date and time in tests. Learn how to “freeze” time and mock time-dependent code to ensure stable test results.
Mocking Global Variables
Explore how to mock implicit dependencies without disruption. Learn to mock global values, methods, and environment variables, creating the perfect testing environment.
Mocking Network Requests
Gain control over network requests and reliably test client-side code. Intercept requests and mock responses using Mock Service Worker, simulating various network scenarios.
Mocking Modules
Understand when to use module mocking and explore alternative techniques. Learn to safely mock modules using Vitest's built-in APIs.
Who this workshop is for: This workshop is intended for all developers, both newcomers to automated testing and those who have been writing tests for years. If you are unsure when to introduce mocks into your test suite or how to do it without having to redo it the next day, this workshop is designed especially for you.
Watch Online Mocking Techniques in Vitest
# | Title | Duration |
---|---|---|
1 | Intro to Mocking Techniques in Vitest | 02:48 |
2 | Intro to Boundaries | 00:43 |
3 | Test boundaries _Problem | 01:40 |
4 | Test boundaries _solution | 02:27 |
5 | Recap of Boundaries | 00:18 |
6 | Intro to Functions | 00:42 |
7 | Mock functions _Problem | 01:57 |
8 | Mock functions _solution | 02:52 |
9 | Spies _Problem | 00:54 |
10 | Spies _solution | 02:22 |
11 | Mock implementation _Problem | 01:32 |
12 | Mock implementation _solution | 03:25 |
13 | Recap of Functions | 00:22 |
14 | Intro to Date and time _solution | 00:59 |
15 | Date and time _Problem | 01:04 |
16 | Date and time _solution | 02:15 |
17 | Timers _Problem | 02:01 |
18 | Timers _solution | 02:47 |
19 | Ticks and tasks _Problem | 02:16 |
20 | Ticks and tasks _solution | 02:15 |
21 | Recap of Date and time | 00:19 |
22 | Intro to Globals | 01:16 |
23 | Global methods _Problem | 00:38 |
24 | Global methods _solution | 02:20 |
25 | Global values _Problem | 01:25 |
26 | Global values _solution | 02:28 |
27 | Environment variables _Problem | 01:22 |
28 | Environment variables _solution | 03:49 |
29 | Recap of Globals | 00:25 |
30 | Intro to Network | 02:17 |
31 | Set up MSW _Problem | 05:02 |
32 | Set up MSW _solution | 01:28 |
33 | Mock responses _Problem | 01:05 |
34 | Mock responses _solution | 02:25 |
35 | Error responses _Problem | 01:40 |
36 | Error responses _solution | 02:59 |
37 | Network errors _Problem | 00:59 |
38 | Network errors _solution | 01:49 |
39 | Response delay _Problem | 00:59 |
40 | Response delay _solution | 02:30 |
41 | Recap of Network | 00:41 |
42 | Intro to Modules | 00:57 |
43 | Dependency injection _Problem | 03:28 |
44 | Dependency injection _solution | 03:45 |
45 | Private side effects _Problem | 02:11 |
46 | Private side effects _solution | 05:28 |
47 | Recap of Modules | 00:31 |
48 | Mocking Techniques in Vitest Outro | 00:41 |